
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->


function validate(field, bool){
	var validChar = "abcdefghijklmnopqrstuvwxyz' ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var validNum = "0123456789"
	var valid, temp;
	var ok = "yes";
	var returnVal = false;

	if(field.value.length == 0){
		returnVal = true;
	}
	else{
		for (var i=0; i<field.value.length; i++) {
			temp = "" + field.value.substring(i, i+1);
			if(bool)	valid = validNum
			else		valid = validChar
			if (valid.indexOf(temp) == "-1") 
				ok = "no";
		}
		if (ok == "no") {
			if(bool)	alert("Invalid entry!  Only numbers are accepted!");
			else		alert("Invalid entry! Only characters are accepted!");
			field.focus();
			field.select();
		}else
			returnVal = true;
	}
		
	return returnVal;
}

//Checks if the cardNumber is valid.
function checkDigit(field){
	//only for preallocated cards - depending on the scheme
	var nCheckDigit =0;
	var nCheckSum = 0;
	var returnVal = false;
	var itemVal = field.value;

	if((itemVal.length != 0) && (itemVal.length != 16)){
		alert("You have entered an incorrect card number.");
		field.focus();
		field.select();
	}
	else if(itemVal.length != 0){
		for( var i=0; i < 16 ; i++) {
			nCheckDigit = itemVal.charAt(i);
			if(i%2 == 0)
				if(nCheckDigit < 5)	nCheckDigit = nCheckDigit*2;
				else				nCheckDigit = nCheckDigit*2 - 9;
			nCheckSum = nCheckSum + eval(nCheckDigit);	
		}	
		if (nCheckSum % 10 == 0)
			returnVal = true;
		else{
			alert("You have entered an incorrect card number.");
			field.focus();
			field.select();
		}
	}

	return returnVal;
}

//Checks for empty fields in the CustLogin.html form
function checkEmpty(form){

	var returnVal = true;
	var selDay   = findSelected(form.Day);
	var selMonth = findSelected(form.Month);
	var selYear  = findSelected(form.Year);

	if ((form.cardNumber.value).length == 0){
		alert("You have to populate the Card Number!" );
		form.cardNumber.focus(); 
		returnVal = false;
	}
	else if ((form.lastName.value).length == 0){
		alert("You have to populate the Last Name!" );
		form.lastName.focus();
		returnVal = false;
	}
	else if ((form.homePhone.value).length == 0)
		if (!confirm("Are you sure you want to leave the phone number empty?")){
			form.homePhone.focus();
			returnVal = false;
		}
	else if (selDay == "00" && selMonth == "00" && selYear == "0000")
		if (!confirm("Are you sure you want to leave the date of birth empty?")){
			form.Day.focus();
			returnVal = false;
		}

	return returnVal;
}


//Checks for empty fields in the OutletLogin.html form
function checkEmptyOutlet(form){
	var returnVal = false;

	if ((form.outletID.value).length == 0){
		alert("You have to populate the Outlet Number!" );
		form.outletID.focus(); 
	}
	else if ((form.outletPin.value).length == 0){
		alert("You have to populate the Outlet PIN!" );
		form.outletPin.focus();
		}
	else
		returnVal = true;
	
	return returnVal;
}

//Checks for empty fields in the CustTechSupport.jsp form
function checkTechSupport(form){
	
	var returnVal = false;

	if(form.firstName.value.length == 0){
		alert("Please populate your First name.");	
		form.firstName.focus();
	}
	else if (form.lastName.value.length == 0){
		alert("Please populate your Surname.");	
		form.lastName.focus();
	}
	else if( form.email.value.length == 0){
		alert("Please populate your email.");
		form.email.focus();
	}
	else 
		returnVal = true;	
	
	return returnVal;
}


//======================================================================
// Dates
//======================================================================

var today = new Date();
var startDate = new Date(1999, 6, 1);

//Check if Day is 31, that Month is not April,June,August or November.
function checkDay(day,month){
	var shortMonth= new Array("04", "06" ,"09", "11");
	var sMonth = findSelected(month);
	var sDay   = findSelected(day);  
    
	for(var i=0; i< shortMonth.length ; i++){
		if(sMonth == shortMonth[i])
			if(sDay == "31"){
				alert("You have entered invalid date.");
				day.focus();
				break;
				return;
		}
	}
}

//Check if selected Day is 29 and Month is February,
//that the Year is a LeapYear.
function checkFeb(day, month, year){

	var leapYear = new Array("1996","2000","2004");
	var sDay=findSelected(day);  
	var sMonth=findSelected(month);
	var sYear = findSelected(year); 
	var isIn = false;

	if(sMonth == "02"){
		if(sDay > "29") {
			alert(" You have entered invalid date.");
			day.focus();
			return false;
		}
		else if(sDay == "29"){
			for(var i=0; i< leapYear.length ; i++){
				if(sYear == leapYear[i])
					isIn = true;
				if(isIn == false){
					alert("You have entered invalid date.");
					day.focus();
					return false;
				}
			}
		}
	}	
	
	return true;
}

//Check that the Date is not greater than the Today
function checkCurrent(day, month, year){
	var sYear = findSelected(year);
	var sMonth = findSelected(month);
	var sDay = findSelected(day);
    
	// the month ranges from 0 to 11
	var startDate = new Date(sYear, sMonth-1, sDay);
	var currentDate = new Date();
        
	if(startDate.valueOf() > currentDate.valueOf()) {
		alert("The date is greater than the current date.");
		day.focus();
		return false;
    }
	
	return true;
}

//Check the date range is valid - StartDate is not greater than EndDate
function checkDate(day, month, year,  endDay, endMonth, endYear){

	var returnVal = true;
	
	var sDay = findSelected(day);
	var sMonth = findSelected(month);
	var sYear = findSelected(year);
	
	var eDay = findSelected(endDay);
	var eMonth = findSelected(endMonth);
	var eYear = findSelected(endYear);
	
	// the month ranges from 0 to 11
	var startDate = new Date(sYear, sMonth-1, sDay);
	var endDate = new Date(eYear, eMonth-1, eDay);
		  
	if(startDate.valueOf() > endDate.valueOf()) {
		alert("The start date is greater than the end date.");
		day.focus();
		returnVal = false;
	}
    
	return returnVal;
}


//Set the date range from 01/06/1996 to today's date
function setDates(myForm){

	document.myForm.startDay.selectedIndex = startDate.getDate() -1
	document.myForm.startMonth.selectedIndex = startDate.getMonth() -1
	document.myForm.startYear.selectedIndex = startDate.getYear() - 95

	document.myForm.endDay.selectedIndex = today.getDate() -1
	document.myForm.endMonth.selectedIndex = today.getMonth()
	document.myForm.endYear.selectedIndex = today.getYear()- 1995
}

//======================================================================

//======================================================================
// Address methods
//======================================================================

function ltrim ( s ){
	return s.replace( /^\s*/, "" )
}

function rtrim ( s ){
	return s.replace( /\s*$/, "" );
}

function trim ( s ){
	return rtrim(ltrim(s));
}

//Copy residential address to postal address
 function copyResidentalToPostal(addressForm){

	document.addressForm.sundry.value = document.addressForm.phySundry.value ; 
	if(!(trim(document.addressForm.postType.value) == "2" || trim(document.addressForm.postType.value) == "3" || 
		 trim(document.addressForm.postType.value) == "5" || trim(document.addressForm.postType.value) == "6")){
		document.addressForm.flatNo.value = document.addressForm.phyFlatNo.value ;
		document.addressForm.streetNo.value = document.addressForm.phyStreetNo.value ; 
		document.addressForm.streetName.value = document.addressForm.phyStreetName.value ; 
		document.addressForm.streetType.value = document.addressForm.phyStreetType.value ;
		document.addressForm.streetSuffix.value = document.addressForm.phyStreetSuffix.value ;
	}

	document.addressForm.suburb.value = document.addressForm.phySuburb.value ;
	document.addressForm.city.value = document.addressForm.phyCity.value ;
	document.addressForm.state.value = document.addressForm.phyState.value ;
	document.addressForm.country.value = document.addressForm.phyCountry.value ;
	countryChange(addressForm,true);

 }

function countryChange(addressForm,loading){
	var index = document.addressForm.postType.selectedIndex;
	var len = document.addressForm.postType.length;
	if(trim(document.addressForm.country.value) == "New Zealand" || 
	   trim(document.addressForm.country.value) == "Australia"){

		for (var i = 0; i < len; i++){
			document.addressForm.postType.options[0] = null;
		}	
		document.addressForm.postType.options[0] = new Option("Standard");
		document.addressForm.postType.options[1] = new Option("PO Box");
		document.addressForm.postType.options[2] = new Option("Private Bag");
		document.addressForm.postType.options[3] = new Option("RD");
		document.addressForm.postType.options[4] = new Option("Counter Delivery");
		document.addressForm.postType.options[5] = new Option("Locked Bag");
		document.addressForm.postType.options[6] = new Option("RMD");
		document.addressForm.postType.options[7] = new Option("RMS");

		document.addressForm.postType.options[0].value = "1";
		document.addressForm.postType.options[1].value = "2";
		document.addressForm.postType.options[2].value = "3";
		document.addressForm.postType.options[3].value = "4";
		document.addressForm.postType.options[4].value = "5";
		document.addressForm.postType.options[5].value = "6";
		document.addressForm.postType.options[6].value = "7";
		document.addressForm.postType.options[7].value = "8";
		
		document.addressForm.postType.options[index].selected = true;
	}else{
		for (var i = 0; i < len; i++){
			document.addressForm.postType.options[0] = null;
		}
		document.addressForm.postType.options[0] = new Option("Standard");
		document.addressForm.postType.options[0].value = "1";
		document.addressForm.postType.options[0].selected = true;
	}
	postTypeChange(document.addressForm,loading);

}

function postTypeChange(addressForm, loading){
	if(trim(document.addressForm.postType.value) == "1"){
		
		document.addressForm.postTypeC.disabled = true;
		document.addressForm.streetName.disabled = false;
		document.addressForm.flatNo.disabled = false;
		document.addressForm.streetType.disabled = false;
		document.addressForm.streetNo.disabled = false;
		document.addressForm.streetSuffix.disabled = false;
		if(!loading){
			document.addressForm.postTypeC.value = "";
			document.addressForm.streetName.value = "";
			document.addressForm.flatNo.value = "";
			document.addressForm.streetType.value = "";
			document.addressForm.streetNo.value = "";
			document.addressForm.streetSuffix.value = "";
		}

	}else if(trim(document.addressForm.postType.value) == "2" || trim(document.addressForm.postType.value) == "3" || 
			 trim(document.addressForm.postType.value) == "5" || trim(document.addressForm.postType.value) == "6"){
		
		document.addressForm.postTypeC.disabled = false;
		document.addressForm.streetName.disabled = true;
		document.addressForm.flatNo.disabled = true;
		document.addressForm.streetType.disabled = true;
		document.addressForm.streetNo.disabled = true;
		document.addressForm.streetSuffix.disabled = true;
		if(!loading){
			document.addressForm.streetName.value = "N/A";
			document.addressForm.flatNo.value = "N/A";
			document.addressForm.streetType.value = "N/A";
			document.addressForm.streetNo.value = "N/A";
			document.addressForm.streetSuffix.value = "N/A";
		}
	}else{
		document.addressForm.postTypeC.disabled = false;
		document.addressForm.streetName.disabled = false;
		document.addressForm.flatNo.disabled = false;
		document.addressForm.streetType.disabled = false;
		document.addressForm.streetNo.disabled = false;
		document.addressForm.streetSuffix.disabled = false;
		if(!loading){
			document.addressForm.postTypeC.value = "";
			document.addressForm.streetName.value = "";
			document.addressForm.flatNo.value = "";
			document.addressForm.streetType.value = "";
			document.addressForm.streetNo.value = "";
			document.addressForm.streetSuffix.value = "";
		}	
	}
}

 function loading(){

	document.addressForm.phyStreetType.value = trim(document.addressForm.phyStreetTypeH.value);
	document.addressForm.phyCountry.value = trim(document.addressForm.phyCountryH.value);

	document.addressForm.country.value = trim(document.addressForm.countryH.value);
	countryChange(document.addressForm, true);
	document.addressForm.postType.value = trim(document.addressForm.postTypeH.value);
	postTypeChange(document.addressForm, true);
	document.addressForm.streetType.value = trim(document.addressForm.streetTypeH.value);

 }

  function resetForm (objForm){
	document.AddressForm.reset(objForm);
	loading();
 }

//Validate address
function validateAddress(addressForm){
	var postTypeArray = new Array(
	"Standard","PO Box" ,"Private Bag" ,"RD" ,"Counter Delivery" ,"Locked Bag" ,"RMD" ,"RMS" ,"RDS" ,"RMB" ,0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 );

	if(trim(document.addressForm.postType.value) != "1"){ 
		if(trim(document.addressForm.postTypeC.value).length == 0){
			alert("You have to give a "+postTypeArray[document.addressForm.postType.selectedIndex]+" code");
			document.addressForm.postTypeC.focus();
			document.addressForm.postTypeC.select();
			return false;
		}
	}
	if(trim(document.addressForm.postCode.value).length == 0){
		alert("You have to give a Postal Address Post Code");
		document.addressForm.postCode.focus();
		document.addressForm.postCode.select();
		return false;
	}
	if(!(trim(document.addressForm.postType.value) == "2" || trim(document.addressForm.postType.value) == "3" 
	 || trim(document.addressForm.postType.value) == "5"  || trim(document.addressForm.postType.value) == "6")){
		if(trim(document.addressForm.streetName.value).length == 0){
			alert("You have to give a Postal Address Street Name");
			document.addressForm.streetName.focus();
			document.addressForm.streetName.select();
			return false;
		}
		if(trim(document.addressForm.streetType.value).length == 0){
			alert("You have to give a Postal Address Street Type");
			return false;
		}
	}
	if(trim(document.addressForm.suburb.value).length == 0 && trim(document.addressForm.city.value).length == 0){
		alert("You have to give either a Suburb Name or a City Name for your Postal Address");
		document.addressForm.suburb.focus();
		document.addressForm.suburb.select();
		return false;
	}
	return true;
 }

//======================================================================
	

//======================================================================
// Utility methods
//======================================================================
	
	
//Utility method 
//returns the selected text in the specified combo box 
function findSelected(combo){
	var comboText="";

	for(var i=0; i<combo.length ; i++)
		if(combo.options[i].selected){
			comboText = combo.options[i].text;
			break;
		}
	return comboText;
}	

//======================================================================


/*

	
function validateEmpty(field){
	var retValue = false;
	var s = field.value;
	var temp = "";


	for(var i =0; i< s.length ; i++)
		if(s.substring(i,1) != " ")
			temp = temp + s.substring(i, 1);
	
	if (temp.length == 0){
		alert("Please populate the " + field.name + " field!");
	}	
	else
		retValue = true;

	return retValue;
}

function checkForm(form){
	var retValue = false;
		
	if( validateEmpty(form.userID)){
		if(validateEmpty(form.password)){
			if(form.outlet.checked == true)
				//retValue = true;
				if ((form.outlet.value == "OutletMaintenance")	|| (form.outlet.value == "OutletQuery")){
					if(!validateEmpty(form.outletID))
						form.outletID.focus();
					else
						retValue = true;	
				
				}
				if ((form.outlet.value == "UpdateCustomer")	|| (form.outlet.value == "CustomerQuery"))
					if(!validateEmpty(form.cardNumber))
						form.cardNumber.focus();
					else
						retValue = true;
				
				if ((form.outlet.value == "ViewPromotion")	|| (form.outlet.value == "PromotionAnalisys"))
					if(!validateEmpty(form.promoID))
						form.promoID.focus();
					else
						retValue = true;

		}
		else
			form.password.focus();
	}
	else
		form.userID.focus();
	
	return retValue;
}

function validateCustList(form){
	
	var retValue = false;

	if (! form.customerNo.value == "")
		retValue = true;

	if (! form.cardNumber == "")
		retValue = true;
	
	if (! form.surname == "")
		retValue = true;
	
	if(! form.firstName == "")
		retValue = true;

	if(! form.maidenName == "")
		retValue = true;
	
	if(! form.transNo == "")
		retValue = true;

	if(! form.chain == "")
		retValue = true;

	if(! form.phone == "")
		retValue = true;
	
	if(! form.outlet == "")
		retValue = true;

	if(! form.suburb == "")
		retValue = true;



	return retValue;
}

function clearFields(form){
	
	form.outletID.value = "";
	form.cardNumber.value = "";
	form.promoID.value = "";

}


var cardString = "Card used more than";
var timeString = "times";

function hideCard(myForm,  bool){
	
	if(bool){
	
		myForm.cardValue.fontcolor = "black";
		myForm.timeString.fontcolor= "black";
		history.go(0);
	/*	document.writeln("<TR>");
		document.writeln("<TD ALIGN=\"CENTER\" colspan=2><br>Card used more than");
		document.writeln("<SELECT NAME=\"value\">");
		document.writeln("<OPTION>0<OPTION>1<OPTION>2<OPTION>3<OPTION>5<OPTION>10");
		document.writeln("</SELECT>times ");
		document.writeln("<BR><BR>");
		document.writeln("</TD>");
		document.writeln("</TR>");
	*//*
	}
	else{
		myForm.cardValue.fontcolor = "white";
		myForm.timeString.fontcolor= "white";
		history.go(0);
	
	}

}
*/
