//sets the option selected for the select box
function SelTheOption(SelName, OptionValue, frm){
	OptLen = eval("document." + frm + "." + SelName + ".length")
	for (i = 0 ; i < OptLen; i++){
		OptSel = eval("document." + frm + "." + SelName + ".options[i].value")
		if (OptSel == OptionValue){
			eval("document." + frm + "." + SelName + ".options[i].selected = true")
			eval("document." + frm + "." + SelName + ".options[i].defaultSelected = true")
			break
		}
	}
}

//Check if the value is numeric or not
function isNumeric(StrChkVal){
	RetVal = true
	NumberOfPoints = 0
	for (i = 0 ; i < StrChkVal.length ; i++){
		if (mid(StrChkVal, i + 1, 1) == "."){
			if (NumberOfPoints == 0){
				NumberOfPoints = 1
			}else{
				RetVal = false
			}
		}else{
			if (isNaN(parseInt(mid(StrChkVal, i + 1, 1), 10))){
				RetVal = false
			}
		}
	}
	return RetVal
}

// Function to trim a string sent and return the resultant string 
function StrTrim(StrVar){
	for (I = 0 ; I < StrVar.length ; I++){
		if ((StrVar.substring(0, 1)) == " "){
			StrVar = StrVar.substring(1, StrVar.length)
			I = I - 1
		}
	}
	for (I = 0 ; I < StrVar.length ; I++){
		if ((StrVar.substring(StrVar.length - 1, StrVar.length - 0)) == " "){
			StrVar = StrVar.substring(0, StrVar.length - 1)
			I = I - 1
		}
	}
	return StrVar
}

//returns a substring of str starting at 'start' that's n characters long.
function mid(str, start, n){
	strlen = str.length
	var jj = str.substring(start - 1, strlen)
	jj = jj.substring(0, n)
	return jj
}

//check if the date is correct or not
function Checkdate(datestr){
	var flag = false
	if (datestr.length == 10){
		firstsl = datestr.indexOf("/")
		secondsl = datestr.indexOf("/", firstsl + 1)
		var mm = (mid(datestr, 1 , firstsl)).valueOf()
		var dd = (mid(datestr, firstsl + 2, secondsl - (firstsl + 1))).valueOf()
		var yy = (mid(datestr, secondsl + 2, datestr.length - secondsl)).valueOf()

		if ((firstsl != -1) && (secondsl != -1)){
			if ((firstsl == 2) || (firstsl == 1)){
				if ((secondsl == 4) || (secondsl == 5) || (secondsl ==3) && ((secondsl - firstsl) > 1)){
					if (((datestr.length - secondsl) == 3) || ((datestr.length - secondsl) == 5)){	
						if (!(isNumeric(mm) && isNumeric(dd) && isNumeric(yy))){
							return flag
						}else{
							if (parseInt(mm, 10) < 1 || parseInt(mm, 10) > 12){
								mm = Number.NaN
								return flag
							}
							if (parseInt(dd, 10) < 1 || (parseInt(dd, 10) > getDays((parseInt(mm, 10) - 1 ),parseInt(yy, 10)))){
								dd = Number.NaN
								return flag
							}
							flag = true
							return flag
						}
					}
				}
			}
		}
	}else{
		//alert("Invalid Date.")
	}
	return flag
}

//returns no of days between two date
function TOTDAYS(FirstVal, SecondVal){
	retDays = "False"
	if (Checkdate(FirstVal) == true && Checkdate(SecondVal) == true){
		FirstVald = DateObj(FirstVal)
        SecondVald = DateObj(SecondVal)
		RetVar = daysBetween(FirstVald, SecondVald)
		retDays = RetVar.toString()
	}	
	return retDays
}

//returns the number of days between two date objects.
function daysBetween(date1, date2){	
	//returns the number of days between two date objects.
	millisecs = date1.getTime() - date2.getTime() 
	return Math.round(millisecs / (1000 * 60 * 60 * 24) - 0.5)
}

//returns date string as date object
function DateObj(datestr){
	firstsl  = datestr.indexOf("/")
	secondsl = datestr.indexOf("/", firstsl + 1)
	var mm = mid(datestr, 1 , firstsl)
	var dd = mid(datestr, firstsl + 2, secondsl - (firstsl + 1))
	var yy = mid(datestr, secondsl + 2, datestr.length - secondsl)
	mnth = getmonth1(mm)
	try1 = mnth + " " + dd + ", " + yy
	date1 = new Date (try1)
	return date1 
}

//Function returns monthname from string
function getmonth1(mmstr){

	xx = mmstr
	nm = null
	if (xx == 1){
		nm = "January"
	}
	if (xx == 2){
		nm = "February"
	}
	if (xx == 3){
		nm = "March"
	}
	if (xx == 4){
		nm = "April"
	}
	if (xx == 5){
		nm = "May"
	}
	if (xx == 6){
		nm = "June"
	}
	if (xx == 7){
		nm = "July"
	}
	if (xx == 8){
		nm = "August"
	}
	if (xx == 9){
		nm = "September"
	}
	if (xx == 10){
		nm = "October"
	}
	if (xx == 11){
		nm = "November"
	}
	if (xx == 12){
		nm = "December"
	}
	return nm
}

//Function checks for leap year
function leapyear(year){
	Retval = false
	if (year % 4 == 0){
		Retval = true
	}
	return Retval
}

//get days of a month and year
function getDays(month,year){
	var ar = new Array(12)
	ar[0] = 31
	ar[1] = (leapyear(year))?29:28
	ar[2] = 31
	ar[3] = 30
	ar[4] = 31
	ar[5] = 30
	ar[6] = 31
	ar[7] = 31
	ar[8] = 30
	ar[9] = 31
	ar[10] = 30
	ar[11] = 31
	return ar[month]
}

//opens calender in a new window                                       
function opencal(frm, fldname){
	URL = "../Content/includes/calendar.asp?varname=" + fldname +"&frm="+frm
	CalWin = window.open(URL, 'calwin', 'scrollbars=yes, resizable=0, menubar=no, width=260, height=240')
	CalWin.focus()
}

//opens calender in a new window  for the main site for form 1                                     
function opencalMain(frm, fldname){
	URL = "/content/includes/calendar.asp?varname=" + fldname +"&frm="+frm
	CalMain = window.open(URL, 'CalMain', 'scrollbars=yes, resizable=0, menubar=no, width=260, height=240')
	CalMain.focus()
}


//Date Comparison based on date difference
function dateComp(dt1, dt2){
	var d1 = new Date(mid(dt1, 7, 4), mid(dt1, 4, 2) - 1, mid(dt1, 1, 2))
	var d2 = new Date(mid(dt2, 7, 4), mid(dt2, 4, 2) - 1, mid(dt2, 1, 2))
	var f = daysBetween(d2, d1)
	   
	if (f > 0){
		return 1
	}
	else if(f == 0){
		return 0
	}
	else{ 
		return -1      
	} 
}

//check if the email is in correct format or not
function emailCheck(frmname, ctrlname){

	var emailStr = eval("document."+frmname+"."+ctrlname).value;
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null){
		alert("Email address seems incorrect (check @ and .'s)");
		eval("document."+frmname+"."+ctrlname).focus();
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i = 0; i < user.length; i++){
		if (user.charCodeAt(i) > 127){
			alert("Ths username contains invalid characters.");
			eval("document."+frmname+"."+ctrlname).focus();
			return false;
		}
	}

	for (i = 0; i < domain.length; i++){
		if (domain.charCodeAt(i) > 127){
			alert("Ths domain name contains invalid characters.");
			eval("document."+frmname+"."+ctrlname).focus();
			return false;
		}
	}

	// See if "user" is valid 
	if (user.match(userPat) == null){
		// user is not valid
		alert("The username doesn't seem to be valid.");
		eval("document."+frmname+"."+ctrlname).focus();
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray = domain.match(ipDomainPat);

	if (IPArray != null){

		// this is an IP address
		for (var i = 1; i <= 4; i++){
			if (IPArray[i] > 255){
				alert("Destination IP address is invalid!");
				eval("document."+frmname+"."+ctrlname).focus();
				return false;
			}
		}	
		return true;
	}

	// Domain is symbolic name.  Check if it's valid.
		 
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;

	for (i = 0; i < len; i++){
		if (domArr[i].search(atomPat) == -1){
			alert("The domain name does not seem to be valid.");
			eval("document."+frmname+"."+ctrlname).focus();
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1){
		alert("The address must end in a well-known domain or two letter " + "country.");
		eval("document."+frmname+"."+ctrlname).focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	if (len < 2){
		alert("This address is missing a hostname!");
		eval("document."+frmname+"."+ctrlname).focus();
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}//end of emailCheck function
	
//function to disable text box 
function Disable(ctrlName){
	ctrlName.blur();
}

//function to check if the 
//filename is greater than 50 or not
function ImageFileName(frmName, cntlName){
	var strimage;
	var myimage;
	var filename;
	var strlength;
	strimage = eval("document." + frmName + "." + cntlName + ".value");
	strlength = strimage.length 
	myimage = strimage.lastIndexOf("\\");
	if (myimage != -1){
		filename = strimage.substring(myimage + 1, strlength)
		if (filename.length > 50){
			alert("The name of the file you want to upload should not exceed 50 characters.");
			var objFile = eval("document." + frmName + "." + cntlName);
			objFile.select();
			objFile.focus();
			return false;
		}
	}
	else {
		if (strlength > 50){
			alert("The name of the file you want to upload should not exceed 50 characters.");
			var objFile = eval("document." + frmName + "." + cntlName);
			objFile.select();
			objFile.focus();
			return false;
		}
	}
	return true;
}

//#####
//opens calender in a new window
function openFlashDemo()
	{
		var URLFlash;
		var FlashDemo;

		URLFlash = "http://www.foxelectricsupply.com/content/media/images/home/Fox Electric Sequence_LargeSm_Prog001.swf";
		//alert(URLFlash);
		FlashDemo = window.open(URLFlash, 'FlashDemo', 'left=220 top=140 scrollbars=no, resizable=1, menubar=no, width=300, height=225');
		FlashDemo.focus();
	}
//#####
