//function stopError() {  return true; }

String.prototype.toCapitalCase = function() {
	var re = /\s/;
	var words = this.split(re);
	re = /(\S)(\S+)/;
	for (i = words.length - 1; i >= 0; i--) {
	re.exec(words[i]);
	words[i] = RegExp.$1.toUpperCase()
	+ RegExp.$2.toLowerCase();
	}
return words.join(' ');
} 

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function number(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;
else if ((("0123456789").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function letter(e)
{
var key;
var keychar;
if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27))
   return true;
else if ((("abcdefghijklmnopqrstuvwxyz ").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function openX(mypage,h,w) {
if(screen.width){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
}else{
	winl = 0;wint =0;}
	if (winl < 0) winl = 0;
		if (wint < 0) wint = 0;
			var settings = 'height=' + h + ',';
			settings += 'width=' + w + ',';
			settings += 'top=' + wint + ',';
			settings += 'left=' + winl + ',';
			settings += 'status=' + 0 + ',';
			settings += 'toolbar=' + 0 + ',';
			settings += 'location=' + 0 + ',';
			settings += 'menubar=' + 1 + ',';
			settings += 'directories=' + 0 + ',';
			settings += 'resizable=' + 0 + ',';
			settings += 'scrollbars=' + 1 + ',';
	win = window.open(mypage,'Directions',settings);
}

function focusField(x,y,z){
	var z=document.getElementById(z);
	if(z != null){
		z.focus();
	}
}

function upperCaseField()
{
	document.loginAccount.txtFirstName.value = document.loginAccount.txtFirstName.value.toCapitalCase();
	document.loginAccount.txtLastName.value = document.loginAccount.txtLastName.value.toCapitalCase();
}

var bCheckNumbers = true;
var bCheckUpperCase = true;
var bCheckLowerCase = true;
var bCheckPunctuation = true;
var nPasswordLifetime = 365;
//<!--br />onkeyup="runPassword(this.value, 'myPassword');"<div style="width: 100px;"><div id="mypassword_text" style="font-size: 10px;"></div><div id="mypassword_bar" style="font-size: 1px; height: 2px; width: 0px; border: 1px solid white;"></div-->
function checkPassword(strPassword)
{
	nCombinations = 0;
	if (bCheckNumbers)
	{
		strCheck = "0123456789";
		if (doesContain(strPassword, strCheck) > 0) { nCombinations += strCheck.length; }
	}
	if (bCheckUpperCase)
	{
		strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		if (doesContain(strPassword, strCheck) > 0) { nCombinations += strCheck.length; }
	}
	if (bCheckLowerCase)
	{
		strCheck = "abcdefghijklmnopqrstuvwxyz";
		if (doesContain(strPassword, strCheck) > 0) { nCombinations += strCheck.length; }
	}
	if (bCheckPunctuation)
	{
		strCheck = ";:-_=+\|//?^&!.@$£#*()%~<>{}[]";
		if (doesContain(strPassword, strCheck) > 0) { nCombinations += strCheck.length; }
	}	
    var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;
	var nPerc = nDays / nPasswordLifetime;
	return nPerc;
} 

function runPassword(strPassword, strFieldID) 
{
	nPerc = checkPassword(strPassword);
   	var ctlBar = document.getElementById(strFieldID + "_bar"); 
   	var ctlText = document.getElementById(strFieldID + "_text");
   	if (!ctlBar || !ctlText){ return; }
    	
    var nRound = Math.round(nPerc * 100);
	if (nRound < (strPassword.length * 5)) 
	{ 
		nRound += strPassword.length * 5; 
	}
	if (nRound > 100)
		nRound = 100;
    	ctlBar.style.width = nRound + "%";
 
 	if (nRound > 95)
 	{
 		strText = "Very Secure";
 		strColor = "#3bce08";
 	}
 	else if (nRound > 75)
 	{
 		strText = "Secure";
 		strColor = "orange";
	}
 	else if (nRound > 50)
 	{
 		strText = "Mediocre";
 		strColor = "#ffd801";
 	}
 	else
 	{
 		strColor = "red";
 		strText = "Insecure";
 	}
	ctlBar.style.backgroundColor = strColor;
	ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
}
 
function doesContain(strPassword, strCheck)
 {
    nCount = 0;  
	for (i = 0; i < strPassword.length; i++) 
	{
		if (strCheck.indexOf(strPassword.charAt(i)) > -1) { nCount++; } 
	} 
	return nCount; 
} 

var patternReg=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|COM|net|NET|org|ORG|edu|EDU|int|INT|mil|MIL|gov|GOV|arpa|biz|aero|name|coop|info|pro|museum|tv|ca))$/;

var jsEmailCreate, jsEmailUpdate
function checkEmail()
{
    if(patternReg.test(document.loginAccount.txtEmail.value)){ 		
		document.getElementById('errEmail').childNodes[0].nodeValue = " ";
		ajaxFunction("email");		
	} else {
		document.getElementById('errEmail').childNodes[0].nodeValue = "Please enter a valid email address.";		
	}
}

function checkLogin(){
	if((document.loginAccount.txtEmail.value != "") && (document.loginAccount.txtUserPass.value != "")){
	  	ajaxFunction("checkLogin");
		return false;
	}else{	
		document.getElementById('errLogin').childNodes[0].nodeValue = "Double check Login";
		return false;
	}	
}

function checkEmailCreate(){
	 if(patternReg.test(document.manageAccount.txtEmail.value)){ 		
	  	ajaxFunction("createEmail");
		return false;
	}else{	
		document.getElementById('eMailMsg').childNodes[0].nodeValue = "Please enter a valid email address.";
		jsEmailCreate = "Email Address\n";	
		return false;
	}	
}

function checkEmailGetLogin(){
	 if(patternReg.test(document.loginAccount.txtEmail.value)){ 		
	  	ajaxFunction("getLogin");
		return false;
	}else{	
		
		document.getElementById('errEmail').childNodes[0].nodeValue = "Please enter a valid email address.";
		jsEmailCreate = "Email Address\n";		
		return false;	
	}	
}

function checkEmailOptOut(){
	 if(patternReg.test(document.loginAccount.txtEmail.value)){ 		
	  	ajaxFunction("OptOut");
	}else{			
		document.getElementById('errEmail').childNodes[0].nodeValue = "Please enter a valid email address.";		
		return false;
	}	
}

var jsEmailUpdate;
function checkEmailUpdate()
{
	if(document.manageAccount.sessionEmailID.value != document.manageAccount.txtEmail.value){
	    if(patternReg.test(document.manageAccount.txtEmail.value)){ 
			ajaxFunction("updateEmail");				
		} else {
			document.getElementById('eMailMsg').childNodes[0].nodeValue = "Please enter a valid email address.";	
			jsEmailUpdate = "Email Address\n";	
		}
	}else{
		jsEmailUpdate = "";	
		document.getElementById('eMailMsg').childNodes[0].nodeValue = "";	
	}
}

function ajaxFunction(which)
{
var xmlHttp = null;
try
  {  // Firefox, Opera 8.0+, Safari  
  xmlHttp=new XMLHttpRequest();  }
catch (e)
  {  // Internet Explorer  
  try
    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }
  catch (e)
   {    try
      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      }
    catch (e)
      {      alert("Your browser does not support AJAX!");      return false;      }    }  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {			
	  if(xmlHttp.status==200){
	  		var s=xmlHttp.responseText;
			switch(which)
			{
			case "email":
			if(s != "undefined"){
				document.getElementById('errEmail').childNodes[0].nodeValue = s;
				}
				if(s != ""){
				}
			break;    
			case "checkLogin":	
				if(s != "Login is incorrect."){ 
					hideShowPreloader('show');
					document.getElementById('btnSubmitButtonID').value = " Checking Login ";
					document.getElementById('btnSubmitButtonID').disabled = true;
					document.loginAccount.submit(); 		
					return true;
				}else{
				document.getElementById('errLogin').childNodes[0].nodeValue = s;
					return false;
				}			
				document.getElementById('errLogin').childNodes[0].nodeValue = s;
			break;    
			case "getLogin":	
				if(s == ""){ 
				hideShowPreloader('show');
				document.getElementById('btnSubmitButtonID').value = " Sending Login ";
				document.getElementById('btnSubmitButtonID').disabled = true;
				document.loginAccount.submit(); 				
				}else{
				return false;
				}
				document.getElementById('errLogin').childNodes[0].nodeValue = "";
			break;   
			case "OptOut":	
				if(s == ""){ 
				hideShowPreloader('show');
				document.getElementById('btnSubmitButtonID').value = " Unsubscribing Account ";
				document.getElementById('btnSubmitButtonID').disabled = true;		
				return true;	
				}else{
				return false;
				}
				document.getElementById('errLogin').childNodes[0].nodeValue = "";
			break;  
			case "updateEmail":	
				if(s == "Unique"){				
					document.getElementById('eMailMsg').childNodes[0].nodeValue = "";
					jsEmailUpdate = "";
				}else{
					var currentData = document.manageAccount.txtEmail.value;

					if(document.manageAccount.txtEmail.value != document.manageAccount.sessionEmailID.value){
						document.getElementById('eMailMsg').childNodes[0].nodeValue = currentData + " is already registered.";
						document.manageAccount.txtEmail.value = document.manageAccount.sessionEmailID.value;
						jsEmailUpdate = "\nEmail Address";
						if(currentData != "undefined"){
							alert(currentData + " is already registered.");
						}
					}else{
						jsEmailUpdate = "";	
						document.getElementById('eMailMsg').childNodes[0].nodeValue = "";	
					}
				}						
			break;   	
			case "createEmail":
				if(s == "Not Found"){
					document.getElementById('eMailMsg').childNodes[0].nodeValue = "";	
					jsEmailCreate = "";	
				}else{
				ref = document.manageAccount.txtEmail.value
				//ref = window.location.search.substring(1);
				if(ref != ""){
				refUrl = document.manageAccount.refUrlValue.value;
				//alert("http://" + refUrl + "/pages_referfriend.asp?r=" + ref);
				window.location = "http://" + refUrl + "/pages_referfriend.asp?r=" + ref;
				}
				//alert(ref);
					document.getElementById('eMailMsg').childNodes[0].nodeValue = document.manageAccount.txtEmail.value + " is already registered.";	
					document.manageAccount.txtEmail.value = ""
					document.manageAccount.txtEmail.focus();				
				}
			break;    		  
			default:
			  //default code
			}//end switch			
			
		}//request status
      }//ready state
    }//onStateChange
	
		var randomNumber = Math.random();
		switch(which)
		{
		case "email":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.loginAccount.txtEmail.value + "&stage=lookUpEmail&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null);    
		break;    
		case "checkLogin":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.loginAccount.txtEmail.value + "&password=" + document.loginAccount.txtUserPass.value + "&stage=loginAccount&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null); 
		break;  
		case "getLogin":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.loginAccount.txtEmail.value + "&stage=getLogin&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null);    
		break;
		case "OptOut":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.loginAccount.txtEmail.value + "&stage=getLogin&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null);    
		break;
		case "updateEmail":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.manageAccount.txtEmail.value + "&stage=updateEmail&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null); 
		break; 
		case "createEmail":
			  selURL = "myAccount_forgotPassDB.asp?email="+ document.manageAccount.txtEmail.value + "&stage=createEmail&rand=" + randomNumber;  
			  xmlHttp.open("GET",selURL,true);
			  xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
			  xmlHttp.send(null);    
		break;		
		
		default:
		  //default code
		}//end switch	
			
}  //end ajax

var jsStore;
function checkStore(x){
	var storeDrop = document.getElementById("selStoreID");
	//var storeImage = document.getElementById("storeImage");		
	//myStore = x + storeDrop.options[storeDrop.selectedIndex].value + ".jpg";	
	//storeImage.src = myStore;
	
	if(storeDrop.options[storeDrop.selectedIndex].value == 0){			
		document.getElementById('storeNumDiv').childNodes[0].nodeValue = "Required" + "   ";			
		jsStore = "Select a Store\n";
	} else {
		document.getElementById('storeNumDiv').childNodes[0].nodeValue = "Store #" + document.getElementById("selStoreID").value + "  ";
		jsStore = "";		
	}
}

var jsFieldLength, jsUserPass, jsFirstName, jsLastName, jsZipCode, errorMessage
function checkFieldLength(myForm, myField, myLength, myErrMsg)
{ 
	valueTest = eval("document." + myForm + "." + myField + "." + "value;");
	if(valueTest.length >= myLength){
		document.getElementById(myErrMsg).childNodes[0].nodeValue = "";
			switch(myField)
			{
			case "txtUserPassID":
			  jsUserPass = "";
			  break;    
			case "txtFirstNameID":
			  jsFirstName = "";
			  break;
			case "txtLastNameID":
			  jsLastName = "";
			  break;
			case "txtZipCodeID":
			  jsZipCode = "";
			  break;
			default:
			  	//alert("No Match");
			}		
	}else{	
		document.getElementById(myErrMsg).childNodes[0].nodeValue = "You need at least " + myLength + " characters.";		
			switch(myField)
			{
			case "txtUserPassID":
			  jsUserPass = "Password\n";
			  break;    
			case "txtFirstNameID":
			  jsFirstName = "First Name\n";
			  break;
			case "txtLastNameID":
			  jsLastName = "Last Name\n";
			  break;
			case "txtZipCodeID":
			  jsZipCode = "Zip Code\n";
			  break;
			default:
			 	//alert("No Match");
			}
	}	
}

function checkAccountUpdate(x){

	if(jsEmailUpdate = "undefined"){ checkEmailUpdate(); }
	checkFieldLength('manageAccount','txtUserPassID', 5,'myPasswordMsg');
	checkFieldLength('manageAccount','txtFirstNameID', 2,'fNameMsg');
	checkFieldLength('manageAccount','txtLastNameID', 2,'lNameMsg');
	checkFieldLength('manageAccount','txtZipCodeID', 5,'zipCodeMsg');
	checkStore();
	var validUpdate;
	validUpdate = jsEmailUpdate + jsUserPass + jsFirstName + jsLastName + jsZipCode + jsStore ;

	var errorMsgHeader = "Please double check\nthe following field(s):\n-------------------------\n";
	if(validUpdate == ""){
		if(x == "yes"){
			return true;
		}else{
			return false;
		}	
	}else{
		if(validUpdate != "undefined"){
			alert(errorMsgHeader + validUpdate);
		}
		return false;
	}
}

function checkAccountCreate(x){
	checkEmailCreate();
	checkFieldLength('manageAccount','txtUserPassID', 5,'myPasswordMsg');
	checkFieldLength('manageAccount','txtFirstNameID', 2,'fNameMsg');
	checkFieldLength('manageAccount','txtLastNameID', 2,'lNameMsg');
	checkFieldLength('manageAccount','txtZipCodeID', 5,'zipCodeMsg');
	checkStore();	
	validUpdate = jsEmailCreate + jsUserPass + jsFirstName + jsLastName + jsZipCode + jsStore ;
	
	var errorMsgHeader = "Please double check\nthe following field(s):\n-------------------------\n";
	if(validUpdate == ""){
		if(x == "yes"){
			return true;	
		}else{
			return false;
		}
	}else{
		if(validUpdate != "undefined"){
			alert(errorMsgHeader + validUpdate);
		}
	return false;
	}
}
var flashPreloader;
function hideShowPreloader(which){	
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);

	if(which == "hide"){
		flashPreloader = document.getElementById('preloading').innerHTML;
		document.getElementById('preloading').childNodes[0].nodeValue = "";
		document.getElementById('preloading').innerHTML="";
	}else{
		if(browser == "Netscape"){
			document.getElementById('preloading').innerHTML = "<img src='images/preloadCircle.gif' />";
		}else{
			document.getElementById('preloading').innerHTML = flashPreloader;
		}

	}
}