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];}
}
//-->

var isNS4 = (document.layers ? true : false);


function checkEmptyField(field, name){
	var returnVal = true;

	if((field.value).length == 0){
		alert("You have to populate the "+ name+" field." );
		field.focus(); 
		returnVal = false;
	}
	return returnVal;

}


function checkEmpty(loginForm){

	var returnVal = true;

	if ((document.loginForm.userID.value).length == 0){
		alert("You have to populate the UserID!" );
		document.loginForm.userID.focus(); 
		returnVal = false;
	}
	else if ((document.loginForm.password.value).length == 0){
		alert("You have to populate the Password!" );
		document.loginForm.password.focus();
		returnVal = false;
	}
	return returnVal;
}

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 if( form.problemDesc.value.length == 0){
		alert("Please write a problem description.");
		form.problemDesc.focus();
	}
	else 
		returnVal = true;	
	
	return returnVal;
}



function ltrim ( s ){
	return s.replace( /^\s*/, "" )
}

function rtrim ( s ){
	return s.replace( /\s*$/, "" );
}

function trim ( s ){
	return rtrim(ltrim(s));
}

function copyResidentalToPostal(addressForm){
	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.countryID.value = document.addressForm.phyCountryID.value ;
	document.addressForm.streetName.value = document.addressForm.phyStreetName.value ;
	document.addressForm.postcode.value = document.addressForm.phyPostcode.value ;
}



function properCase(field){
	var text = field.value;

	text = capitalizeWords(text);
	field.value = text;

}

function capitalizeWords(string) {
	var tmpStr, tmpChar, preString, postString, strlen; 
	tmpStr = string.toLowerCase(); 
	stringLen = tmpStr.length; 
	if (stringLen > 0) { 
		for (i = 0; i < stringLen; i++) { 
			if (i == 0) { 
				tmpChar = tmpStr.substring(0,1).toUpperCase(); 
				postString = tmpStr.substring(1,stringLen); 
				tmpStr = tmpChar + postString; 
			} else { 
				tmpChar = tmpStr.substring(i,i+1); 
					if (tmpChar == " " && i < (stringLen-1)) { 
						tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
						preString = tmpStr.substring(0,i+1);
						postString = tmpStr.substring(i+2,stringLen);
						tmpStr = preString + tmpChar + postString; }
					} 
			} 
		} 
	return tmpStr; 
}






function validateDate(day,month,year) {
		if (day < 29) {
			return true;
		}
		else {
			if (month == "01" | month == "03" | month == "05" | month == "07" | month == "08" | month == "10" | month == "12" ) {
				return true;
		}
		else {
			if (month != "02") {
				if (day <= 30) {
					return true;
				}
				else {
					return false;
				}
			}
			else {
				//check for leap year because it is Feruary
				if ( (1996-year)%4 == 0) {
					if (day <= 29) {
						return true;
					}
					else {
						return false;
					}
				}
				else {
					if (day <=28) {
						return true;		
					}
					else {
						return false;
					}
				}
			}
		}
	}		
}
 
function validateData(){
	
	sDD		=document.dateForm.startDD.value
	sMM		=document.dateForm.startMM.value
	sYYYY	=document.dateForm.startYYYY.value

	eDD		=document.dateForm.endDD.value
	eMM		=document.dateForm.endMM.value
	eYYYY	=document.dateForm.endYYYY.value


	result = true;

	if(!validateDate(sDD,sMM,sYYYY)){
		alert("Start Date is invalid.");
		return false;
	}
	if(!validateDate(eDD,eMM,eYYYY)){
		alert("End Date is invalid.");
		return false;
	}

	var startDate = new Date(sYYYY,sMM,sDD);
	var endDate   = new Date(eYYYY,eMM,eDD);

	if(startDate.getTime() > endDate.getTime()){
		alert("Start Date cannot be after End Date");
		return false;
	}

	
	 

	return result;	
}
