var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var inUse = "";

function openHomeQuote() {
	var url = "http://streamline.naqtechnology.com.au?broker=aceirm&subclassid=building&singleframe=yes";
	var popUp = window.open(url, 'homeQuote', 'resizable=no, dependent=yes, status=no, toolbar=no, scrollbars=yes, location=no');
   	popUp.focus();
}

function openMotorQuote() {
	var url = "http://streamline.naqtechnology.com.au?broker=aceirm&subclassid=motor&singleframe=yes";
	var popUp = window.open(url, 'motorQuote', 'resizable=no, dependent=yes, status=no, toolbar=no, scrollbars=yes, location=no');
   	popUp.focus();
}

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 format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function validateDate(ele){
	if(inUse == "date" || inUse == "") {
		inUse = "date";
		var dt=document.getElementById(ele);
		if (isDate(dt.value)==false){
			dt.focus()
			//alert("Date is not in correct format dd/mm/yyyy");
		}
		else {
			inUse = "";
		}
	}
}

function checkNumber(ele) {
	if(inUse == "checkNum" || inUse == "") {
		var dom = document.getElementById(ele);
		if(dom.value == "" || isInteger(dom.value) == false) {
			alert("Value must be a number");
			dom.focus();
		}
		else {
			inUse = "";
		}
	}
}

function checkPhoneNumber(ele) {
	if(inUse == "checkPhoneNum" || inUse == "") {
		inUse = "checkPhoneNum";
		var dom = document.getElementById(ele);
		if(dom.value == "" || isInteger(dom.value) == false) {
			alert("Value must be a valid 10 digit number, Must include area code.");
			dom.focus();
		}
		else {
			inUse = "";
		}
	}
}

function validateLicence(ele) {
	if(inUse == "lic" || inUse == "") {
		inUse = "lic";
		var dom = document.getElementById(ele);
		if(isInteger(dom.value) == false) {
			alert("Licence number is not in correct numeric format, must only contain numbers.");
			dom.focus();
		}
		else if(dom.value.length != 8) {
			alert("Licence number is not correct length, Must be 8 digits long.");
			dom.focus();
		}
		else {
			inUse = "";
		}
	}		
}

function checkYear(ele) {
	if(inUse == "year" || inUse == "") {
		inUse = "year";
		var dom = document.getElementById(ele);
		if(isInteger(dom.value) == false || (dom.value < minYear || dom.value > maxYear)) {
			alert("Must be a valid 4 digit year between 1900 and 2100.");
			dom.focus();
			return;
		}
		inUse = "";
	}	
}

function checkRegistration(ele) {
	if(inUse == "rego" || inUse == "") {
		inUse = "rego";
		var dom = document.getElementById(ele);
		if(dom.value.length <= 7) {
			inUse = "";
			return;			
		}
		alert("Registration number number is correct.");
		dom.focus();
	}
}

function isCurrency(ele) {
	if(inUse == "curr" || inUse == "") {
		inUse = "curr";
		var dom = document.getElementById(ele);
		var i = 0;
		if(dom.value != "") {
			for(i = 0; i < dom.value.length; i++) {
				if(isInteger(dom.value.charAt(i)) == false && dom.value.charAt(i) != ".") {
					alert("Dollar value is in incorrect format. Please remove any alphanumeric characters you have eg. abc or $.\nDecimals (.) allowed.");
					dom.focus();
					return false;
				}
			}
		}
		else {
			alert("Dollar value is in incorrect format. Please remove any alphanumeric characters you have eg. abc or $.\nDecimals (.) allowed.");
			dom.focus();
			return false;
		}
		inUse = "";
		return true;
	}	
}

function totalAccessories(ele, e) {
	var i = 0, totalCost = 0;
	var dom;
	for(i = 1; i < e; i++) {
		dom = document.getElementById("cost"+i+ele);
		if(dom.value != "Value") {
			if(isCurrency(dom.id)) {
				totalCost = totalCost+parseFloat(dom.value);
			}		
		}
	}
	document.getElementById("accessValue").value = "$"+totalCost;
}

function checkracqNumber(ele) {
	if(inUse == "racq" || inUse == "") {
		inUse = "racq";
		var i = 0;
		var dom = document.getElementById(ele);
		if(isInteger(dom.value) == true && dom.value.length == 16) {
			inUse = "";
			return;
		}
		alert("RACQ membership number not correct. This number is 16 digits in length and found on your membership card.");
		dom.focus();
	}	
}

function checkEmailAddress(ele) {
	if(inUse == "email" || inUse == "") {
		inUse = "email";
		var dom = document.getElementById(ele);
		if(dom.value.indexOf("@") == -1 || dom.value.substr(dom.value.indexOf("@"), dom.value.length).indexOf(".") == -1) {
			alert("You have typed an invalid email address, an email address must contain a '@' followed by a least one '.'");
			dom.focus();
			return;
		}
		inUse = "";
	}
}

function isPostCode(ele) {
	if(inUse == "pCode" || inUse == "") {
		inUse = "pCode";
		var dom = document.getElementById(ele);
		if(isInteger(dom.value) == false || dom.value == "" || dom.value.length > 4) {
			alert("You have typed an invalid Australian postcode. Please try again");
			dom.focus();
			return;			
		}
		inUse = "";
	}
}


