/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=0000;
var maxYear=2100;

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 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){
		alert("The date format should be MM/DD/YYYY.")
		return false
	}
	if (strMonth.length<1 || month<0 || month>12){ // MODIFIED to allow month 0
		alert("Please enter a valid month.")
		return false
	}
	if (strDay.length<1 || day<0 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){ // MODIFIED to allow day 0
		alert("Please enter a valid day.")
		return false
	}
	if (strYear.length != 4 || year<minYear || year>maxYear){ // MODIFIED to allow year 0
		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 ValidateForm(){

//trim spaces off all form inputs
	var hullid=document.getElementById('hull_id');
	var inputs=document.getElementsByTagName('input');
	for (var i=0; i<inputs.length; i++) {
		try {
			inputs[i].value = trim(inputs[i].value);
		} catch(e) {
			//alert(e);
			//do nothing
		}
	}

//compute certification string from hull ID
	try{
		document.getElementById('form_certify').value = hex_md5(hullid.value);
	}
	catch(e){
		alert(e);
		return false;
	}

	
//validate hull id
	if ((hullid.value) && ((IsNumeric(hullid.value)==false) || (hullid.value>99999) || (hullid.value<10000))) {
		alert("Hull ID must be a five-digit number")
		hullid.focus()
		return false
	}
	
//validate dates
	var date1=document.getElementById('shipping_date')
	var date2=document.getElementById('date_acquired')
	if (date1.value) {
		if (isDate(date1.value)==false){
			date1.focus()
			date1.select()
			return false
		}
	}
	if (date2.value) {
		if (isDate(date2.value)==false){
			date2.focus()
			date2.select()
			return false
		}
	}
	
//validate email address
	var email=document.getElementById('owner_email')
	if(email.value != ""){
		var goodEmail = email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
		if (!goodEmail) {
		   alert('Please enter a valid e-mail address.')
		   email.focus()
		   email.select()
		   return false
		}
	}
	return true
 }

function IsNumeric(strString) {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function switchmodel() {
	document.getElementById("ignition_switch_model").value = document.getElementById("ignition_switch_model_list").value; 
	if(document.getElementById("ignition_switch_model_list").value==""){
		alert("If you know your ignition switch model number and it doesn't appear in this list, please enter it manually.");
		document.getElementById("ignition_switch_model").style.visibility = "visible";
		document.getElementById("ignition_switch_model").focus();
	} else {
		document.getElementById("ignition_switch_model").style.visibility = "hidden";
	}	
}

function notifycustom(formobj, item) {
	if(formobj.value=="" || formobj.value=="Special Order" || formobj.value=="Custom/Unknown" || formobj.value=="Custom"){
		alert("If you have a custom " + item + ", please indicate it in the Additional Comments box below.");
		document.getElementById("comments").focus();
	}
}
