﻿function Comment(postId) {
    OpenPop("Comment.aspx?postId=" + postId, "comment", 400, 300, 0);
}

function Collapse(pollId) {
    div = document.getElementById("PollDiv" + pollId);
    img = document.getElementById("PollCollapseImage" + pollId);
    
    if (div.style.display == "") {
        div.style.display = "none";
        img.src = "Resources/Images/Pics/uncollapse.gif";
    } else {
        img.src = "Resources/Images/Pics/collapse.gif";
        div.style.display = "";
    }
}
function Vote(pollId) {
    i = 0;
    while(true) {
        check = document.getElementById("PollOption" + pollId + i);
        if (check == undefined) {
            break;
        }
        if (check.checked) {
            VoteFor(pollId, check.value);
            return;
        }
        i++;
    }
    alert("Tiene que escojer una opción");
}
function VoteFor(pollId, pollOptionId) {
    parameters = "PollID[=]" + pollId + "[|]PollOptionID[=]" + pollOptionId;
	SubmitAjax("VoteFor", parameters);
    document.forms[0].submit();
}

function FromSpanishCharset(text) {
    if (text != null) {
        text = ReplaceAll(text,"&Aacute;", "Á");
        text = ReplaceAll(text,"&Eacute;", "É");
        text = ReplaceAll(text,"&Iacute;", "Í");
        text = ReplaceAll(text,"&Oacute;", "Ó");
        text = ReplaceAll(text,"&Uacute;", "Ú");
        text = ReplaceAll(text,"&aacute;", "á");
        text = ReplaceAll(text,"&eacute;", "é");
        text = ReplaceAll(text,"&iacute;", "í");
        text = ReplaceAll(text,"&oacute;", "ó");
        text = ReplaceAll(text,"&uacute;", "ú");
        text = ReplaceAll(text,"&Ntilde;", "Ñ");
        text = ReplaceAll(text,"&ntilde;", "ñ");
        text = ReplaceAll(text,"&reg;", "®");
        text = ReplaceAll(text,"&copy;", "©");
        text = ReplaceAll(text,"&cent;", "¢");
        text = ReplaceAll(text,"&iexcl;", "¡");
        text = ReplaceAll(text,"&iquest;", "¿");    
        text = ReplaceAll(text,"&ldquo;", "“");    
        text = ReplaceAll(text,"&rdquo;", "”");
        text = ReplaceAll(text,"&quot;", "\"");
    }
    return text;
}

function ReplaceAll(argvalue, y, x) {
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }
  return argvalue;
}

function PostBack(eventTarget, eventArgument) {
    theForm = document.forms[0];
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;        
        theForm.submit();
    }
}

function VisObject(ck, id) {
    ckc = document.getElementById(ck);
    
    con = document.getElementById(id);
    if (ckc.checked == false) {    
        con.style.display = "none";
    } else {
        con.style.display = "";
    }
}
function DisObject(ck, id) {
    ckc = document.getElementById(ck);    
    con = document.getElementById(id);
    if (ckc.checked == false) {    
        con.disabled = true;
    } else {
        con.disabled = false;
    }
}

function Focus(id) {
    cc = document.getElementById(id);
    cc.focus();
    cc.select();
}

function SubmitAjax(action, parameters) {
	if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) {
            xmlhttp.overrideMimeType("plain/text");
        }
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (document.location.toString().indexOf("?") == -1) {
		url = document.location + "?ajax=true&action=" + action;
	} else {
		url = document.location + "&ajax=true&action=" + action;
	}	
		
	xmlhttp.open("POST", url, false);	
	xmlhttp.setRequestHeader("Content-Type", "plain/text; charset=UTF-8");	
	xmlhttp.send(FromSpanishCharset(parameters));
	
	while (xmlhttp.readyState != 4) {
		setTimeout("Sleep()",600);
	}
		
	return xmlhttp.responseText;
}

function Sleep() {
    return "";
}

function Trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function OpenPop(url, name, width, height, sc) {    
    l = (screen.width) ? (screen.width - width) / 2 : 0;
    t = (screen.height) ? (screen.height - height) / 2 : 0;
    params = "width=" + width + "px; height=" + height + "px; toolbar=0; scrollbars=" + sc + "; location=0; statusbar=0; menubar=0; resizable=0; top=" + t + "; left=" + l;
    window.open(url, name, params);
}

function OpenDialog(url, width, height) {
    return window.showModalDialog(url,'Admin','center:yes;resizable:no;dialogHeight:' + height + 'px; dialogWidth:' + width + 'px');
}

function CallPageFromChild(page) {
    parent.window.location = page;
}
 
function CallPage(page) {
    window.location = page;
}
           
function DoDelete() {
    return confirm("Está seguro que quiere borrar este registro?");
}

function DoFileDelete() {
    return confirm("Está seguro que quiere borrar este archivo?");
}

function DoExit() {    
    return confirm("Seguro que quiere cerrar la sessión?");
} 
        
function ValNumber(e, obj, usePoint) {    
	if (e.keyCode == 46 && !usePoint) {
		return false;
	}		

	if (obj.value.indexOf(".") > -1 && e.keyCode == 46) {
		return false;
	}

	if (obj.value.indexOf("-") > -1 && e.keyCode == 45) {
		return false;
	}

	if (e.keyCode == 46) return true;
	if (e.keyCode == 8) return true;
	if (e.keyCode == 9) return true;
	if (e.keyCode == 16) return true;
	if (e.keyCode == 37) return true;
	if (e.keyCode == 38) return true;
	if (e.keyCode == 45) return true;
	if (e.keyCode == 40) return true;
	if (e.keyCode == 27) return true;
	if (e.keyCode == 13) return true;

	if (e.keyCode > 47 && e.keyCode < 58) {
		return true;
	}					
	return false;
}