/*
	Functions removed from registrationform.asp page
*/

function popUp(url)
{
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0, status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
	self.name = "mainWin"; 
}


//******************************************************************
// checkpassword() updated to check if username is same as password
// John Ashmore 10/07/2003
//******************************************************************

/*
function checkpassword()
{	
	var usename = document.frmRegistration.txtUsername.value;
	var passwd = document.frmRegistration.txtPassword.value;
	
	usename = usename.toLowerCase();
   	passwd = passwd.toLowerCase();
	
	
	if (usename == passwd && usename != "")
	{
		alert("Please enter a password that is different to your username.");	
		return false;
	}
	
	else if (document.frmRegistration.txtPassword.value==document.frmRegistration.txtPassword2.value) 
		return true;
	
	else 
	{
		alert("The passwords do not match.");	
		return false;
	}		
}
*/


function checkcomplete()
{
	var bresult=true;
	var smessage="";

	/*if (document.frmRegistration.txtTitle.value + "" == "")
		{
		smessage="Title";
		bresult=false;
		}*/
	if (document.frmRegistration.txtForename.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Forename"
		else smessage="Forename";
		bresult=false;
		}
	if (document.frmRegistration.txtSurname.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Surname"
		else smessage="Surname";
		bresult=false;
		}			
	if (document.frmRegistration.txtCompanyName.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Company Name"
		else smessage="Company Name";
		bresult=false;
		}			
	if (document.frmRegistration.txtAddress1.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Address Line 1"
		else smessage="Address Line 1";
		bresult=false;
		}			
	if (document.frmRegistration.txtPostcode.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Postcode"
		else smessage="Postcode";
		bresult=false;
		}
	if (document.frmRegistration.cboBusinessSector.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Business Type"
		else smessage="Business Type";
		bresult=false;
		}
		
	if (document.frmRegistration.txtTelephoneNo.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Telephone Number"
		else smessage="Telephone Number";
		bresult=false;
		}			
	if (document.frmRegistration.txtEmail.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", E-mail"
		else smessage="E-mail";
		bresult=false;
		}
	
	if (document.frmRegistration.cboBatch.value == 0)
		{
		if (smessage + "" != "") smessage = smessage + ", Batch Customer (yes or no)"
		else smessage="Batch Customer (yes or no)";
		bresult=false;
		}
				
	if (document.frmRegistration.txtPassword.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Password"
		else smessage="Password";
		bresult=false;
		}
	if (document.frmRegistration.txtUsername.value + "" == "")
		{
		if (smessage + "" != "") smessage = smessage + ", Username"
		else smessage="Username";
		bresult=false;
		}
		
	if (!document.frmRegistration.chkAgreement.checked)
	{
		if (smessage + "" != "") smessage = smessage + ", Terms of Use checkbox";
		else smessage="Terms of Use checkbox";
		bresult=false;
	}	
	
	if(document.frmRegistration.cboPromo.selectedIndex<1)
	{
		if (smessage + "" != "") smessage = smessage + ", Where did you hear about us.";
		else smessage="Where did you hear about us.";
		bresult=false;
	}
		
	if (smessage + "" != "")
		alert("The following fields need completing: " + smessage);						
		
	return bresult;
}

function sendformdetails()
{	
	if (checkcomplete() && emailCheck(document.frmRegistration.txtEmail.value))
	{
		if (checkPassword())
			document.frmRegistration.submit();
	}
}	

function SetInitial()
{
	var s = document.frmRegistration.txtForename.value;				
	document.frmRegistration.txtInitials.value = s.substr(0,1);
}

///////////////////////////////////////////////////////////////////////
// Carl Durber												31/12/01
//
// Freeware email validation script from 
// http://javascript.internet.com/forms/email-address-validation.html
//
// Validates email as far as possible
//
///////////////////////////////////////////////////////////////////////
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. */
	alert("Email address is invalid.")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
	// user is not valid
	alert("Invalid Email: The username part invalid")
	return false
}

/* 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) {
		    alert("Destination IP address is invalid.")
		return false
		}
	}
	return true
}


// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Invalid email domain.")
	return false
}


/* 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.
	alert("Invalid Email: The address must end in a three-letter domain, or two letter country.")
	return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
	var errStr="Invalid Email: Missing hostname."
	alert(errStr)
	return false
}

// If we've gotten this far, everything's valid!
return true;
}


//*****************************************************************
//Inform user of legally binding agreement if checkbox is checked- 
//John Ashmore 10/07/2003
//*****************************************************************
function agreeTerms()
{
	if (document.frmRegistration.chkAgreement.checked)
		alert('Please be advised that on checking this box and clicking submit, you will be entering into a legally binding contract with Intact from QAS based upon the Online Privacy Policy and the Terms of Use.');
}



// Need a variable to track if the user has opened the fid address page. We don't want to keep making 
// the address fields visible / invisible after the first time that they search.
var isFirstSearch = true;


// Opens the 'Find Address' popup
function findAddress()
{
	var strCountry = document.getElementById('cboCountry').value;
	var strPostCode = document.getElementById('txtPostcodeSearch').value;
	var strAddDetails = document.getElementById('txtBuilding').value;	
	var strError = '';

	
	/*
	if ('uk' != strCountry)
	{
		strError += 'Cannot get address for non-uk countries.\n';
	}
	
	if (0 == strPostCode.length)
	{
		strError += 'Please enter a postcode.\n';		
	}
	*/
	
	if (0 < strError.length)
	{
		alert(strError);
	}
	
	else
	{
		if (strCountry == 'uk')
			strCountry = 'GBRUnited Kingdom';
		
		document.frmRegistration.action = 'registrationform.asp?CountryCode=' + strCountry + '&SearchStage=lookup&Building='+ strAddDetails+'&Postcode='+ strPostCode;
		document.frmRegistration.submit();
		
		// Set the tracker to false so that the checkCountryChange() won't fire again
		isFirstSearch = false;
	}		
}


function checkCountryChange()
{
	// Show / hide the address controls depending on the country
	if ("uk" == document.getElementById("cboCountry").value && isFirstSearch)
	{
		document.getElementById("tbRwCompName").style.display		= "none";
		document.getElementById("tbRwDepName").style.display		= "none";
		document.getElementById("tbRwAddress1").style.display		= "none";
		document.getElementById("tbRwAddress2").style.display		= "none";
		document.getElementById("tbRwAddress3").style.display		= "none";
		document.getElementById("tbRwTown").style.display			= "none";
		document.getElementById("tbRwCounty").style.display			= "none";
		document.getElementById("tbRwPostcode").style.display		= "none";
		
		document.getElementById("tbRwBuilding").style.display		= "block";
		document.getElementById("tbRwPostcodeSearch").style.display = "block";
		document.getElementById("tbRwFindAddress").style.display	= "block";
		
		document.getElementById("tbRwBuildingAlt").style.display	= "none";
		document.getElementById("tbRwStreetAlt").style.display		= "none";
		document.getElementById("tbRwTownAlt").style.display		= "none";
		document.getElementById("tbRwFindAddressAlt").style.display	= "none";
		
		document.getElementById("tbRwToggleSearch").style.display	= "block";
		document.getElementById("postCodeToggle").innerHTML = "<a href='javascript:setAltSearchFields();'>If you don't know the postcode, click here.</a>";
		
		document.getElementById("cboCountry").tabIndex = 7;
	}	

	else
	{
		document.getElementById("tbRwCompName").style.display		= "block";
		document.getElementById("tbRwDepName").style.display		= "block";
		document.getElementById("tbRwAddress1").style.display		= "block";
		document.getElementById("txtAddress1").value				= "";
		document.getElementById("tbRwAddress2").style.display		= "block";
		document.getElementById("txtAddress2").value				= "";
		document.getElementById("tbRwAddress3").style.display		= "block";
		document.getElementById("txtAddress3").value				= "";
		document.getElementById("tbRwTown").style.display			= "block";
		document.getElementById("txtTown").value					= "";
		document.getElementById("tbRwCounty").style.display			= "block";
		document.getElementById("txtCounty").value					= "";
		document.getElementById("tbRwPostcode").style.display		= "block";
		
		document.getElementById("tbRwBuilding").style.display		= "none";
		document.getElementById("tbRwPostcodeSearch").style.display = "none";
		document.getElementById("tbRwFindAddress").style.display	= "none";
		
		document.getElementById("tbRwBuildingAlt").style.display	= "none";
		document.getElementById("tbRwStreetAlt").style.display		= "none";
		document.getElementById("tbRwTownAlt").style.display		= "none";
		document.getElementById("tbRwFindAddressAlt").style.display	= "none";
		
		document.getElementById("tbRwToggleSearch").style.display	= "none";
		
		document.getElementById("cboCountry").tabIndex = 15;
	}
	
	
	var selIndex = document.getElementById("cboCountry").selectedIndex;
	
	var countryCode = document.getElementById("cboCountry").options[selIndex].value;
	var countryText = document.getElementById("cboCountry").options[selIndex].innerHTML;
	
	
	// Set the hidden fields
	document.getElementById("tdCountryCode").innerHTML = "<input type='hidden' name='CountryCode' value='"+countryCode+"'>";
	document.getElementById("tdCountryText").innerHTML = "<input type='hidden' name='CountryText' value='"+countryText+"'>";	
}



/* We want the appearance of a multiple select listbox, with the ability to only select
one listitem at a time*/
function singleSelect()
{
	// Store the current selected index
	var intSel = document.getElementById("ddlAddress").selectedIndex;
	
	// Deselect everything
	document.getElementById("ddlAddress").listindex = -1;
	
	// Reselect only the current selected index
	document.getElementById("ddlAddress").selectedIndex = intSel;
}


function checkAndPopulate()
{
	var strSPM = document.getElementById("ddlAddress").value;
	
	if (null == strSPM || 0 == strSPM.length)
	{
		alert ('Please select an address from the list');
	}
	
	else
	{
		//window.location = 'registrationform.asp?SearchStage=populate&SPM='+strSPM;
		document.frmRegistration.action = 'registrationform.asp?SearchStage=populate&SPM='+strSPM;
		document.frmRegistration.submit();
	}
} 


function searchAgain()
{
	document.frmRegistration.action = 'registrationform.asp?ClearAddress=yes';
	document.frmRegistration.submit();
}



function setAltSearchFields()
{
	document.getElementById("tbRwBuilding").style.display		= "none";
	document.getElementById("tbRwPostcodeSearch").style.display = "none";
	document.getElementById("tbRwFindAddress").style.display	= "none";
	document.getElementById("tbRwFindAddress").style.display	= "none";

	document.getElementById("tbRwBuildingAlt").style.display	= "block";
	document.getElementById("tbRwStreetAlt").style.display		= "block";
	document.getElementById("tbRwTownAlt").style.display		= "block";
	document.getElementById("tbRwFindAddressAlt").style.display	= "block";
	
	document.getElementById("tbRwToggleSearch").style.display	= "block";
	document.getElementById("postCodeToggle").innerHTML = "<a href='javascript:setDefSearchFields();'>If you know the postcode, click here.</a>";	
}


function setDefSearchFields()
{
	document.getElementById("tbRwBuilding").style.display		= "block";
	document.getElementById("tbRwPostcodeSearch").style.display = "block";
	document.getElementById("tbRwFindAddress").style.display	= "block";
	document.getElementById("tbRwFindAddress").style.display	= "block";

	document.getElementById("tbRwBuildingAlt").style.display	= "none";
	document.getElementById("tbRwStreetAlt").style.display		= "none";
	document.getElementById("tbRwTownAlt").style.display		= "none";
	document.getElementById("tbRwFindAddressAlt").style.display	= "none";
	
	document.getElementById("tbRwToggleSearch").style.display	= "block";
	document.getElementById("postCodeToggle").innerHTML = "<a href='javascript:setAltSearchFields();'>If you don't know the postcode, click here.</a>";
}


function findAddressAlt()
{
	var strCountry = document.getElementById('cboCountry').value;
	var strBuilding = document.getElementById('txtBuildingAlt').value;
	var strStreet = document.getElementById('txtStreetAlt').value;	
	var strTown = document.getElementById('txtTownAlt').value;
	
	if (strCountry == 'uk')
		strCountry = 'GBRUnited Kingdom';
	
	document.frmRegistration.action = 'registrationform.asp?CountryCode=' + strCountry + '&SearchStage=lookup&Building='+ strBuilding+'&Street='+ strStreet+'&Town='+ strTown;
	document.frmRegistration.submit();
}



function PromCodeFreeText()
{
	var strOptionText = document.getElementById("cboPromo").options[document.getElementById("cboPromo").selectedIndex].innerText;
	
	if (-1 < strOptionText.toLowerCase().indexOf("please specify"))
	{
		document.getElementById("tbRwColPromoTxt").style.display = "block";
	}
	
	else
	{
		document.getElementById("txtQasPromo").value = "";
		document.getElementById("tbRwColPromoTxt").style.display = "none";
	}
	
	
	if ((-1 < strOptionText.toLowerCase().indexOf("systems integrator"))||(-1 < strOptionText.toLowerCase().indexOf("qas partner")))
	{
		document.getElementById("tbRwPartnerName").style.display = "block";
		document.getElementById("tbRwPromoCode").style.display = "none";
	}
	
	else
	{
		document.getElementById("txtPartnerName").value = "";
		document.getElementById("tbRwPromoCode").style.display = "block";
		document.getElementById("tbRwPartnerName").style.display = "none";
	}
}


function checkEmail()
{
	var strEmail = trim(document.getElementById("txtEmail").value);
	var strCheckEmail = trim(document.getElementById("txtEmail2").value);

	if (strEmail != strCheckEmail)
	{
		alert("Your re-typed email address did not match the original.\nPlease re-enter your email address.");
	}
	
}



/*
Need to check that the password obeys the following rules:

	- It is alphanumeric
	- It consists of between 6 and 32 characters
	- It is not the same as the username
*/

// Page variable keeps track of password validity
var passwrdIsOk = false;

function checkPassword()
{
	// Asssume the password is not OK
	passwrdIsOk = false;
	
	// Set some strings to check against - we are looking at alphanumeric chars only
	var strAlphabet = "abcdefghijklmnopqrstuvwxyz";
	var strNumbers = "0123456789";
	
	
	// Get back the password
	var strPassword = trim(document.getElementById("txtPassword").value).toLowerCase() + "";
	var strUsername = trim(document.getElementById("txtUsername").value).toLowerCase() + "";
	
	var ch;
	var intAlpha = 0;
	var intNumeric = 0;
	
	
	// First make sure that it is the right number of characters
	if (6 > strPassword.length || 32 < strPassword.length)
	{
		alert("Your password should consist of between 6 and 32 characters.");
		return false;
	}
	
	// Now make sure that it does not contain the username
	if (0 < strUsername.length && strPassword == strUsername)
	{
		alert("Your password should not be the same as your username.");
		return false;
	}
	
	// If it is, make sure we have an alphanumeric string by checking against strAlphabet
	// and strNumbers.
	
	// Loop through the password
	for (i = 0; i < strPassword.length; i++)
	{
		// Get back the character
		ch = strPassword.charAt(i);
		
		// Is it a number
		if (-1 < strNumbers.indexOf(ch))
		{
			intNumeric++;
		}
		
		// Is it an alphabetical char
		else if (-1 < strAlphabet.indexOf(ch))
		{
			intAlpha++;
		}
		
		// If it is something else, throw it out
		else
		{
			alert("Your password contains at least one invalid character.\nPlease use only alphabetical or numeric characters.");
			return false;
		}
		
	}
	
	// Make sure that there are > 0 alphabetical and numeric characters
	if (0 == intAlpha || 0 == intNumeric)
	{
		alert("Your password needs to be a mixture of alphabetical and numeric characters.");
		return false;
	}
	
	// Otherwise, the password is OK
	else
	{
		passwrdIsOk = true;
		return true;
	}
}


// Check the passwords match
function validatePassword()
{
	// If the original password was not entered correctly, let them go back
	// and fix it without throwing up an obvious message from here.	
	if (!passwrdIsOk)
		return;
		
		
	// Get back the two passwords	
	var strPass = trim(document.getElementById("txtPassword").value);
	var strCheckPass = trim(document.getElementById("txtPassword2").value);

	// Compare them
	if (strCheckPass != strPass)
	{
		alert("Your re-typed password did not match the original.\nPlease re-enter your password.");
	}
	
}


// Trims all white space at the beginning and end of a string
function trim(s) 
{
	while (s.substring(0,1) == ' ') 
	{
		s = s.substring(1,s.length);
	}
  
	while (s.substring(s.length-1,s.length) == ' ') 
	{
		s = s.substring(0,s.length-1);
	}
  
	return s;
	
}	// End function
