// ************************************
//	
//		EEP JAVASCRIPT DOCUMENT
//
//		By: BGH Studios
//		Date: 2/11/2009
//
// ************************************


	// ************************************
	// STANDARD UNIVERSAL FUNCTIONS
	// ************************************

	function isChecked( groupName, parentForm) {
		for( var i=0, limit = parentForm[groupName].length; i < limit; ++i) {
			if(parentForm[groupName][i].checked)
			return true;
		}
		return false;
	}

	var alertEmailString = "You must enter a valid E-Mail Address to continue.";
	
	function EmailCheck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert(alertEmailString);
		   return false;
		}
	
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(alertEmailString);
		   return false;
		}
	
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert(alertEmailString);
			return false;
		}
	
		 if (str.indexOf(at,(lat+1))!=-1){
		   alert(alertEmailString);
			return false;
		 }
	
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert(alertEmailString);
			return false;
		 }
	
		 if (str.indexOf(dot,(lat+2))==-1){
		   alert(alertEmailString);
			return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert(alertEmailString);
			return false;
		 }
	
		 return true					
	}

	function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}
	
	function winUnload(){
		confirm("You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?");
	}
	
	function Confirm_DeleteMeasure() {
		
		
		var answer = confirm("Are you sure you want to delete this measure?");
		if (answer)
			return true;
		else
			return false
		
	}
	
	
	// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	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<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("The 'Installation Date' field must be a valid date to continue.")
			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("The 'Installation Date' field must be a valid date to continue.")
			return false
		}
	return true
	}
	
	
	function EEP_RebateExtension_Validate() {
		var theForm = document.EEP_RebateExtension;
		
		// CONFIRM NUMBER
		if (theForm.ConfirmationNum.value == "") {
			alert("You must insert a value into the 'Confirmation Number' field to continue.");
			theForm.ConfirmationNum.focus();
			return false;
		}
		
		// REASON FOR EXTENSION
		if (theForm.reason_extension.value == "") {
			alert("You must insert a value into the 'Reason for extension' field to continue.");
			theForm.reason_extension.focus();
			return false;
		}
		
		var InstallDate_mm = theForm.install_mm[theForm.install_mm.selectedIndex].value;
		var InstallDate_dd = theForm.install_dd[theForm.install_dd.selectedIndex].value;
		var InstallDate_yy = theForm.install_yy[theForm.install_yy.selectedIndex].value;
		var InstallDate = InstallDate_mm + "/" + InstallDate_dd + "/" + InstallDate_yy;
				
		if (isDate(InstallDate) == false) {
			return false
		}
			
		// NAME
		if (theForm.name.value == "") {
			alert("You must insert a value into the 'Your Name' field to continue.");
			theForm.name.focus();
			return false;
		}
		
		// CONTACT EMAIL
		if (theForm.email.value == "") {
			alert("You must insert a value into the 'Email' field to continue.");
			theForm.email.focus();
			return false;
		} else {
			if ((theForm.email.value==null) || (theForm.email.value=="")) {
				alert(alertEmailString);
				theForm.email.focus();
				return false;
			}
			if (EmailCheck(theForm.email.value) == false){
				theForm.email.focus();
				theForm.email.select();
				return false;
			}
		}
		
		
		// CUSTOMER PHONE (PART 1/3)
		if (theForm.contact_ph1.value == "") {
			alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
			theForm.contact_ph1.focus();
			return false;
		} else {
			if (isNaN(theForm.contact_ph1.value) == true) {
				alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
				theForm.contact_ph1.focus();
				theForm.contact_ph1.select();
				return false;
			} else {
				if (theForm.contact_ph1.value.length < 3) {
					alert("The value in the 'Phone (Area Code)' field must be 3 characters long.");
					theForm.contact_ph1.focus();
					return false;
				}
			}
		}
		
		// CUSTOMER PHONE (PART 2/3)
		if (theForm.contact_ph2.value == "") {
			alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
			theForm.contact_ph2.focus();
			return false;
		} else {
			if (isNaN(theForm.contact_ph2.value) == true) {
				alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
				theForm.contact_ph2.focus();
				theForm.contact_ph2.select();
				return false;
			} else {
				if (theForm.contact_ph2.value.length < 3) {
					alert("The value in the 'Phone (Area Code)' field must be 3 characters long.");
					theForm.contact_ph2.focus();
					return false;
				}
			}
		}
		
		// CUSTOMER PHONE (PART 3/3)
		if (theForm.contact_ph3.value == "") {
			alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
			theForm.contact_ph3.focus();
			return false;
		} else {
			if (isNaN(theForm.contact_ph3.value) == true) {
				alert("Please insert a numeric value into the 'Phone (Area Code)' field.");
				theForm.contact_ph3.focus();
				theForm.contact_ph3.select();
				return false;
			} else {
				if (theForm.contact_ph3.value.length < 3) {
					alert("The value in the 'Phone (Area Code)' field must be 3 characters long.");
					theForm.contact_ph3.focus();
					return false;
				}
			}
		}
		
	}
	
	function Admin_RebateExtensionDeny(Type,Alerts) {
		switch(Type) {
			case "c":
				var theForm = document.Cust_Extension;
				break;
			case "p":
				var theForm = document.Pres_Extension;
				break;
		}
	
		
		var ext_denyreason = document.getElementById('ext_denyreason');
		if (ext_denyreason) {
			for (var i=0; i < theForm.extension_approvedeny.length; i++) {
				if(theForm.extension_approvedeny[i].checked) {
					switch(theForm.extension_approvedeny[i].value) {
						case "0":
							ext_denyreason.disabled = true;
							ext_denyreason.value = "";
							ext_denyreason.style.backgroundColor = "#cfcfcf";
							break;
						case "1":
							if (Alerts) {
								alert("Note: When choosing to Deny a Rebate Extension, you will need to fill in the reason the extension was denied.");
							}
							ext_denyreason.disabled = false;
							ext_denyreason.style.backgroundColor = "#ffffff";
							break;
					}
				}
			}
		}
	}
	
	function Validate_RebateExtension(Type) {
		switch(Type) {
			case "c":
				var formName = "CustRebate_View";
				var theForm = document.Cust_Extension;
				break;
			case "p":
				var formName = "Pres_Extension";
				var theForm = document.Pres_Extension;
				
				break;
		}
		
		var ext_denyreason = document.getElementById('ext_denyreason');
		approveDeny = theForm.extension_approvedeny;
		approveDenyLen = approveDeny.length
		for(var i = 0; i < approveDenyLen; i++) {
			if(approveDeny[i].checked) {
				if (approveDeny[i].value == "1") {
					if (theForm.ext_denyreason.value == "") {
						alert("You must fill out a reason the Rebate Extension was denied.");
						theForm.ext_denyreason.focus();
						return false;
					}
				}
			}
		}
		
		// EXTENDED DATE
		if (theForm.extension_date.value == "") {
			alert("Please insert a numeric value into the 'Extend Rebate by Days' field.");
			theForm.extension_date.focus();
			return false;
		} else {
			if (isNaN(theForm.extension_date.value) == true) {
				alert("Please insert a numeric value into the 'Extend Rebate by Days' field.");
				theForm.extension_date.focus();
				theForm.extension_date.select();
				return false;
			} else {
				if (parseInt(theForm.extension_date.value) > 60) {
					alert("The value in the 'Extend Rebate by Days' field must be no more than 60 Days.");
					theForm.extension_date.focus();
					return false;
				}
			}
		}		
	}
	
	
	function returnStatusType(Type,preValue,postValue) {
		switch(Type) {
			case "c":
				var theForm = document.EEP_CustRebate_View;
				break;
			case "p":
				var theForm = document.EEP_PresRebate_View;
				break;	
		}
		
		
		// WAITING ON PRE-INSTALLATION INSPECTION
		if ((preValue == true) || (theForm.rebate_status[theForm.rebate_status.selectedIndex].value == "16")) {
			theForm.pre_installDate_mm.disabled = false;
			theForm.pre_installDate_dd.disabled = false;
			theForm.pre_installDate_yyyy.disabled = false;
		} else {
			theForm.pre_installDate_mm.disabled = true;
			theForm.pre_installDate_dd.disabled = true;
			theForm.pre_installDate_yyyy.disabled = true;
		}
		
		// FUNDS NOT RESERVED
		if (theForm.rebate_status[theForm.rebate_status.selectedIndex].value == "4") {
			alert("Note: When 'Funds Not Reserved' is selected as a status, you will need to fill in the reason the rebate was denied.");
			theForm.deny_reason.disabled = false;
			theForm.deny_reason.style.backgroundColor = "";
			theForm.deny_reason.focus();
		} else {
			theForm.deny_reason.disabled = true;
			theForm.deny_reason.style.backgroundColor = "#cfcfcf";
		}
		
		// WAITING ON POST-INSTALLATION INSPECTION
		if ((postValue == true) || (theForm.rebate_status[theForm.rebate_status.selectedIndex].value == "17")) {
			theForm.post_installDate_mm.disabled = false;
			theForm.post_installDate_dd.disabled = false;
			theForm.post_installDate_yyyy.disabled = false;
		} else {
			theForm.post_installDate_mm.disabled = true;
			theForm.post_installDate_dd.disabled = true;
			theForm.post_installDate_yyyy.disabled = true;
		}
		
		
				
	}
	
	function validateForm(formName) {
	
		switch(formName) {
			case "ChannelPartners":
				var p = 0;
				
				var a = new Array();
				a = document.getElementsByName("programs[]");
				for(i=0;i<a.length;i++){
					if(a[i].checked){
						p=1;
					}
				}
				
				var b = new Array();
				b = document.getElementsByName("partners[]");
				for(i=0;i<b.length;i++){
					if(b[i].checked){
						p=1;
					}
				}
				
				if (p==0){
					alert("You must choose a 'Program' or 'Partner Type' to continue.");
					return false;
				}
			
				break;
		}
		
	}
	
	function validate_DocumentUpload() {
		var theForm = document.UploadForm;
		var p = 0;
		var a = new Array();
		a = document.getElementsByName("documentTypes[]");
		
		
		// CONTACT EMAIL
		if (theForm.email.value == "") {
			alert("You must insert a value into the 'Email' field to continue.");
			theForm.email.focus();
			return false;
		} else {
			if ((theForm.email.value==null) || (theForm.email.value=="")) {
				alert(alertEmailString);
				theForm.email.focus();
				return false;
			}
			if (EmailCheck(theForm.email.value) == false){
				theForm.email.focus();
				theForm.email.select();
				return false;
			}
		}
		
		if (theForm.other.value == "") {
			for(i=0;i<a.length;i++){
				if(a[i].checked){
					p=1;
				}
			}
			if (p==0){
				alert("You must fill check a Document Type or fill out the Other Box.");
				return false;
			}
		}
		
		if (theForm.fileToUpload.value != "") {
			checkExt(theForm);
		} else {
			alert("You must choose a file to upload before you can continue.");
			theForm.fileToUpload.focus();
			return false;
		}
		
		return true;
	
	}
	
	function checkExt(formObj){
		var filename = formObj.fileToUpload.value;
		var filelength = parseInt(filename.length) - 3;
		var fileext = filename.substring(filelength,filelength + 3);
		
		// Check file extenstion
		if (fileext != "gif" && fileext != "jpg" && fileext != "JPG" && fileext != "pdf"){
			alert ("Only files in JPG, GIF and PDF format will be accepted.");
			formObj.fileToUpload.focus();
			return false;
		}
	}
	
	function ConfirmDelete(type) {
		var answer = confirm("Are you sure you want to Delete this " + type +"?\n\n (This action can NOT be undone)")
		if (answer){
			return true;
		} else {
			return false;
		}
	}