// All scripts by or modified by DJR
var rePhone = /^\d{3}-\d{3}-\d{4}$/;

function goBack(the_path, display_message) {
// Version 2.0.1
// note that the format should be path - host root
var cgi = new Object();
var p = the_path;
var m = display_message;
var msg = "Thank You for Your Submission";
cgi.http_host = location.host;
// handle a no argument invocation 	
	if (p == null)
	{
		// set p to a default value in this case the target is the root index.htm
		p = "/index.htm";
		window.location.href = "http://" + cgi.http_host + p;
		// display a page-defined message if directed
		// pass 0 into function in order to prevent msg	
			if (m != 0) 
			{
				alert(msg);
			}	
			else
			{
				//do nothing
			}
	}
	else
	{	
		window.location.href = "http://" + cgi.http_host + p;
		if (m != 0) 
			{
				alert(msg);
			}	
			else
			{
				//do nothing
			}
	}
// now reload the calling window
// window.location.reload(true);
}

// DJR Alternative close method due to pop-ups
// this method will have the same signature as goBack but will have diff behavior
function goBack2 (display_message) {
	var m = display_message;
	var msg = "Thank You for Your Submission";
	
	// alert thank you
 	if(m != 0) alert(msg);

	 //window.opener.location.reload(true);

	//window.close(); // close the child popup
	self.close();
}

function PowerPartner() {
obj=document.forms[0];
/// require at least one radio button be selected
var radioSelected = false;
var msg = "Thank You for Your Submission";
for (i = 0;  i < obj.act_type.length;  i++)
{
if (obj.act_type[i].checked)
radioSelected = true;
}
if (!radioSelected)
{
alert("Please select for your home or for your business");
return (false);
 }
if ((obj.act_type[0].checked) && (obj.Company_Name.value == ""))
{
alert("Please enter business/company name");
return (false);
 }
// check first name
if (obj.fname.value == "")
 {
 alert("Please enter a first name");
 return (false);  
 }    
// check last name 
if (obj.lname.value == "")
 {
 alert("Please enter a last name");
 return (false);  
 }    
// check address
if (obj.address.value == "")
 {
 alert("Please enter an address");
 return (false);  
 }    
// check city
if (obj.city.value == "")
 {
 alert("Please enter a city name");
 return (false);  
 }    
// check for phone 
if ((obj.phone.value == "") && (obj.phone2.value == ""))
 {
 alert("Please enter a phone number");
 return (false);  
 }    
}

// this is for the OR3 application entry form
function CheckIt() {
obj=document.forms[0];
	 if ((obj.ReqFname.value=="N/A" ) && (obj.ReqLname.value=="N/A" ) && (obj.ReqOrg.value=="N/A" ))
	{
 	alert("Please Indicate Requestor or Organizational name");
	return (false)
	}		
	if (obj.ReqInfo.value=="")
	 	{
	alert("Please Indicate the Information Requested") 	
	return (false)
		}
//	 alert(obj.ReqReleased[0].checked + ":" + obj.ReqCR.options[obj.ReqCR.selectedIndex].value + ":")
//	alert(obj.ReqCR.options[obj.ReqCR.selectedIndex].value) --->
	if (obj.ReqReleased[0].checked==false) {
//	alert(obj.ReqReleased[0].checked + ":" + obj.ReqCR.options[obj.ReqCR.selectedIndex].value + ":") --->
	if (obj.ReqCR.options[obj.ReqCR.selectedIndex].value=="") {
	alert("Please Select a Competitive Reason for Denial")	
	return (false);
	}
}

//	if (obj.ReqCR.options[obj.ReqCR.selectedIndex].value =="")
//			{
//	alert("Please Select a Competitive Reason for Denial")
//	return (false)
//		} 

		if (obj.SubFname.value=="" ) 
				{
		alert("Please Indicate Submitters' First name")
		return (false)
				}
		if (obj.SubLname.value=="" ) 
					{
		alert("Please Indicate Submitters' Last name")		
		return (false)				
					}
}		

// -DJR also make this a function that can be called 
function SubmitFormTo (path) {
var cgi = new Object();
cgi.http_host = location.host;
var path = "http://" + cgi.http_host + path;
document.forms[0].submit(path);
// alert(path);
// return path;
}


function CheckCommercial() {
obj=document.forms[0];


// check if name field is blank
if (obj.name.value == "")
{
alert("Please enter a company name");
obj.name.focus();
return (false);
}
// check if address field is blank
if (obj.address_r.value == "")
{
alert("Please enter a facility address");
obj.address_r.focus();
return (false);
}
// check if city is blank
if (obj.city.value == "")
{
alert("Please enter a facility city");
obj.city.focus();
return (false);
}
// check zip for blank
if (obj.zip.value == "")
{
alert("Please enter a valid 5 digit ZIP code");
obj.zip.focus();
return (false);
}
// check to make sure ZIP contains only numbers 
var checkOK = "0123456789";
var checkStr = obj.zip.value;
var allValid = true;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numeric characters in the 5 digit ZIP code field.");
obj.zip.focus();
return (false);
}
// require that there be at least 5 numeric carachters in ZIP field
if (obj.zip.value.length < 5)
{
alert("Please enter at least 5 characters in the ZIP code.");
obj.zip.focus();
return (false);
}
// check to make sure that the AE account number is not blank
if (obj.ae_account.value == "")
{
alert("Please enter your 7 digit Austin Energy account number");
obj.ae_account.focus();
return (false);
}
// check that the account number is comprised of numeric characters only
var checkOK = "0123456789";
/*
DJR the intention of this script was to provide generic validation for any/all AE forms
*/

var checkStr = obj.ae_account.value;
var allValid = true;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numeric characters in the Austin Energy account number field.");
obj.ae_account.focus();
return (false);
}
// insure that there are 7 numbers 
if (obj.ae_account.value.length < 7)
{
alert("Please enter your 7 digit Austin Energy account number");
obj.ae_account.focus();
return (false);
}
// check if contact name field is not blank
if (obj.con_name.value == "")
{
	alert("Please provide a contact name");
	obj.con_name.focus();
	return (false);
}
// check the email address is not blank
if (obj.con_email.value == "")
{
	alert("Please provide a valid email address");
	obj.con_email.focus();
	return (false);
}


if((obj.con_phone.value != null) && (obj.con_phone.value.length > 0)){
	if(!rePhone.test(obj.con_phone.value)){ 
		alert("Please provide a valid phone number.");
		obj.con_phone.focus();			
		return (false);
	}
}

if((obj.con_fax.value != null) && (obj.con_fax.value.length > 0)){
	if(!rePhone.test(obj.con_fax.value)){
		alert("Please provide a valid fax number.");
		obj.con_fax.focus();		
		return false;
	}
}


if (!(obj.building_stat[0].checked || obj.building_stat[1].checked))
{
	alert("Please enter a building condition");
	//obj.building_stat.focus();
	return (false);
}

if (!(obj.building_stat_ownership[0].checked || obj.building_stat_ownership[1].checked))
{
	alert("Please enter a Own/lease status");
	//obj.building_stat_ownership.focus();
	return (false);
}

// only allow numbers and a decimal to be entered into sq_feet field 
var checkOK = "0123456789.";
var checkStr = obj.sq_feet.value;
var allValid = true;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numeric characters in the square footage field.");
obj.sq_feet.focus();
return (false);
}
// only allow numbers and a decimal to be entered into sq_feet_ac field 
var checkOK = "0123456789.";
var checkStr = obj.sq_feet_ac.value;
var allValid = true;
var allNum = "";
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
if (!allValid)
{
alert("Please enter only numeric characters in the air conditioned square footage field.");
obj.sq_feet_ac.focus();
return (false);
 }
}

// %%%%%%%%%%%%%%%%%%%%%  DJR the below scripts are meant to be more generic and pluggable %%%%%%%%%%%%%%%%%%%%% //		
// form validator
function validateSolarForm(theForm) {
    if (!checkName(theForm["fname"])) 
	{
		return (false);
	}
	if (!checkName(theForm["lname"]))
	{
		return (false);
	}
	if (!checkText(theForm["address"], "Please enter your Street Address"))
	{
		return (false);
	} 
	if (!checkText(theForm["city"], "Please enter your City"))
	{
		return (false);
	}
	if (!checkState(theForm["state"]))
	{
		return (false);
	}
	if (!checkZipCode(theForm["zip"]))
	{
		return (false);
	} 
	if (!checkPhoneNumber(theForm["phone"]))
	{
		return (false);
	}
	if (!checkEmail(theForm["email"]))
	{
		return (false);
	}
	
	
	/* DJR email validation for businesscard application disabled
	if (!checkEmail(theForm["email"]))
	{
		return (false);
	}
	*/
	if (!getSelectedRadio(theForm["buildingStatus"]))
	{
		return (false);
	}
}	// end of solar rebate validation

// DJR checks phone Number
 function checkPhoneNumber(fld)
  {
    var phoneString = fld.value;
	// var phonePattern = /^\\(?*([0-9][0-9][0-9])*-? *\\)?*([0-9][0-9][0-9])*-?*([0-9][0-9][0-9][0-9])*$/;
    var phonePattern = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;
	var msg = "Please enter your Phone Number";
	if(fld.value.length == 0)
		{
			alert(msg);
			fld.focus();
			return (false);
		}
		else
		{
			// return (true);
			var rv = phonePattern.test(phoneString);
			if (rv == false) 
			{
				var msg = "Please enter a valid phone number in the format xxx-xxx-xxxx"; 
				alert(msg);
				return (false);
				fld.focus();
			}	
			else
			{
				return (true);
			}
		}
	}

  
// DJR checkEmail Address  
function checkEmail(fld)
{
  	var emailString = fld.value;
	var emailPattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	var msg = "Please enter your E-mail Address";
	if(fld.value.length == 0)
	{
		// DJR turning this off for now but will want a switch
		// alert(msg);
		// return (false);
		return (true);
		// fld.focus();
	}
	else
	{
		var rv =  emailPattern.test(emailString);
		var msg = "Please enter a valid e-mail address in the format somebody@yourdomain.com";
		// alert(rv);
		if (rv == false)
		{
			alert(msg);
			return (false);
			fld.focus();
		}
		else
		{
			return (true);
		}
	}
}
   
// DJR check Name fields. It basically checks to make sure the length is not 0.
	function checkName(fld)
	{
	var msg;
	// var nameString = fld.value;	
	// alert(fld.name);
		if(fld.name == "fname")
		{
			var msg = "Please enter your First Name";
		}
		else if(fld.name == "lname")
		{
			var msg = "Please enter your Last Name";
		}
		else
		{
			var msg = "Please enter a Name";
		} 
		// DJR second block checks length
		if(fld.value.length == 0)
		{
			alert(msg);
			fld.focus();
			return (false);
		}
		else
		{
			return (true);
		}
	}
   
// DJR check to make sure the value of any text input control is not 0
function checkText(fld, message)
{
	var msg = message;
	if(fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return (false);
	}
	else 
	{
		return (true);
	}
}

// DJR check the value of the ZIP code make sure it is not 0 and that it is in the right format   
function checkZipCode(fld)
{
	var zipString = fld.value;
	var msg = "Please enter your ZIP Code";
	var zipPattern = /\d{5}(-\d{4})?/;
	if(fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return (false);
	}
	else 
	{
		var rv = zipPattern.test(zipString);
		if(rv == false)
		{
		var msg = "Please enter your ZIP code in the format xxxxx or xxxxx-xxxx";
		alert(msg);
		return (false);
		fld.focus();
		}
		else
		{
			return (true);
		}
	}
} 
   
// DJR function to insure that check/radio controls are selected
function checkControl(fld,theForm)
{
	var ValueString = fld.value;
	var formString = theForm;
	var StringLen = formString.fld.length;
	alert(StringLen);
	if (StringLen == 0)
	{
		var msg = "Please select Existing Building or New Construction";
		alert(msg);
		fld.focus();
		return (false);
	}
	else
	{
		for(i=0;i<StringLen;i++)
		{
			if(formString.fld[i].checked)
			{
				var valueString = formString.fld[i].value;
			}
		}
		return (true);
	}
	alert(valueString);
}   
  
// DJR function to insure that check/radio controls are selected
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   // if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      var radioChoice = false;
	  var msg = "Please select Existing Building or New Construction";
	  for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            radioChoice = true;
		 } // end if
      } // end for
  
   // if we get to this point, no radio button is selected
      if (!radioChoice) 
	  { 
	  	// alert(msg + " " + radioChoice);
		alert(msg);
		// fld.focus();
		return (false);	  
	   // if the one button is checked, return zero
  	   } else {	
	   	return (true);
	   }	
} // Ends the "getSelectedRadio" function   
  

// DJR check state abbreviations for acceptable values  
function checkState(fld)
{
	var stateString = fld.value;
	var msg = "Please enter your state abbreviation";
	var statePattern = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;
	if(fld.value.length == 0)
	{
		alert(msg);
		fld.focus();
		return (false);
	}
	else 
	{
		var rv = statePattern.test(stateString);
		if(rv == false)
		{
		var msg = "Please enter your valid two letter state abbreviation in the format xx";
		alert(msg);
		return (false);
		fld.focus();
		}
		else
		{
			return (true);
		}
	}
}   