//----------------------------------------------------//
//----------------------------------------------------//
//--			GENERIC VARIABLES								--//
//----------------------------------------------------//
//----------------------------------------------------//
var String = String(document.location)
var passwordSyntax =	"<ul>"+
								"<li>at least one lower case letter</li>"+
								"<li>at least one upper case letter</li>"+
								"<li>at least one number</li>"+
								"<li>at least one special character<BR>"+
								"besides parentheses ( eg. !@#$%^&amp;*[] )</li>"+
								"<li>at least 8 characters, no more than 50</li>"+
								"<li>(eg TrustNo1!)</li>"+
								"</ul>"
								
var weakpasswordSyntax =	"<ul>"+
										"<li>at least 6 characters</li>"+								
										"</ul>"
if (passwordType != "STRONG"){	passwordSyntax = weakpasswordSyntax;}

//----------------------------------------------------//
//----------------------------------------------------//
//--			BEGIN :: LOGIN JAVASCRIPT					--//
//----------------------------------------------------//
//---------------------------------------------------//
function fillCustom(){	//fires onLoad for the CHANGES pages
	document.sendemail.HdrFrom.value = customDataArray[0]
	document.sendemail.HdrFromAddress.value = customDataArray[1]
	document.sendemail.HdrReplyTo.value = customDataArray[2]
	document.sendemail.address1.value = customDataArray[3]
	document.sendemail.address2.value = customDataArray[4]
	document.sendemail.address3.value = customDataArray[5]
	document.sendemail.address4.value = customDataArray[6]
	if (document.sendemail.securitylevel){document.sendemail.securitylevel.value = customDataArray[9]}	
	if (document.sendemail.loginpage){document.sendemail.loginpage.value = customDataArray[10]}	
	
	if (document.sendemail.redirecturl){document.sendemail.redirecturl.value = customDataArray[8]}	
	if (document.sendemail.finalredirecturl){document.sendemail.finalredirecturl.value = customDataArray[11]}					
	if (document.login){document.login.redirecturl.value = customDataArray[8]}
	if (document.signup){document.signup.redirecturl.value = customDataArray[8]}
	if (document.signup){document.signup.securitylevel.value = customDataArray[9]}
	if (document.signup){document.signup.ownergroup.value = customDataArray[12]}
	
	
	if(document.getElementById('welcome')){
		document.getElementById('welcome').style.display = 'none';
		document.getElementById('sendduplicate').style.display = 'none';
		document.getElementById('sendforgotten').style.display = 'none';
		document.getElementById('verification').style.display = 'none';
		document.getElementById('sent').style.display = 'none';
		document.getElementById('notsent').style.display = 'none';
		document.getElementById('sendemaildiv').style.display = 'none';
		document.getElementById('errorinactive').style.display = 'none';
		document.getElementById('errorinvalid').style.display = 'none';
		document.getElementById('passwordsyntax').style.display = 'none';
	}
	
	if (document.login){document.login.action = processingPage}
	if (document.signup){document.signup.action = processingPage}
	if (document.sendemail){document.sendemail.action = processingPage}	

	 //Verification login to activate account
	var emailprepopulate = QueryString('e');
	var activate = QueryString('a');	
	if (emailprepopulate && activate == 'true'){
		document.login.emailaddress.value=emailprepopulate;		
		document.login.form.value="initiallogin";
		document.getElementById('welcome').style.display = 'block';
		document.login.action = processingPage +"?a=true&e="+ emailprepopulate;
	}
}

function QueryString(Query) {	
	if (String && String!=""){
	Query+="=";
	var pos=String.indexOf(Query);
	if (pos!=-1){
		pos=pos+Query.length;
		var pos2=((String.indexOf("&", pos)!=-1)? String.indexOf("&", pos) : String.length);
		return(String.substring(pos, pos2));
	}
	} else return(false);
} 


function displayForgottenPassword(){
	fillCustom();
	if (document.login.emailaddress.value != ''){
		document.sendemail.emailaddress.value = document.login.emailaddress.value
	}
	definePopup('Forgot Your Password?','sendforgotten')
}

function displayPasswordSyntax(){
	fillCustom();
	definePopup('Password Syntax','passwordsyntax')
}

function showPopup(){
	var sendval = QueryString('send');	
	if (sendval){
		if (sendval == 'failedpasswordsend'){
			definePopup('Email Address Not Found!','notsent')
		}else{
			definePopup('Password Sent!','sent')
		}	
	}
	var successval = QueryString('success');
	if (successval){
		if (successval == 'exists'){		
			document.sendemail.emailaddress.value = QueryString('e');
			definePopup('Duplicate e-Mail!','sendduplicate')					
		}else{
			definePopup('Verification e-Mail Sent!','verification')				
		}			
	}	
	
	var errorval = QueryString('error');
	if (errorval){
		if (errorval == 'inactive'){	definePopup('Account not yet Activated!','errorinactive')}
		if (errorval == 'invalid'){document.getElementById('errorinvalid').style.display = 'block';}
	}	
}

function definePopup(title,field){
	popuptitle.innerHTML = title
	document.getElementById(field).style.display = 'block';
	if (field == 'sendduplicate' || field == 'sendforgotten'){
		document.getElementById('sendemaildiv').style.display = 'block';				
	}
	displaySent();
}

function displaySent(){
	if (document.getElementById('popup').style.display == "none"){
		document.getElementById('popup').style.display = "block";
	}else{
		document.getElementById('popup').style.display = "none";
	}
}

function clearMsg(){	
	document.getElementById('errorinvalid').style.display = 'none';
}


function goSubmit(){
	if (!ValidEmail(document.signup.emailaddress.value)){
		alert('Please enter a valid e-Mail address!')
		return false;
	}
	
	if (passwordType == "STRONG"){
		gocontinue = ValidatePassword();
	}else{
		gocontinue = ValidateWeakPassword();
	}
		
	if(gocontinue){
		if (VerifyPassword()){ document.signup.submit(); }else{ return false; }
	}else{		
		return false;
	}
}

function sendForgottenPassword(){
	if (document.sendemail.emailaddress.value != ''){
		document.sendemail.submit();
	}else{
		alert("Please enter your e-Mail address.")
		return false
	}
}


function VerifyPassword(){
	if (document.forms[0].password.value != ""){
		if (document.forms[0].password0.value != document.forms[0].password.value){
		  window.alert("Your password does not match the confirmation.\nPlease re-enter your new password.");
		  document.forms[0].password0.value = ""
		  document.forms[0].password.value = ""
		  return(false);
		}
		else{
		  return(true);
		}
	}
	else{
		window.alert("Your new password may not be blank");
		return(false);
	}
}

function ValidatePassword() {
	var x=1,s=''  
	var val = document.forms[0].password.value;
	var VArray = new Array(5)
	var DArray = new Array(5) 
	
	VArray[0] = "[a-z]+";
	VArray[1] = "[A-Z]+"	;
	VArray[2] = "[0-9]+";
	VArray[3] = "[^a-zA-Z_0-9]+";
	VArray[4] = ".{8,50}$";	
	VArray[5] = "\x20+";	
	DArray[0] = "-at least 1 lower case letter";
	DArray[1] = "-at least 1 upper case letter"	;
	DArray[2] = "-at least 1 number";
	DArray[3] = "-at least one special character besides parentheses or double quotes ( eg. !@'#$%^&*[] )"	
	DArray[4] = "-at least 8 characters, no more than 50.";
	DArray[5] = "-no spaces";
	
	for (var i = 0;i < 6;i++){
		var reg = eval("/"+ VArray[i] +"/")
		if (reg.test(val)) {
			if (i == 5){x=x-1;s = s + DArray[i] +'\r\n'}else{x=x+1}
			if (i == 3){
				if (val.indexOf('(') != -1 || val.indexOf(')') != -1 || val.indexOf('"') != -1){x=x-1;s = s + DArray[i] +'\r\n'}											}
		}else{
			if (i != 5){s = s + DArray[i] +'\r\n'}
		}		
	}
	s = 'Your password must adhere to the following pattern:\r\n\r\n'+ s +'\r\nClick the \'OK\' button below to re-enter your password.'	
	if (x < (6)){	alert(s);return false;}else{return true}	
	s=''	
}

function ValidateWeakPassword() {
	var val = document.forms[0].password.value;
	var s = 'Your password must adhere to the following pattern:\r\n\r\n-must be at least 6 characters long\r\n\r\nClick the \'OK\' button below to re-enter your password.'		
	if (val.length < 6){alert(s);return false;}else{return true}
}

function checkLoginFields(){
	var eCheck = document.login.emailaddress.value
	var pCheck = document.login.password.value
	if (eCheck == '' && pCheck == ''){alert('Please enter an emailaddress and password!');return false;	}
	if (eCheck == '' && pCheck != ''){alert('Please enter an emailaddress!');return false;}
	if (eCheck != '' && pCheck == ''){alert('Please enter a password!');return false;}
	if (eCheck != '' && pCheck != ''){return true;	}
}

function ValidEmail(strIn) {
	var reEmail = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/;
	return(reEmail.test(strIn + ""));
}

function placeEmail(){
	var eVal = QueryString('e')
	if (eVal){
		document.sendemail.emailaddress.value = eVal
	}
}
//----------------------------------------------------//
//----------------------------------------------------//
//--			END :: LOGIN JAVASCRIPT						--//
//----------------------------------------------------//
//----------------------------------------------------//



//----------------------------------------------------//
//----------------------------------------------------//
//--			BEGIN :: CHANGES JAVASCRIPT				--//
//----------------------------------------------------//
//----------------------------------------------------//
function fillCustomChange(){	//fires onLoad for the CHANGES pages
	document.passwordform.action = processingPage;
	document.emailform.action = processingPage;
	if (document.getElementById('changeemailaddress')){document.getElementById('changeemailaddress').style.display = 'none';}
	if (document.getElementById('changepassword')){document.getElementById('changepassword').style.display = 'none';}
	if (document.getElementById('pUpdated')){document.getElementById('pUpdated').style.display = 'none';}
	if (document.getElementById('pNoupdate')){document.getElementById('pNoupdate').style.display = 'none';}
	if (document.getElementById('pInvalid')){document.getElementById('pInvalid').style.display = 'none';}
	if (document.getElementById('eUpdated')){document.getElementById('eUpdated').style.display = 'none';}
	if (document.getElementById('eNoupdate')){document.getElementById('eNoupdate').style.display = 'none';}
	if (document.getElementById('eInvalid')){document.getElementById('eInvalid').style.display = 'none';}
	if (document.getElementById('eNoMatch')){document.getElementById('eNoMatch').style.display = 'none';}
	if (document.getElementById('error')){document.getElementById('error').style.display = 'none';}
	if (document.getElementById('errorbuttons')){document.getElementById('errorbuttons').style.display = 'none';}
									
	var pVal = QueryString('p')
	if (pVal){
		if (pVal != 'updated'){document.getElementById('errorbuttons').style.display = 'block';}
		switch(pVal){	
			case "updated":
				popuptitle.innerHTML = "Password Updated!"
				document.getElementById('pUpdated').style.display = 'block';
			break
			case "noupdate":
				popuptitle.innerHTML = "Technical Problem!"
				document.getElementById('pNoupdate').style.display = 'block';
			break
			case "invalid":
				popuptitle.innerHTML = "Invalid Password!"
				document.getElementById('pInvalid').style.display = 'block';
			break		
			default:
				document.getElementById('errorbuttons').style.display = 'none';
				document.passwordform.emailaddress.value = pVal;
				popuptitle.innerHTML = "Change Password for: "+ pVal
				document.getElementById('changepassword').style.display = 'block';
			break
		}
	}else{		
		var pError =true;		
	}

	var eVal = QueryString('e')
	if (eVal){
		if (eVal != 'updated'){document.getElementById('errorbuttons').style.display = 'block';}
		switch(eVal){	
			case "nomatch":
				popuptitle.innerHTML = "Address not found..."
				document.getElementById('eNoMatch').style.display = 'block';
			break
			case "updated":
				popuptitle.innerHTML = "e-Mail Updated!"								
				document.getElementById('eUpdated').style.display = 'block';
				refreshOpener( QueryString('o'), QueryString('n'));
			break
			case "noupdate":
				popuptitle.innerHTML = "Address already in use..."
				document.getElementById('eNoupdate').style.display = 'block';
			break
			case "invalid":
				popuptitle.innerHTML = "Invalid Password!"
				document.getElementById('eInvalid').style.display = 'block';
			break		
			default:
				document.getElementById('errorbuttons').style.display = 'none';
				document.emailform.currentemail.value = eVal;
				popuptitle.innerHTML = "Change e-Mail Address for: "+ eVal
				document.getElementById('changeemailaddress').style.display = 'block';
			break
		}
	}else{		
		var eError =true;		
	}

	if ((pVal ==  null && eVal == null) || (eError == true && pError == true )){throwError();}
}

function throwError(){
	popuptitle.innerHTML = "Error..."
	document.getElementById('error').style.display = 'block';
}

//----------------------------------------------------//
//--  For the Password Change Page						--//
//----------------------------------------------------//
function goSubmitChange(){
	if (passwordType == "STRONG"){
		if(ValidatePassword()){
			if (VerifyPasswordChange()){return true;}else{return false;}}else{return false;
		}
	}else{
		if(ValidateWeakPassword()){
			if (VerifyPasswordChange()){return true;}else{return false;}}else{return false;
		}
	}
}

function VerifyPasswordChange(){
	if (document.passwordform.password.value != ""){
		if (document.passwordform.password.value != document.passwordform.ecom_password2.value){
		  window.alert("Your password does not match the confirmation.\nPlease re-enter your new password.");
		  document.passwordform.password.value = ""
		  document.passwordform.ecom_password2.value = ""
		  return(false);
		}
		else{
		  return(true);
		}
	}
	else{
		window.alert("Your new password may not be blank");
		return(false);
	}
}

//----------------------------------------------------//
//--  For the e-Mail Change Page							--//
//----------------------------------------------------//
function ConfirmAddress(){
	if (!ValidEmail(document.emailform.email1.value) || !ValidEmail(document.emailform.email2.value)){		
		alert("You have entered an invalid or blank email address!")		
		return false
	}
	
	if (document.emailform.email1.value != '' && document.emailform.email2.value != ''){
		if (document.emailform.email1.value != document.emailform.email2.value){
			alert("The e-Mail Addresses do not match.")			
			return false
		}else{
			if (document.emailform.currpassword.value != ''){				
				//document.emailform.submit();
				return true;
			}else{
				alert("Please enter your password.")		
				return false;
			}
		}
	}
}

function refreshOpener(oldval,newval){
	if (window.opener){
 		var r = window.opener.location.href.toString();
 		results = r.replace(oldval, newval)	
 		opener.document.forms[0].action = results;
 		opener.document.forms[0].submit();
 		return true;
	}
}
//----------------------------------------------------//
//----------------------------------------------------//
//--			END :: CHANGES JAVASCRIPT					--//
//----------------------------------------------------//
//----------------------------------------------------//

//----------------------------------------------------//
//----------------------------------------------------//
//--	Copyright 2003,2004 Cierant Corporation			--//
//----------------------------------------------------//
//----------------------------------------------------//