var xmlHttp;

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function WindowOpen1()
 {
  window.open('sec-code.asp', 'Window1', 'resizable=yes,height=200,width=200,scrollbars=no');
 }


// JavaScript Document

var blankImage = new Image();
blankImage.src = 'blank.gif';

var locks = new Object();

locks.amount = 0;
locks.table = new Array();

function valreg(theForm)
{
	if (theForm.yourname.value == "" || theForm.yourname.value == "Name")
	{
	alert("Please enter your name.");
	theForm.yourname.value = "";
	return false;
	}
	
	if (theForm.youremail.value == "" || theForm.youremail.value == "Email")
	{
	alert("Please enter your email address");
	theForm.youremail.value = "";
	return false;
	}
	
	// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = theForm.youremail.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
	if (EmailAt && EmailPeriod)
	break;
	if (j == checkEmail.length)
	break;
	}
	// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
	EmailValid = true
	break;
	}
	}
	if (!EmailValid)
	{
	alert("Please enter a valid email address.");
	theForm.youremail.focus();
	return (false);
	}
		
} 

function GetXmlHttpObject(handler)
{ 
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This site doesn't work with Opera") 
return 
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{ 
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
} 
try
{ 
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler 
return objXmlHttp
} 
catch(e)
{ 
alert("Error. Scripting for ActiveX/Javascript might be disabled") 
return 
} 
} 
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler 
return objXmlHttp
}
} 





function addLock(name)
{
	if(locks.amount == 0)
		lockScreen();
	
	if(!locks[name] || locks[name] == '')
	{
		locks.table[name] = locks.length;
		locks.amount++;
	}
};

function removeLock(name)
{
	if(locks.table[name] != '')
	{
		locks.table[name] = '';
		locks.amount--;
	}

	if(locks.amount == 0)
		unlockScreen();
};

function lockScreen()
{
	document.getElementById('page_cover').style.display = 'block';
	document.getElementById('loadTable').style.display = 'block';

};

function unlockScreen()
{
	document.getElementById('page_cover').style.backgroundImage = "url('semi-transparent.gif')";
	document.getElementById('loadTable').style.display = 'block';
	document.getElementById('page_cover').style.display = 'none';
};


function formCheck() 
{

	
// ---------- Endo of Include file -----------------

	document.formLocation.CardHolder.focus()
    if (document.formLocation.CardHolder.value == "")
    {
      alert("Please enter your name as it appears on your card");
      return false;
    }

//	formLocation.postcode.focus();

//	if (formLocation.postcode.value == "" ) {
//		alert("Please enter a postcode")
//		return false
//	}


//	if (!checkPostcode(formLocation.postcode)) 
//	{
//		return false;
//	}
	

	document.formLocation.cardno.focus()
    if (document.formLocation.cardno.value == "")
    {
      alert("Please enter your credit or debit card number");
      return false;
    }

	if ((document.formLocation.cardno.value.charAt(0) != "3") &&
		(document.formLocation.cardno.value.charAt(0) != "4") &&
		(document.formLocation.cardno.value.charAt(0) != "5") &&
		(document.formLocation.cardno.value.charAt(0) != "6")) {
    	alert("Please enter a valid credit or debit card number");
    	return false;
	}
	cardnolen = 0;
	cardnoNoSpc = "";
	for (i=0; i<22; i++) 
	{
		if ((document.formLocation.cardno.value.charAt(i) != ' ') && (document.formLocation.cardno.value.charAt(i) != '')) 
			{
			cardnoNoSpc = cardnoNoSpc + document.formLocation.cardno.value.charAt(i);
			cardnolen = cardnolen + 1;
			}
	}

if ((cardnolen < 13) || (cardnolen > 22)) {
    	alert("Please enter a valid credit or debit card number");
    	return false;
	}
	luhntotal = 0
	even = false
	for (i=cardnolen-1; i>=0 ; i--) {
		if (even) {
			testvalue = cardnoNoSpc.charAt(i) * 2
			if (testvalue > 9) {
				testvalue = testvalue - 9
			}
			luhntotal = luhntotal + parseInt(testvalue, 10)
		}
		else
		{
			luhntotal = luhntotal + parseInt(cardnoNoSpc.charAt(i), 10)
		}

		even = !even
	}
	if ((luhntotal % 10) != 0) {
    	alert("Please enter a valid credit or debit card number");
    	return false;
	}

	document.formLocation.cardno.value = cardnoNoSpc;

    if (document.formLocation.expirymonth.options[document.formLocation.expirymonth.selectedIndex].value == "MM")
    {
      alert("Please enter the month your credit or debit card expires");
      return false;
    }

    if (document.formLocation.expiryyear.options[document.formLocation.expiryyear.selectedIndex].value == "YY")
    {
      alert("Please enter the year your credit or debit card expires");
      return false;
    }

    if ((document.formLocation.startmonth.options[document.formLocation.startmonth.selectedIndex].value != "") && (document.formLocation.startyear.options[document.formLocation.startyear.selectedIndex].value == ""))
    {
      alert("Please enter the year of your switch card start date");
      return false;
    }

    if ((document.formLocation.startyear.options[document.formLocation.startyear.selectedIndex].value != "") && (document.formLocation.startmonth.options[document.formLocation.startmonth.selectedIndex].value == ""))
    {
      alert("Please enter the month of your switch card start date");
      return false;
    }
	
	
	document.formLocation.securitycode.focus()
    if (document.formLocation.securitycode.value == "")
    {
      alert("Please enter your three digit security code.");
      return false;
    }
	
	document.formLocation.securitycode.focus()
	if (document.formLocation.securitycode.value.length  < 3)
    {
      alert("Please enter your three digit security code.");
      return false;
    }
	
	
	document.formLocation.securitycode.focus()
	if (document.formLocation.securitycode.value.length  < 3)
    {
      alert("Please enter your three digit security code.");
      return false;
    }
	else
	{
	// document.formLocation.makepaymentbutton.disabled = true;	
	}



}	


function isNumber(strLetter) {
	if ("0123456789".indexOf(strLetter) == -1) {
		return false
	}
	else {	
		return true
	}
} //isNumber

function isLetter(strLetter) {
	if ("ABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(strLetter) == -1) {
		return false
	}
	else {	
		return true
	}
} //isLetter


function checkPostcode(postcode){
	
	postcode.value = postcode.value.toUpperCase()

	if (postcode.value == "" ) {
		alert("Please enter a postcode")
		return false
	}

	postcodeNoSpc = new String
	for (i=0; i<8 ; i++) {
		if (postcode.value.charAt(i)!=" ") {
			postcodeNoSpc = postcodeNoSpc + postcode.value.charAt(i)
		}
	}

	//Check length is valid
	if ((postcodeNoSpc.length < 5) || (postcodeNoSpc.length > 7)) {
		alert("Invalid postcode entered - please re-enter")
		return false
	}

	//Validate the first character of the outcode
	if (!isLetter(postcodeNoSpc.charAt(0))) {
		alert("Invalid postcode entered - please re-enter")
		return false
	}

	//Validate the incode - which must be NAA
	if (!isNumber(postcodeNoSpc.charAt(postcodeNoSpc.length - 3))) {
		alert("Invalid postcode entered - please re-enter")
		return false
	}

	if (!isLetter(postcodeNoSpc.charAt(postcodeNoSpc.length - 2))) {
		alert("Invalid postcode entered - please re-enter")
		return false
	}

	if (!isLetter(postcodeNoSpc.charAt(postcodeNoSpc.length - 1))) {
		alert("Invalid postcode entered - please re-enter")
		return false
	}

	//Validate the rest of the outcode - three cases depending on length
	if (postcodeNoSpc.length == 5) { //case AN so second char must be N
		if (!isNumber(postcodeNoSpc.charAt(1))) {
			alert("Invalid postcode entered - please re-enter")
			return false
		}
	}

	if (postcodeNoSpc.length == 6) { //case ANN AAN ANA so cannot have second and third char both as A
		if ((isLetter(postcodeNoSpc.charAt(1))) && (isLetter(postcodeNoSpc.charAt(2)))) {
			alert("Invalid postcode entered - please re-enter")
			return false
		}
	}

	if (postcodeNoSpc.length == 7) { //case AANN AANA so second char must be A and third char must be N
		if (!isLetter(postcodeNoSpc.charAt(1))) {
			alert("Invalid postcode entered - please re-enter")
			return false
		}
		if (!isNumber(postcodeNoSpc.charAt(2))) {
			alert("Invalid postcode entered - please re-enter")
			return false
		}
	}

	validpostcode = new String
	for (i=0; i<8 ; i++) {
		validpostcode = validpostcode + postcodeNoSpc.charAt(i)
		if (i == (postcodeNoSpc.length - 4)) {
			validpostcode = validpostcode + ' '
		}
	}

	postcode.value = validpostcode

	return true
} //checkPostcode

function resizeTasks()
{
	try
	{
		document.getElementById('page_cover').style.height = document.documentElement.scrollHeight + 'px';
	}
	catch(e)
	{
		
	}
};

window.onresize = resizeTasks;

function valpromo(form)
{
	if (document.delform.promocode.value == "")
	{
	alert("Please enter your promotion code");
	return false;
	}

	var url="checkcode.asp?un=" + document.delform.promocode.value;
	xmlHttp=GetXmlHttpObject(codecheck);
	xmlHttp.open("GET", url , true);
	//window.open(url);
	xmlHttp.send(null);
}

function codecheck() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
	var resp = xmlHttp.responseXML; 
	var stat = resp.getElementsByTagName('stat')[0].firstChild.data;
	//var ss = document.getElementById('msg')
	//ss.innerHTML = stat;
	alert(stat);
	window.location.reload( true );
	} 
} 

function numeralsOnly(evt, notNumberFunction) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
        ((evt.which) ? evt.which : 0));

	//handle control chars
	if(evt.charCode == 0)
		if((charCode == 8) || (charCode == 46) || (charCode == 13) || (charCode == 9) || ((charCode > 36)&&(charCode < 41)))
			return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        if(notNumberFunction != null)
		notNumberFunction();

        return false;
    }
    return true;
};
