
function fillCategory(){
	var scale_model = document.registration.ScaleModel;
	// this function is used to fill the category list on load
	document.registration.ScaleModel.value = 'BarManager'; //default
	addOption(scale_model, "BarManager", "Bar Manager");
	addOption(scale_model, "CoinCounter", "Coin Counter");
	addOption(scale_model, "DrinkManager", "Drink Manager");
	addOption(scale_model, "Exceed", "Exceed");
	addOption(scale_model, "Exceedi", "Exceed i");
	addOption(scale_model, "ExceedW", "Exceed W");
	addOption(scale_model, "InkManager", "Ink Manager");
	addOption(scale_model, "Loadcell", "Load Cell");
	addOption(scale_model, "Loadcelldisplay", "Load Cell Display");
	addOption(scale_model, "Loadcelltester", "Load Cell Tester");
	addOption(scale_model, "Loadcellsimulator", "Load Cell Simulator");
	addOption(scale_model, "Parcel", "Parcel Scales");
	addOption(scale_model, "Plus", "Plus");
	addOption(scale_model, "Postage", "Postage/Porto+");
	addOption(scale_model, "PS1000", "PS1000");
	addOption(scale_model, "PS2000", "PS2000");
	addOption(scale_model, "PS2000QT", "PS2000 QT Pro");
	addOption(scale_model, "PS2000W", "Precise/PS2000W");
	addOption(scale_model, "PS3000", "PS3000");
	addOption(scale_model, "SE20", "SE20");
	addOption(scale_model, "SE25", "SE25");
	addOption(scale_model, "SE35", "SE35");
	addOption(scale_model, "USB", "USB Scales");
	SelectSubCat();
}

function SelectSubCat(){
// ON selection of category this function will work
	var scale_model = document.registration.ScaleModel;
	var sub_model = document.registration.SubScaleModel;
	
	removeAllOptions(sub_model);

//Coin Counter
	if(document.registration.ScaleModel.value == 'CoinCounter'){
		document.registration.SubScaleModel.value = 'Australia'; //default
		addOption(sub_model,"Australia", "Australia");
		addOption(sub_model,"Europe", "Europe");
		addOption(sub_model,"USA", "USA");
	}
//Exceed
	if(document.registration.ScaleModel.value == 'Exceed'){
		document.registration.SubScaleModel.value = 'Australia'; //default
		addOption(sub_model,"Australia", "Australia");
		addOption(sub_model,"Austria", "Austria");
		addOption(sub_model,"France", "France");
		addOption(sub_model,"Ireland", "Ireland");
		addOption(sub_model,"Germany", "Germany");
		addOption(sub_model,"Sweden", "Sweden");
		addOption(sub_model,"UK", "UK");
		addOption(sub_model,"USA", "USA");
	}
//Exceed i
	if(document.registration.ScaleModel.value == 'Exceedi'){
		document.registration.SubScaleModel.value = 'Ei5kg_05'; //default
		addOption(sub_model,"Ei5kg_05", "Exceed i 5kg/0.05g");
	}
//Exceed W
	if(document.registration.ScaleModel.value == 'ExceedW'){
		document.registration.SubScaleModel.value = 'EW500g_01'; //default
		addOption(sub_model,"EW500g_01", "Exceed W 500g/0.01g");
		addOption(sub_model,"EW500g_1", "Exceed W 500g/0.1g");
		addOption(sub_model,"EW5kg_05", "Exceed W 5kg/0.05g");
		addOption(sub_model,"EW10kg_1", "Exceed W 10kg/1g");
	}
//Ink Manager
	if(document.registration.ScaleModel.value == 'InkManager'){
		document.registration.SubScaleModel.value = 'Ink500g'; //default
		addOption(sub_model,"Ink500g", "Ink Manager 500g/0.02g");
	}
//Parcel Scales
	if(document.registration.ScaleModel.value == 'Parcel'){
		document.registration.SubScaleModel.value = 'Malaysia'; //default
		addOption(sub_model,"Malaysia", "Malaysia Parcel Scales");
	}	
//Plus
	if(document.registration.ScaleModel.value == 'Plus'){
		document.registration.SubScaleModel.value = 'P500g'; //default
		addOption(sub_model, "P500g", "Plus 500g");
		addOption(sub_model, "P5kg", "Plus 5kg");
		addOption(sub_model, "UPlus5lb", "USPS Plus 5lb");
		addOption(sub_model, "UPlus10lb", "USPS Plus 10lb");
	}
//Postage/Porto+
	if(document.registration.ScaleModel.value == 'Postage'){
		document.registration.SubScaleModel.value = 'Australia'; //default
		addOption(sub_model,"Australia", "Australia");
		addOption(sub_model,"France", "France");
		addOption(sub_model,"Germany", "Germany");
		addOption(sub_model,"USA", "USA");
	}
//PS1000
	if(document.registration.ScaleModel.value == 'PS1000'){
		document.registration.SubScaleModel.value = 'PS1000_3kg'; //default
		addOption(sub_model, "PS1000_3kg", "PS1000 3kg/10lb");
		addOption(sub_model, "PS1000_10kg", "PS1000 10kg");
	}
//PS2000
	if(document.registration.ScaleModel.value == 'PS2000'){
		document.registration.SubScaleModel.value = 'Australia'; //default
		addOption(sub_model,"Australia", "Australia");
		addOption(sub_model,"Austria", "Austria");
		addOption(sub_model,"Holland", "Holland");
		addOption(sub_model,"USA", "USA");
	}
//PS2000 QT Pro
	if(document.registration.ScaleModel.value == 'PS2000QT'){
		document.registration.SubScaleModel.disabled = true;
	}
	else if(document.registration.ScaleModel.value == 'BarManager' || document.registration.ScaleModel.value == 'DrinkManager' || document.registration.ScaleModel.value == 'Loadcell'){
		document.registration.SubScaleModel.disabled = true;
	}
	else if(document.registration.ScaleModel.value == 'Loadcelldisplay' || document.registration.ScaleModel.value == 'Loadcelltester' || document.registration.ScaleModel.value == 'Loadcellsimulator'){
		document.registration.SubScaleModel.disabled = true;
	}
	else{
		document.registration.SubScaleModel.disabled = false;
	}
//PS2000W
	if(document.registration.ScaleModel.value == 'PS2000W'){
		document.registration.SubScaleModel.value = 'PS2W500g_01'; //default
		addOption(sub_model,"PS2W500g_01", "PS2000W 500g/0.01g");
		addOption(sub_model,"PS2W1kg_05", "PS2000W 1kg/0.05g");
		addOption(sub_model,"PS2W5kg_05", "PS2000W 5kg/0.05g");
		addOption(sub_model,"PS2W10kg_1", "PS2000W 10kg/1g");
	}
//PS3000
	if(document.registration.ScaleModel.value == 'PS3000'){
		document.registration.SubScaleModel.value = 'Australia'; //default
		addOption(sub_model,"Australia", "Australia");
		addOption(sub_model,"Bahrain", "Bahrain");
		addOption(sub_model,"Germany", "Germany");
		addOption(sub_model,"UK", "UK");
		addOption(sub_model,"USA", "USA");
	}
//SE20, SE25, SE35 - only Ireland models for the meantime
	if(document.registration.ScaleModel.value == 'SE20' || document.registration.ScaleModel.value == 'SE25' || document.registration.ScaleModel.value == 'SE35'){
		document.registration.SubScaleModel.value = 'Ireland'; //default
		addOption(sub_model,"Ireland", "Ireland");
	}
//USB Scales
	if(document.registration.ScaleModel.value == 'USB'){
		document.registration.SubScaleModel.value = 'USB5lb'; //default
		addOption(sub_model, "USB5lb", "USB Plus 5lb");
		addOption(sub_model, "USB10lb", "USB Plus 10lb");
		addOption(sub_model, "USBPl5kg", "USB Plus 5kg");
		addOption(sub_model, "USBPS22lb", "USB PS2000 22lb");
		addOption(sub_model, "USBPS5kg", "USB PS2000 5kg");
		addOption(sub_model, "USB30lb", "USB Parcel 30lb");
		addOption(sub_model, "USB70lb", "USB Parcel 70lb");
		addOption(sub_model, "USB15kg", "USB Parcel 15kg");
		addOption(sub_model, "USB30kg", "USB Parcel 30kg");
	}
//check options for Model A, B, or ISP
	if(document.registration.ScaleModel.value == 'Postage' || document.registration.ScaleModel.value == 'PS3000'){
		document.registration.Model_AB[0].disabled = false;
		document.registration.Model_AB[1].disabled = false;
		document.registration.Model_AB[2].disabled = false;
		document.registration.Model_AB[3].disabled = false;	
	}
	else{
		document.registration.Model_AB[0].disabled = true;
		document.registration.Model_AB[1].disabled = true;
		document.registration.Model_AB[2].disabled = true;
		document.registration.Model_AB[3].disabled = true;
	}
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}

function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

	selectbox.options.add(optn);
}

function validate(){

	serial=document.registration.SerialNumber.value;
	fname=document.registration.FirstName.value;
	lname=document.registration.LastName.value;
	email=document.registration.EmailAddress.value;
	add=document.registration.Address.value;
	city=document.registration.City.value;
	state=document.registration.State.value;
	ctry=document.registration.Country.value;
	pcode=document.registration.Postcode.value;
	date=document.registration.Date_Purchased.value;
	model=document.registration.ScaleModel.value;
	elane=document.registration.Elane_Field.value;

//validate all
	if(isEmpty(serial) || isEmpty(fname) || isEmpty(lname) || isEmpty(email) || isEmpty(add) || isEmpty(city) || isEmpty(state) || isEmpty(ctry) || isEmpty(pcode) || isEmpty(date) || isEmpty(elane)){
	    alert('Please provide information for required fields.');
	    event.returnValue=false;
	}
	else if(!(elane=="elane")){
	    alert('Please provide information for required fields.');
	    event.returnValue=false;
	}
	else{

		//validate email address
		if(echeck(email)==false){
		    alert('Please provide your correct email address.');
		    event.returnValue=false;
		}
		
		//check model of scale if applicable - a, b, c
		else if(model == 'Postage' || model == 'PS2000' || model == 'PS3000' || model == 'SE25' || model == 'SE35'){
			if((document.registration.Model_AB[0].checked==false) && (document.registration.Model_AB[1].checked==false) && (document.registration.Model_AB[2].checked==false) && (document.registration.Model_AB[3].checked==false)){
			    alert('Please select whether your scale is Model A, Model B, Model ISP or not specified.');
			    event.returnValue=false;			
			}
		}
		
		else if(serial.length==8){ //check that all are digits
			if(isInteger(serial)!=true){
				alert('Please provide the correct serial number of your scales.');
				event.returnValue=false;
			}
		}
		else if(serial.length==9){
			temp=serial.substring(0,1); //parse first character
			//check if first character is letter
			if(!((temp >= "A" && temp <= "Z") || (temp >= "a" && temp <= "z"))){
				alert('Please provide the correct serial number of your scales.');
				event.returnValue=false;				
			}
			else{
				//now get the last 8 characters which should be digits
				temp=serial.substring(1);
				//8 characters must be digits
				if(isInteger(temp)!=true){
					alert('Please provide the correct serial number of your scales.');
					event.returnValue=false;
				}
			}
		}
		//not allowed if length is not 8 or 9
		else if(serial.length<8 || serial.length>9){ //8 or 9 digits
		    alert('Please provide the correct serial number of your scales.');
		    event.returnValue=false;
		}

		//validate date of purchase
		else if(isDate(date)==false){
			alert('Please provide the correct date of purchase.');
			event.returnValue=false;
		}
	}
}

function isEmpty(s){
	return ((s == null) || (s.length == 0))
}

function isDigit (c){
	return ((c >= "0") && (c <= "9"))
}
	
function isInteger (s){
	var i;

	// Search through string's characters one by one
   	// until we find a non-numeric character.
   	// When we do, return false; if we don't, return true.

   	for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


//function to check validity of email address
//from smartwebby.com
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }
 		 return true;				
}


//functions to validate date of purchase
//from smartwebby.com
function isDate(dtStr){

	var currentTime = new Date();
	var dtCh= "-";
	var minYear=1995;
	var maxYear=currentTime.getFullYear();
	var maxMonth = currentTime.getMonth() + 1;
	var maxDay = currentTime.getDate();

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=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){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	if(year==maxYear){
		if(month>maxMonth){
			return false;
		}
		if(month==maxMonth && day>maxDay){
			return false;
		}
	}
	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;
}
