///////////////// open popup /////////////////////////////////////////////////

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



///////////////// restrict input to numbers only /////////////////////////////
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

else
   return false;
}

///////////////// validate register form /////////////////////////////////////////////

function checkForm() {

if(document.submitDetails.firstName.value.length < 2){
		alert("Please enter a valid First Name");
		document.submitDetails.firstName.focus();
		return false;
}

if(document.submitDetails.lastName.value.length < 2){
		alert("Please enter a valid Surname");
		document.submitDetails.lastName.focus();
		return false;
}

if(document.submitDetails.address.value.length < 4){
		alert("Please enter a valid Street");
		document.submitDetails.address.focus();
		return false;
}

if(document.submitDetails.suburb.value.length < 2){
		alert("Please enter a valid Suburb");
		document.submitDetails.suburb.focus();
		return false;
}

if(document.submitDetails.state.value == "" ){
		alert("Please select a State");
		document.submitDetails.state.focus();
		return false;
}

if(document.submitDetails.postCode.value.length != 4){
		alert("Please enter a valid Postcode");
		document.submitDetails.postCode.focus();
		return false;
}


/////////////////////////////////////////////////////////////////////////////////////////////////// validate date format /////////////////////////////////
/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year

var dateError = false;

var dtCh= "/";
var minYear=1900;
var maxYear=2007;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		alert("The date of birth format should be: dd/mm/yyyy")
		dateError = true
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid Month")
		dateError = true
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid Day")
		dateError = true
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid Year of Birth")
		dateError = true
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid Date of Birth")
		dateError = true
		return false
	}	
	
return true
}


	var dt=document.submitDetails.DOB
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}


	
	


if(document.submitDetails.homePhone.value.length != 10){
		alert("Please enter a valid Day Time Phone Number (including area code)");
		document.submitDetails.homePhone.focus();
		return false;
}

email = document.submitDetails.email.value;
AtPos = email.indexOf("@");
StopPos = email.lastIndexOf(".");

if (email == "") {
alert("Enter valid Email Address");
document.submitDetails.email.focus();
return false;
}

if (AtPos == -1 || StopPos == -1) {
alert("Enter valid Email Address");
document.submitDetails.email.focus();
return false;
}

if (StopPos < AtPos) {
alert("Enter valid Email Address");
document.submitDetails.email.focus();
return false;
}


if (StopPos - AtPos == 1) {
alert("Enter valid Email Address");
document.submitDetails.email.focus();
return false;
}

if(document.submitDetails.prod1.value.length < 4){
		alert("Please enter the last 4 digits of product barcode 1");
		document.submitDetails.prod1.focus();
		return false;
}

if(document.submitDetails.prod2.value.length < 4){
		alert("Please enter the last 4 digits of product barcode 2");
		document.submitDetails.prod2.focus();
		return false;
}



if(document.submitDetails.var3.checked == false){
	alert("You must agree to the Terms and Conditions");
	document.submitDetails.var3.focus();
	return false;
}	

if(document.submitDetails.prod1.value.length == 4 && document.submitDetails.prod2.value.length == 4){
		document.submitDetails.entry.value = document.submitDetails.prod1.value +" "+ document.submitDetails.prod2.value;
	
}





	return true;
		
		
}

/////////////////////////////////////////////////////////////////////////////////////////////////// calculate age /////////////////////////////////
function calculateAge() {
document.getElementById("dobPopup").style.display = 'none';

var today=new Date(); 
var d = document.submitDetails.DOB.value.split('/'); 
var by = Number(d[2]); var bm = Number(d[1])-1; var bd = Number(d[0]); 
var bday = new Date(by,bm,bd) 
var age=0; var dif=bday; 

while(dif<today){ 
	var dif = new Date(by+age,bm,bd); 
	age++; 
} 
	age +=-2 ; 

if(age < 18 && age > 1){
	document.getElementById("dobPopup").style.display = canSee;
}

}


/////////////////////////////set hidden fields

function setHiddenField(selected,change){
	
   var selectedBox = selected;
   var changeField = change;
   
   if(selectedBox.checked == true){
	 changeField.value = "1";
	}else{
	  changeField.value = "2";
    }

}

