var captcha = new Array(); 
captcha[11] = '67326';
captcha[12] = '59183';
captcha[13] = '57485';
captcha[14] = '37163';
captcha[15] = '21431';
captcha[16] = '83164';
captcha[17] = '25461';
captcha[18] = '19638';
captcha[19] = '22778';
captcha[20] = '19837';
captcha[21] = '86574';
captcha[22] = '39563';
captcha[23] = '28547';
captcha[24] = '85653';
captcha[25] = '44825';
captcha[26] = '82349';
captcha[27] = '91754';
captcha[28] = '86448';
captcha[29] = '77519';
captcha[30] = '74968';

function CheckInput(TheForm) {
	if (TheForm.NewAgentID.value == '') {
		TheForm.NewAgentID.focus();
		alert('Your desired Agent ID is required');
		return false;
	} else if (TheForm.Password.value == '') {
		TheForm.Password.focus();
		alert('Your Password is required');
		return false;
	} else if (TheForm.CompanyName.value == '' && (TheForm.AgentType.value == 'agent' || TheForm.AgentType.value == 'var')) {
		TheForm.CompanyName.focus();
		alert('Your Company Name is required');
		return false;
	} else if (TheForm.FirstName.value == '') {
		TheForm.FirstName.focus();
		alert('Your First Name is required');
		return false;
	} else if (TheForm.LastName.value == '') {
		TheForm.LastName.focus();
		alert('Your Last Name is required');
		return false;
	} else if (TheForm.Email.value == '') {
		TheForm.Email.focus();
		alert('Your Email Address is required');
		return false;
	} else if (!(/^.+@.+\..{2,4}$/.test(TheForm.Email.value))) {
		TheForm.Email.focus();
		alert('The Email Address entered is not in the proper format.');
		return false;
	} else if (TheForm.Address1.value == '') {
		TheForm.Address1.focus();
		alert('Your Address is required');
		return false;
	} else if (TheForm.City.value == '') {
		TheForm.City.focus();
		alert('Your City is required');
		return false;
	} else if (TheForm.State.value == '' && TheForm.CountryCode.value == 'US') {
		TheForm.State.focus();
		alert('Your State / Region is required');
		return false;
	} else if (TheForm.Province.value == '' && TheForm.CountryCode.value == 'CA') {
		TheForm.Province.focus();
		alert('Your Province is required');
		return false;
	} else if (TheForm.Region.value == '' && TheForm.CountryCode.value != 'CA' && TheForm.CountryCode.value != 'US') {
		TheForm.Region.focus();
		alert('Your Region is required');
		return false;
	} else if (TheForm.PostalCode.value == '') {
		TheForm.PostalCode.focus();
		alert('Your Zip Code is required');
		return false;
	} else if ((TheForm.npa.value + TheForm.nxx.value + TheForm.xxxx.value).replace(/[^0-9]/g, '').length != 10) {
		TheForm.npa.focus();
		alert('The Phone number you provided does not appear to be valid.');
		return false;
	} else if (TheForm.AgentType.value == 'affiliate' && !isChecked(TheForm.ExperienceLevel)) {
		TheForm.ExperienceLevel[0].focus();
		alert('Your Experience Level is required');
		return false;
	} else if (TheForm.AgentType.value == 'affiliate' && TheForm.ServiceAgreement.checked == false) {
		TheForm.ServiceAgreement.focus();
		alert('Please review the service agreement and check the box if you agree.');
		return false;
	} else if (TheForm.TextVerify.value == '' || TheForm.TextVerify.value == 'Enter text here') {
		TheForm.TextVerify.focus();
		alert('The Security Code is required');
		return false;
	} else if(trim(TheForm.TextVerify.value) != captcha[TheForm.SecurityIndex.value]) {
		TheForm.TextVerify.focus();
		alert('The security code is not correct.');
		return false;
	} else if(!checkForm()) {
		alert('Check User Name and Password');
		return false;
	}

	return true;
}

function CheckInputSignupStep2(TheForm){
	if (TheForm.ServiceAgreement.checked == false) {
		TheForm.ServiceAgreement.focus();
		alert('Please review the service agreement and check the box if you agree.');
		return false;
	}
	return true;
}

function isChecked(butts) {
    for(i=0; i<butts.length; i++) if(butts[i].checked) return true;
    return false;
}

function ShowStates(CountryCode) {
	if (CountryCode == "US") { // Show the USA
		document.getElementById('states_us').style.display = "";
		document.getElementById('states_ca').style.display = "none";
		document.getElementById('states_other').style.display = "none";
	} else if (CountryCode == "CA") {
		document.getElementById('states_us').style.display = "none";
		document.getElementById('states_ca').style.display = "";
		document.getElementById('states_other').style.display = "none";
	} else { // other
		document.getElementById('states_us').style.display = "none";
		document.getElementById('states_ca').style.display = "none";
		document.getElementById('states_other').style.display = "";
	}
}

function validate(f) {
	var hasError = false;
	var error = 'The following problems were encountered:\n\n';
	
	if(trim(f.AgentFirstName.value) == '' || trim(f.AgentFirstName.value) == 'First Name') {
		hasError = true;
		error += 'Please provide your First Name.\n';
	}
	if(trim(f.AgentLastName.value) == '' || trim(f.AgentLastName.value) == 'Last Name') {
		hasError = true;
		error += 'Please provide your Last Name.\n';
	}
	
	if((f.npa.value + f.nxx.value + f.xxxx.value).replace(/[^0-9]/g, '').length != 10) {
		hasError = true;
		error += 'The Phone number you provided does not appear to be valid.\n';
	}
		
	if(trim(f.AgentEmail.value) == '' || trim(f.AgentEmail.value) == 'Enter your email here') {
		hasError = true;
		error += 'Please provide your Email.\n';
	} else if(!(/^.+@.+\..{2,4}$/.test(f.AgentEmail.value))) {
		hasError = true;
		error += 'The Email Address you provided is not valid. Please check your value and try again.\n';
	}
	
	var thisAgentType = f.AgentType.options[f.AgentType.selectedIndex].value;

	if(hasError) {
		alert(error);
		return !hasError;
	} else {
		displayOverlay('checkCaptcha',thisAgentType,f.AgentFirstName.value,f.AgentLastName.value,f.npa.value,f.nxx.value,f.xxxx.value,f.extension.value,f.AgentEmail.value);
		return false;
	}
}

function validateCaptcha(f) {
	var hasError = false;
	var error = 'The following problems were encountered:\n\n';
	var thisIndex = f.SecurityIndex.value;
	
	
	if(trim(f.TextVerify.value) == '' || trim(f.TextVerify.value) == 'Enter text here') {
		hasError = true;
		error += 'Please enter security text.\n';
	} else if(trim(f.TextVerify.value) != captcha[thisIndex]) {
		hasError = true;
		error += 'The security code is not correct.\n';
	}
	
	if(hasError) alert(error);
	return !hasError;
}

function trim(s) {
	return s.replace(/^\s*|\s*$/g, '');
}	

var connection = createXMLHttpRequest();
var overlayIsLoaded = true;
var xmlHttp;
var loadingImage = '<div style="margin:15px 0;width:160px;height:60px;background:#fff;text-align:center;vertical-align:middle;">Loading....<br><br><img src="../images/ajax-loader-sr.gif" width="128" height="15" border="0" style="vertical-align:middle;" alt="loading information..."></div>';

function showWaitScreen() {
	var wait = document.getElementById('pleaseWaitScreen');
	wait.style.top = document.body.scrollTop;
	wait.style.display = 'inline';
}

function displayOverlay(id,AgentType,AgentFirstName,AgentLastName,npa,nxx,xxxx,extension,AgentEmail) {		
	var overlay = document.getElementById('overlay');
	var overlayContent = document.getElementById('overlay_content');
	var overlayActions = document.getElementById('overlay_actions');		
	var overlayBackdrop = document.getElementById('overlay_backdrop');
	var hide = (overlay.style.display == 'block' && overlay.contentID == id); // If we've called this function for an overlay that is already shown, we want to hide the overlay.
	var html = '';		
	
	// Hide browser's main scrollbars when overlay is visible or show them when overlay is closed.
	document.body.style.overflow = hide ? 'auto' : 'hidden';

	// If overlay is about to be shown ...
	if (!hide) {

		// Indicate which overlay is currently visible.
		overlay.contentID = id;

		// Populate content and action areas for a specific id.
		switch (id) {
			
			case 'cancel':
				overlayContent.innerHTML = loadingImage;
				xmlHttp = GetXmlHttpObject();
				if (xmlHttp == null){
					alert ("Browser does not support HTTP Request");
					return false;
				}
				var url = "index.html";
				var params = "";
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange = function(){
					if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){ 
						overlayContent.innerHTML = xmlHttp.responseText;
					} 	
				}
				xmlHttp.send(params);
				break;								

			case 'checkCaptcha':
				
				//ajax begin
				overlayContent.innerHTML = loadingImage;
				xmlHttp = GetXmlHttpObject();
				if (xmlHttp == null){
					alert ("Browser does not support HTTP Request");
					return false;
				}
				var url = "captcha_check.html";
				var params = "AgentType=" + AgentType + "&AgentFirstName=" + AgentFirstName + "&AgentLastName=" + AgentLastName + "&npa=" + npa + "&nxx=" + nxx + "&xxxx=" + xxxx + "&extension=" + extension + "&AgentEmail=" + AgentEmail;
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange = function(){
					if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){ 
						overlayContent.innerHTML = xmlHttp.responseText;
					} 	
				}
				xmlHttp.send(params);
				//ajax end
				
				break;
		}	
		overlayBackdrop.style.top = Math.max(document.body.scrollTop,document.documentElement.scrollTop) + "px";
		overlay.style.top = Math.max(document.body.scrollTop, document.documentElement.scrollTop) + "px";
		overlay.style.overflow = 'auto';		
	}
	// Hide or show overlay screens.
	overlayBackdrop.style.display = hide ? 'none' : 'block';
	overlay.style.display = hide ? 'none' : 'block';
	sizeOverlayContent(overlayContent);
}	

function GetXmlHttpObject(){
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
} 

function sizeOverlayContent(oc) {
	var oc = typeof(oc) == 'undefined' ? document.getElementById('overlay_content') : oc;
	var windowHeight = getWindowSize()[1];
	// If the overlay is longer than the window height, shrink it to allow scrolling of content area.
	oc.style.height = (oc.scrollHeight + 110 > windowHeight) ? windowHeight - 110 : 'auto';		
}

function overlayItemHover(item, isOver) {		
	var el = document.forms.overlay_form.elements[item.getAttribute('radioName')];
	if (el.length && !el[item.getAttribute('radioIndex')].checked) item.className = 'overlayItem_' + (isOver ? 'hover' : 'off');
}

function overlayItemClick(item) {		
	var el = document.forms.overlay_form.elements[item.getAttribute('radioName')];
	var clickedIndex = item.getAttribute('radioIndex');
	if (el.length) {
		for (var i = 0; i < el.length; i++) {
			document.getElementById('overlayItem_' + i).className = 'overlayItem_' + (i == clickedIndex ? 'on' : 'off');
		}
		el[clickedIndex].checked = true;
	}
}

function createXMLHttpRequest() {
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	return null;
}

function addEvent(e, func) {
	var existingEvent = window[e];
	if (typeof window[e] != 'function') {
		window[e] = func;
	} else {
		window[e] = function() {
			if (existingEvent) existingEvent(); 
		func();
		}
	}
}

function getWindowSize() {
	var w = 0, h = 0;

	if(typeof(window.innerWidth) == 'number') {
		// Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
		// IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if(document.body && document.body.clientHeight) {
		// IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	return [w,h];
}

function getCheckedRadioIndex(el) {
	var checkedIndex = -1;
	for(var i = 0; i < el.length; i++) if(el[i].checked) checkedIndex = i;
	return checkedIndex;
}

function getCheckedRadioValue(el) {
	return el.length ? el[getCheckedRadioIndex(el)].value : el.value;
}

function sortAlphaNumeric(a, b) {

	var intOne = parseInt(a);
	var intTwo = parseInt(b);

	// both are not numbers
	if (isNaN(intOne) && isNaN(intTwo)) {
		if (a > b) return 1;
		else if (a < b) return -1;
		else return 0;
	}
	// item one is not number - item two is number
	else if (isNaN(intOne)) return 1;
	// item one is a number - item two is not number
	else if (isNaN(intTwo)) return -1;
	// both are numbers
	else {
		if (intOne > intTwo) return 1;
		else if (intOne < intTwo) return -1;
		else return 0;
	}

}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); };

String.prototype.listGetAt = function(index, delimiter) {
	delimiter = typeof(delimiter) == 'undefined' ? ',' : delimiter;
	return this.split(delimiter)[index];
}

Array.prototype.indexOf = function(obj) {
	for(var i = 0; i < this.length; i++) {
		if(this[i] == obj) return i;
	}
	return -1;
}

function confirmClick(ConfirmationMessage) {
	if(!confirm(ConfirmationMessage)) return false;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

