// JavaScript Document



function checkForm()

{

	/*var pck = false;

	for(var ii=1; ii<=6; ii++)

		if( Boolean( $('r'+ii).checked ))

			pck = true;

	if(!pck)

	{

		CollapsiblePanel1.open();

		return error(null,'Por%20favor%20seleccione%20un%20plan');

	}*/

	if(isEmpty('ptype'))

	{

		CollapsiblePanel1.open();

		return error(null,'Por%20favor%20seleccione%20un%20plan');

	}

	if(isEmpty('first_name'))

		return error('first_name',errstr[0]);

	if(isEmpty('email'))

		return error('email',errstr[0]);

	if(!isValidEmail($('email').value))

		return error('email',errstr[5]);

	if(isEmpty('username'))

		return error('username',errstr[0]);

	if(!isValidString($('username').value))

		return error('username',errstr[6]);

	if(isEmpty('password1'))

		return error('password1',errstr[0]);

	if(isEmpty('password2'))

		return error('password2',errstr[0]);

	if($('password1').value != $('password2').value)

		return error(null,errstr[3]);

	if(isEmpty('subdomain'))

		return error('subdomain',errstr[0]);

	if(!isValidString($('subdomain').value))

		return error('subdomain',errstr[6]);

	//if(!$('accept').checked)

	//	return error('accept',errstr[1]);

	if(!proceedUser || !proceedSubd)

		return error(null,errstr[2]);

	return true;

}

function checkForm3()

{

	if(isEmpty('first_name'))

		return error('first_name',errstr[0]);
	
	if(isEmpty('email'))

		return error('email',errstr[0]);

	if(!isValidEmail($('email').value))

		return error(null,errstr[5]);

	if(isEmpty('phone'))

		return error('phone',errstr[0]);

	
	

	return true;

}





function checkForm2()

{

	if(isEmpty('first_name'))

		return error('first_name',errstr[0]);
		
	if(isEmpty('company'))

		return error('company',errstr[0]);


	if(isEmpty('last_name'))

		return error('last_name',errstr[0]);

	if(isEmpty('email'))

		return error('email',errstr[0]);

	if(!isValidEmail($('email').value))

		return error(null,errstr[5]);

	if(isEmpty('telephone'))

		return error('telephone',errstr[0]);

	if(isEmpty('address'))

		return error('address',errstr[0]);

	if(isEmpty('city'))

		return error('city',errstr[0]);

	if(stateIsText)

	{

		if(isEmpty('state'))

			return error('state',errstr[0]);

	}

	if(isEmpty('zip_code'))

		return error('zip_code',errstr[0]);

	return true;

}

function login()

{

	if(isEmpty('header_username'))

		return error('header_username',errstr[0]);

	if(isEmpty('header_password'))

		return error('header_password',errstr[0]);

	$('header_login').submit();

}

function checkInvoice()

{

	if(isEmpty('rfc'))

		return error('rfc',errstr[0]);

	if(isEmpty('name'))

		return error('name',errstr[0]);

	if(isEmpty('address'))

		return error('address',errstr[0]);

	if(isEmpty('colony'))

		return error('colony',errstr[0]);

	if(isEmpty('zip_code'))

		return error('zip_code',errstr[0]);

	/*if(isEmpty('telephone'))

		return error('telephone',errstr[0]);*/

	

	return true;

}



var proceedUser = false;

var proceedSubd = false;



function checkAva(elem)

{

	if(elem.name == 'username')

		proceedUser = false;

	else

		proceedSubd = false;

	var fnm = elem.getAttribute('fnm');

	var wlh =  window.location.href;

	var lang = wlh.substr(5+wlh.indexOf('lang='),3);

	if(!isEmpty(elem.id))

		loadAsync('dummy.php?lang='+lang+'&f='+elem.name+'&v='+elem.value+'&fnm='+fnm, elem.name+'_resp', elem)

}



/* JISG */



function Ajax()

{

	if(typeof XMLHttpRequest != 'undefined')

	{

		return new XMLHttpRequest();

	}

	else if(window.ActiveXObject)

	{

		var mtVersion = ['MSXML2.XMLHttp.5.0', 'MSXML2.XMLHttp.4.0',

						 'MSXML2.XMLHttp.3.0', 'MSXML2.XMLHttp', 'Microsoft.XMLHttp'];

		for(var i=0; i<mtVersion.length; i++)

		{

			try

			{

				var objXmlHttp = new ActiveXObject(mtVersion[i]);

				return objXmlHttp;

			}

			catch(e){ }

		}

	}

	throw new Error("Ajax object couldn't be created");

}

function loadAsync(url, cbDiv, elem)

{

	$(cbDiv).innerHTML = waitMessage;

	var ajaxObj	= new Ajax();

	ajaxObj.open('GET', url, true); // Peticion asincrona

	ajaxObj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	ajaxObj.onreadystatechange = function()

	{

		if(ajaxObj.readyState==4)

		{

			if(ajaxObj.status==200)

			{

				var resp = ajaxObj.responseText.split(ARS);

				respProc(resp, cbDiv, elem);

			}

			else

			{

				ajaxObj.abort();

				$(cbDiv).innerHTML = "Request timeout";

			}

		}

	}

	ajaxObj.send(null);

}

function respProc(resp, cbDiv, elem)

{

	var code = resp[0];

	proceed = (isNaN(parseInt(code,10)) || parseInt(code,10) > 0 ) ? false : true;

	if(elem.name == 'username')

		proceedUser = proceed;

	else

		proceedSubd = proceed;

	$(cbDiv).innerHTML = resp[1];

}

function isEmpty(fieldName)

{

	var cont = $(fieldName).value;

	if(cont == null || cont.length == 0)

		return true;

	return Boolean(/^\s*$/.test(cont));

}

function error(fieldName, reason)

{

	var msg = '';

	if(fieldName != null)

	{

		try{ msg = unescape($(fieldName).getAttribute('fnm'))+' '; } 

		catch (e){}

		attention(fieldName);

	}

	alert(msg + unescape(reason));

	return false;

}

function attention(fieldName)

{

	$(fieldName).style.backgroundColor = '#FFFF88';

	$(fieldName).focus();

}

function unattention(fieldRef)

{

	fieldRef.style.backgroundColor = '';

}

function $(elem)

{

	return document.getElementById(elem);

}

function toggle()

{

	var act = $('shipform').style.display;

	if(act == 'none')

		$('shipform').style.display = 'block';

	else

		$('shipform').style.display = 'none';

}



function updURLParam(name, value) /* JISG */

{

	var wlh = window.location.href;

	var n = name + '=';

	var r = '';

	if(wlh.indexOf('?')!= -1)

	{

		if(wlh.indexOf(n)!= -1)

			wlh = wlh.substr(0, wlh.indexOf(n)) +n+value + wlh.substr(wlh.indexOf(n) + n.length + value.length);

		else

			r = '&'+n+value;

	}

	else

		r = '?'+n+value;

	window.location.href = wlh + r;

}



function isValidEmail(email)

{

    if(email == null || email.length == 0)

		return false;

    if (! allValidChars(email))  // check to make sure all characters are valid

        return false;

    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character

        return false;

    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @

      	return false;

    } else if (email.indexOf("@") == (email.length-1)) {  // @ must not be the last character

        return false;

    } else if (email.indexOf("@.") >=0) { // dot cant be after a @

		return false;

	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid

		return false;

    } else if (email.indexOf(".") == (email.length-1)) {  // . must not be the last character

		return false;

    }

    return true;

}



function allValidChars(email)

{

  var parsed = true;

  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";

  for (var i=0; i < email.length; i++) {

    var letter = email.charAt(i).toLowerCase();

    if (validchars.indexOf(letter) != -1)

      continue;

    parsed = false;

    break;

  }

  return parsed;

}

function isValidString(email)

{

  var parsed = true;

  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789-_";

  for (var i=0; i < email.length; i++) {

    var letter = email.charAt(i).toLowerCase();

    if (validchars.indexOf(letter) != -1)

      continue;

    parsed = false;

    break;

  }

  return parsed;

}



function datos()

{

	window.open('datos.html','datosBL','width=300,height=200,top=250,left=100,scrollbars=no,resizable=no,menubar=no,location=no,toolbar=no');

}



var htmlFieldCode;

var stateIsText = false;

function swapField(elem)

{

	if(elem.value == 1)

	{

		$('alternField').innerHTML = htmlFieldCode;

		stateIsText = false;

	}

	else

	{

		htmlFieldCode = $('alternField').innerHTML;

		$('alternField').innerHTML = '<input type="text" name="state" id="state" class="box3" size="20" maxlength="50">';

		stateIsText = true;

	}

}

