function getXmlHttpRequest()
{
    var httpRequest = null;
    try
	{
	    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
    catch (e)
	{
	    try
		{
		    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		    {
			httpRequest = null;
		    }
	}
    if (!httpRequest && typeof XMLHttpRequest != "undefined"){
	try{
	    httpRequest = new XMLHttpRequest();
	} catch (e) {
	    httpRequest = null;
	} 
    }
    return httpRequest;
} 
function getUrlSync(url)
{
	return getUrl(url, false, null);
}

// call a url
function getUrl(url, async, handleStateChange) {
	var xmlHttpReq = getXmlHttpRequest();
	if (!xmlHttpReq)
		return;
	if (handleStateChange){
	    xmlHttpReq.onreadystatechange = function(){
		handleStateChange(xmlHttpReq);
	    };
	}else{
	    xmlHttpReq.onreadystatechange = function() {;}
	}
	xmlHttpReq.open("GET", url, async);
	xmlHttpReq.send(null);
}
function execOnSuccess(stateChangeCallback)
{
    return function(xmlHttpReq){
	    if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200){
		stateChangeCallback(xmlHttpReq);
	    }
	    //	    alert(xmlHttpReq + " " + xmlHttpReq.readyState + " " + xmlHttpReq.status);
	};
}
function getUrlResponseCallbackSaveEmail(xmlHttpReq) {
	if(xmlHttpReq.responseText == null){
	    document.getElementById("messages").innerHTML= "Mala Mia! trata mas tarde o enviale un email al webmaster a <font color=\"#cc0000\">webmaster@dembow.tv</font>";
	    //	    alert(httpreq_error);
	    return;
	}
	var response = xmlHttpReq.responseText;
	var response_code = response.split(",");
	if(response_code[0]=="1"){
	    //add here the sucess shit and delete the whole form
	    document.getElementById("form").innerHTML = "";
	    document.getElementById("messages").innerHTML = "<strong>Gracias su direccion de email ah sido guardada.</strong>";
	} else {
	    //alert(response_code[0]);
	    if (response_code[1].length == 0 || response_code[0]== null){
		document.getElementById("messages").innerHTML = "<strong>Error en el servidor trata mas tarde</strong>";
	    }
	    if(response_code[1]==0){
		document.getElementById("messages").innerHTML = "<strong>Formato erroneo enviale un email al webmaster@dembow.tv con la direccion.</strong>";
	    }else if(response_code[1]==1){
		document.getElementById("messages").innerHTML = "<strong>Error en la base de dato trata mas tarde.</strong>";
	    }else if(response_code[1]==2){
		document.getElementById("messages").innerHTML = "<strong>Mala Tuya! La direccion de email ya esta guardada en la base de dato bajo esta categoria</strong>";
	    }
	}
}
function urlEncodeDict(dict)
{ 
    var result = "";
	for (var i=0; i<3; i++) {
	    if(i=="0"){
		result += encodeURIComponent(dict[i].name) + "=" + encodeURIComponent(dict[i].value);
	    }else{
		result += "&" + encodeURIComponent(dict[i].name) + "=" + encodeURIComponent(dict[i].value);
	    }
	}
	return result;
}
//check all this shit and copy on php
function emailCheck(emailStr) {
    /* The following pattern is used to check if the entered e-mail address
       fits the user@domain format.  It also is used to separate the username
       from the domain. */
    var emailPat= /^(.+)@(.+)$/;
    /* The following string represents the pattern for matching all special
       characters.  We don't want to allow special characters in the address. 
       These characters include ( ) < > @ , ; : \ " . [ ]    */
    var specialChars= '\\(\\)<>@,;:\\\\\\\"\\.\\[\\]';
    /* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
    var validChars="\[^\\s" + specialChars + "\]";
    /* The following pattern applies if the "user" is a quoted string (in
       which case, there are no rules about which characters are allowed
       and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
       is a legal e-mail address. */
    var quotedUser="(\"[^\"]*\")";
    /* The following pattern applies for domains that are IP addresses,
       rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
       e-mail address. NOTE: The square brackets are required. */
    var ipDomainPat='/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/';
    /* The following string represents an atom (basically a series of
       non-special characters.) */
    var atom=validChars + '+';
    /* The following string represents one word in the typical username.
       For example, in john.doe@somewhere.com, john and doe are words.
       Basically, a word is either an atom or quoted string. */
    var word="(" + atom + "|" + quotedUser + ")";
    // The following pattern describes the structure of the user
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    /* The following pattern describes the structure of a normal symbolic
       domain, as opposed to ipDomainPat, shown above. */
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    /* Finally, let's start trying to figure out if the supplied address is
       valid. */
    /* Begin with the coarse pattern to simply break up user@domain into
       different pieces that are easy to analyze. */
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
	/* Too many/few @'s or something; basically, this address doesn't
	   even fit the general mould of a valid e-mail address. */
	return "false,<strong>Mala tuya! checkea el email que este en esta forma: webmaster<font color=\"#cc0000\">@</font>dembow<font color=\"#cc0000\">.</font>tv</strong>";
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    // See if "user" is valid 
    if (user.match(userPat)==null) {
	// user is not valid
	return "false,<strong>Mala Tuya! El nombre de usuario no es valido. <font color=\"#cc0000\">webmaster</font>@dembow.tv</strong>";
    }
    /* if the e-mail address is at an IP address (as opposed to a symbolic
       host name) make sure the IP address is valid. */
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
	// this is an IP address
	for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
		return "false,<strong>El IP address es incorrecto webmaster@<font color=\"#cc0000\">127.0.0.1</font>.tv</strong>";
	    }
	}
	return "true,";
    }
    // Domain is symbolic name
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
	return "false,<strong>Mala Tuya! El domain name esta mal escrito webmaster@<font color=\"#cc0000\">dembow</font>.tv</strong>";
    }
    /* domain name seems valid, but now make sure that it ends in a
       three-letter word (like com, edu, gov) or a two-letter word,
       representing country (uk, nl), and that there's a hostname preceding 
       the domain or country. */
    /* Now we need to break up the domain to get a count of how many atoms
       it consists of. */
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
	// the address must end in a two letter or three letter word.
	return "false,<strong>Mala Tuya! la direccion tiene que terminar con 3 letras o 2 letras. webmaster@dembow. <font color=\"#cc0000\">tv</font></strong>";
    }
    // Make sure there's a host name preceding the domain.
    if (len<2) {
	return "false,This address is missing a hostname!";
    }
    return "true,";
}
function save_email(){
    //make the split
    var email = emailCheck(document.getElementById("user_email").value).split(",");
    if(email[0]=="false"){
	document.getElementById('messages').innerHTML = email[1];
	return false;
    }
    var xmlHttpReq = getXmlHttpRequest();
    if (!xmlHttpReq){
	document.getElementById('register_newsletter').submit();
    } else {
	document.getElementById("type").value = "0";
	getUrl("save_email.php?" + urlEncodeDict(document.getElementById("register_newsletter")), true, execOnSuccess(getUrlResponseCallbackSaveEmail));
    }
    return false;
}
function popup(){
    window.open("privacy.html","privacy","height=300,status=1,dependent,top,width=300");
}
//this its so the enter works i thing the flash its screwing this up
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}      
