/*function CheckRosterForm() {
	if (document.question.team.value == "") {
		alert("Please enter your team name.");
		document.question.team.focus();
		document.question.team.select();
		return false;
	}
	else if (document.question.password.value == "") {
		alert("Please enter your password.");
		document.question.password.focus();
		document.question.password.select();
		return false;
	}
	else {
		return true;
	}
}*/

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 9;

function isInteger(s) {   
	var i;

	if (s.length != 10) return false;
	
	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++) {   
		// Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  
	return returnString;
}

function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkForm () {
	if (document.contact_form.contact_fullname.value == "") {
		alert("Please enter your name.");
		document.contact_form.contact_fullname.focus();
		document.contact_form.contact_fullname.select();
		return false;
	}
	if (document.contact_form.contact_email.value == "") {
		alert ("Please enter an Email Address.");
		document.contact_form.contact_email.focus();
		document.contact_form.contact_email.select();
		return false;
	}
	if (!test(document.contact_form.contact_email.value)) { 
		alert ("Please enter a valid Email Address.");
		document.contact_form.contact_email.focus();
		document.contact_form.contact_email.select();
		return false;
	}

	var Phone=document.contact_form.contact_phone;

	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please enter a Phone Number.");
		Phone.focus();
		Phone.select();
		return false;
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please enter a valid Phone Number.");
		Phone.value="";
		Phone.select();
		return false;
	}
	if (!checkAlphanum(document.contact_form.contact_comments.value)) {
		alert('Please enter only Alphanumeric and the following special characters: .,?!_()');
		document.contact_form.contact_comments.focus();
		return false;

		function checkAlphanum(strObject) {
			/* Returns true if the field has all alphanumeric characters, false if not.
			You must pass in a input (text) object, not the value. */
			var re = /^[0-9A-Za-z\s.,!?_()]+$/;
			return re.test(strObject);
		}
	}

	return true;
}

function test(src) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(src);
}

function wipeText(el, name) {
	if (el.defaultValue == el.value) {
		el.value = "";

	if (el.name == "comments") {
			el.className="input6";
			document.getElementById(el.name).focus();
			document.getElementById(el.name).select();
		}
		else if (el.name == "fullname" || el.name == "email" || el.name == "phone" || el.name == "email_signup" || el.name == "captcha") { 
			el.className="input2";
			document.getElementById(el.name).focus();
			document.getElementById(el.name).select();
		}
		else el.className="input4";

		if (el.name == "password") {
			document.getElementById("swapper").innerHTML="<input type=\"password\" name=\"password\" id=\"pass_box\" class=\"input4\" />" ;
			document.getElementById("pass_box").focus();
			document.getElementById("pass_box").select();
		}
	}
}

function CheckStates() {
	if (document.state_search.State_ID.value == "0") {
		alert("Must select a state.");
		document.state_search.State_ID.focus();
		return false;
	}
	else {
		return true;
	}
}

function checkRightSide() {
	if (document.sidebar_contact_form.fullname.value == "") {
		alert("Please enter your name.");
		document.sidebar_contact_form.fullname.select();
		document.sidebar_contact_form.fullname.focus();
		return false;
	}
	if (document.sidebar_contact_form.email.value == "") {
		alert ("Please enter an Email Address.");
		document.sidebar_contact_form.email.focus();
		document.sidebar_contact_form.email.select();
		return false;
	}
	if (!test(document.sidebar_contact_form.email.value)) { 
		alert ("Please enter a valid Email Address.");
		document.sidebar_contact_form.email.focus();
		document.sidebar_contact_form.email.select();
		return false;
	}

	var Phone=document.sidebar_contact_form.phone;

	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please enter a Phone Number.");
		document.sidebar_contact_form.phone.focus();
		document.sidebar_contact_form.phone.select();
		return false;
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please enter a valid Phone Number.");
		Phone.value="";
		document.sidebar_contact_form.phone.focus();
		document.sidebar_contact_form.phone.select();
		return false;
	}
	if (document.sidebar_contact_form.comment.value == "") {
		alert("Please enter a Comment or Question.");
		document.sidebar_contact_form.comment.focus();
		document.sidebar_contact_form.comment.select();
		return false;
	}
	if (!checkAlphanum(document.sidebar_contact_form.comment.value)) {
		alert('Please enter only Alphanumeric and the following special characters: .,?!_()');
		document.sidebar_contact_form.comment.focus();
		return false;

		function checkAlphanum(strObject) {
			/* Returns true if the field has all alphanumeric characters, false if not.
			You must pass in a input (text) object, not the value. */
			var re = /^[0-9A-Za-z\s.,!?_()]+$/;
			return re.test(strObject);
		}
	}

	return true;
}

function Show_EW() {
	var pgm = "documents/user_assets/news/" + document.news.News_Story_ID.value;

	newwindow=window.open (pgm,"mywindow","location=1,status=1,scrollbars=1,resizable=1,width=700,height=800"); 
	if (window.focus) {newwindow.focus()}
	return false;
}

function checkCreateForm () {
	if (document.create_form.First_Name.value == "") {
		alert("Please enter your First Name.");
		document.create_form.First_Name.focus();
		document.create_form.First_Name.select();
		return false;
	}
	if (document.create_form.Last_Name.value == "") {
		alert("Please enter your Last Name.");
		document.create_form.Last_Name.focus();
		document.create_form.Last_Name.select();
		return false;
	}
	if (document.create_form.Email_Address.value == "") {
		alert ("Please enter an Email Address.");
		document.create_form.Email_Address.focus();
		document.create_form.Email_Address.select();
		return false;
	}
	if (!test(document.create_form.Email_Address.value)) { 
		alert ("Please enter a valid Email Address.");
		document.create_form.Email_Address.focus();
		document.create_form.Email_Address.select();
		return false;
	}
	if (document.create_form.Company.value == "") {
		alert ("Please enter your Company/CO-OP Name.");
		document.create_form.Company.focus();
		document.create_form.Company.select();
		return false;
	}
	if (document.create_form.User_Name.value == "") {
		alert ("Please enter an User Name.");
		document.create_form.User_Name.focus();
		document.create_form.User_Name.select();
		return false;
	}
	if (document.create_form.Password.value == "") {
		alert ("Please enter an Password.");
		document.create_form.Password.focus();
		document.create_form.Password.select();
		return false;
	}

return true;
}

function IsNumeric(strString) {
   //  check for valid numeric strings	
   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 CalculateValue() {
	var NumberOfVehicle = document.diesel_calc.NumberOfVehicle.value;
	var GallonOfFuel = document.diesel_calc.DEFConsumptionPerGallonOfFuel.value;

	if (NumberOfVehicle.length == 0) {
		alert("Please enter a value for Cost of Typical #2 ULSD (per gallon).");
		document.diesel_calc.NumberOfVehicle.focus();
		document.diesel_calc.NumberOfVehicle.select();
		return false;
  } 
  else if (IsNumeric(NumberOfVehicle) == false) {
  	alert("Please check, numeric values only!");
		document.diesel_calc.NumberOfVehicle.focus();
		document.diesel_calc.NumberOfVehicle.select();
		return false;
  }

	if (GallonOfFuel.length == 0) {
		alert("Please enter a value for Expected Miles Per Gallon Increase.");
		document.diesel_calc.DEFConsumptionPerGallonOfFuel.focus();
		document.diesel_calc.DEFConsumptionPerGallonOfFuel.select();
		return false;
  } 
  else if (IsNumeric(GallonOfFuel) == false) {
  	alert("Please check, numeric values only!");
		document.diesel_calc.DEFConsumptionPerGallonOfFuel.focus();
		document.diesel_calc.DEFConsumptionPerGallonOfFuel.select();
		return false;
  }
	
	answer1 = NumberOfVehicle * GallonOfFuel / 100;
	document.diesel_calc.EstimatedAnnualDEFConsumption.value = answer1.toFixed(2);
	answer2 = (NumberOfVehicle * GallonOfFuel / 100) + (NumberOfVehicle * 1);
	document.diesel_calc.EstimatedDEFFillUpsPerYear.value = answer2.toFixed(2);
}

function calc2_dif() {
	var ulsd_cost = document.calculator2.ulsd_cost.value;
	var pdr_cost = document.calculator2.pdr_cost.value;
	var ulsd_good = 0;
	var pdr_good = 0;

	if (ulsd_cost.length > 0) {
		if (!IsNumeric(ulsd_cost)) { 
			alert("Numeric values only for #2 ULSD Cost.");
			document.calculator2.ulsd_cost.focus();
			document.calculator2.ulsd_cost.select();
		}
		else {
			ulsd_good = 1
		}
	}
	if (pdr_cost.length > 0) {
		if (!IsNumeric(pdr_cost)) { 
			alert("Numeric values only for PDR Cost.");
			document.calculator2.pdr_cost.focus();
			document.calculator2.pdr_cost.select();
		}
		else {
			pdr_good = 1
		}
	}
	
	if (ulsd_good && pdr_good) document.calculator2.hidden_per_dif.value =  (pdr_cost / ulsd_cost * 100).toFixed(2);
	if (document.calculator2.hidden_per_dif.value > 0) document.calculator2.per_dif.value =  '+' + document.calculator2.hidden_per_dif.value + '%';
	else document.calculator2.per_dif.value =  document.calculator2.hidden_per_dif.value + '%';
}

function calc2_mpg() {
	var cur_mpg = document.calculator2.cur_mpg.value;
	var per_dif = document.calculator2.hidden_per_dif.value;

	if (!IsNumeric(cur_mpg)) { 
		alert("Numeric values only for Current MPG.");
		document.calculator2.cur_mpg.focus();
		document.calculator2.cur_mpg.select();
	}
	else {
		document.calculator2.mpg_roi.value =  (cur_mpg * per_dif / 100).toFixed(2);
		document.calculator2.ulsd_mpg.value =  cur_mpg;
	}
}

function calc2_cpm() {
	var pdr_mpg = document.calculator2.pdr_mpg.value;
	var ulsd_mpg = document.calculator2.ulsd_mpg.value;
	var ulsd_cost = document.calculator2.ulsd_cost.value;
	var pdr_cost = document.calculator2.pdr_cost.value;

	if (!IsNumeric(pdr_mpg)) { 
		alert("Numeric values only for PDR Mileage (MPG).");
		document.calculator2.pdr_mpg.focus();
		document.calculator2.pdr_mpg.select();
	}
	else {
		cost_ulsd =  ulsd_cost / ulsd_mpg;
		cost_pdr =  pdr_cost / pdr_mpg;
		document.calculator2.savings_mile.value =  (cost_ulsd - cost_pdr).toFixed(3);
		document.calculator2.cost_ulsd.value = cost_ulsd.toFixed(3);
		document.calculator2.cost_pdr.value = cost_pdr.toFixed(3);
		document.calculator2.hidden_cost_ulsd.value = cost_ulsd;
		document.calculator2.hidden_cost_pdr.value = cost_pdr;
	}
}

function calc2_truck() {
	var hidden_cost_ulsd = document.calculator2.hidden_cost_ulsd.value;
	var hidden_cost_pdr = document.calculator2.hidden_cost_pdr.value;
	var truck_miles = document.calculator2.truck_miles.value;

	if (!IsNumeric(truck_miles)) { 
		alert("Numeric values only for Per Truck (annual miles).");
		document.calculator2.truck_miles.focus();
		document.calculator2.truck_miles.select();
	}
	else {	
		savings_mile = hidden_cost_ulsd - hidden_cost_pdr;
		document.calculator2.truck_savings.value =  (truck_miles * savings_mile).toFixed(2); 
		document.calculator2.hidden_truck_savings.value = truck_miles * savings_mile;
	}
}

function calc2_fleet() {
	var truck_savings = document.calculator2.hidden_truck_savings.value;
	var fleet_number = document.calculator2.fleet_number.value;

	if (!IsNumeric(fleet_number)) { 
		alert("Numeric values only for Truck Fleet (number of trucks).");
		document.calculator2.fleet_number.focus();
		document.calculator2.fleet_number.select();
	}
	else {
		document.calculator2.fleet_savings.value =  (fleet_number * truck_savings).toFixed(2); 
	}
}

function reset_values2() {
	for(i=0; i<document.calculator2.elements.length; i++) {
		if(document.calculator2.elements[i].type=="text") document.calculator2.elements[i].value="";
	}
}

function calc3_dif() {
	var ulsd_cost = document.calculator3.ulsd_cost.value;
	var pdr_cost = document.calculator3.pdr_cost.value;
	var ulsd_good = 0;
	var pdr_good = 0;

	if (ulsd_cost.length > 0) {
		if (!IsNumeric(ulsd_cost)) { 
			alert("Numeric values only for #2 ULSD Cost.");
			document.calculator3.ulsd_cost.focus();
			document.calculator3.ulsd_cost.select();
		}
		else {
			ulsd_good = 1
		}
	}
	if (pdr_cost.length > 0) {
		if (!IsNumeric(pdr_cost)) { 
			alert("Numeric values only for PDR Cost.");
			document.calculator3.pdr_cost.focus();
			document.calculator3.pdr_cost.select();
		}
		else {
			pdr_good = 1
		}
	}
	
	if (ulsd_good && pdr_good) document.calculator3.hidden_per_dif.value =  (pdr_cost / ulsd_cost * 100).toFixed(2);
	if (document.calculator3.hidden_per_dif.value > 0) document.calculator3.per_dif.value =  '+' + document.calculator3.hidden_per_dif.value + '%';
	else document.calculator3.per_dif.value =  document.calculator3.hidden_per_dif.value + '%';
}

function calc3_mpg() {
	var cur_mpg = document.calculator3.cur_mpg.value;
	var per_dif = document.calculator3.hidden_per_dif.value;

	if (!IsNumeric(cur_mpg)) { 
		alert("Numeric values only for Current MPG.");
		document.calculator3.cur_mpg.focus();
		document.calculator3.cur_mpg.select();
	}
	else {
		mpg_roi =  cur_mpg * per_dif / 100;
		document.calculator3.ulsd_mpg.value =  cur_mpg;
		document.calculator3.mpg_roi.value = mpg_roi.toFixed(2);
	}
}

function calc3_cpm() {
	var pdr_mpg = document.calculator3.pdr_mpg.value;
	var ulsd_mpg = document.calculator3.ulsd_mpg.value;
	var ulsd_cost = document.calculator3.ulsd_cost.value;
	var pdr_cost = document.calculator3.pdr_cost.value;

	if (!IsNumeric(pdr_mpg)) { 
		alert("Numeric values only for PDR Mileage (MPG).");
		document.calculator3.pdr_mpg.focus();
		document.calculator3.pdr_mpg.select();
	}
	else {
		exp_pdr_mpg =  (ulsd_mpg * document.calculator3.pdr_mpg.value / 100) + parseFloat(ulsd_mpg);
		cost_ulsd =  ulsd_cost / ulsd_mpg; 
		cost_pdr =  pdr_cost / exp_pdr_mpg;
		document.calculator3.savings_mile.value =  (cost_ulsd - cost_pdr).toFixed(3);
		document.calculator3.cost_ulsd.value = cost_ulsd.toFixed(3);
		document.calculator3.cost_pdr.value = cost_pdr.toFixed(3)
		document.calculator3.exp_pdr_mpg.value =  exp_pdr_mpg.toFixed(3);
		document.calculator3.hidden_cost_ulsd.value = cost_ulsd;
		document.calculator3.hidden_cost_pdr.value = cost_pdr;
	}
}

function calc3_truck() {
	var hidden_cost_ulsd = document.calculator3.hidden_cost_ulsd.value;
	var hidden_cost_pdr = document.calculator3.hidden_cost_pdr.value;
	var truck_miles = document.calculator3.truck_miles.value;

	if (!IsNumeric(truck_miles)) { 
		alert("Numeric values only for Per Truck (annual miles).");
		document.calculator3.truck_miles.focus();
		document.calculator3.truck_miles.select();
	}
	else {
		savings_mile = hidden_cost_ulsd - hidden_cost_pdr;
		document.calculator3.truck_savings.value =  (truck_miles * savings_mile).toFixed(2); 
		document.calculator3.hidden_truck_savings.value = truck_miles * savings_mile;
	}
}

function calc3_fleet() {
	var truck_savings = document.calculator3.hidden_truck_savings.value;
	var fleet_number = document.calculator3.fleet_number.value;

	if (!IsNumeric(fleet_number)) { 
		alert("Numeric values only for Truck Fleet (number of trucks).");
		document.calculator3.fleet_number.focus();
		document.calculator3.fleet_number.select();
	}
	else {
		document.calculator3.fleet_savings.value =  (fleet_number * truck_savings).toFixed(2); 
	}
}

function reset_values3() {
	for(i=0; i<document.calculator3.elements.length; i++) {
		if(document.calculator3.elements[i].type=="text") document.calculator3.elements[i].value="";
	}
}

function toggle_layer(whichLayer, display_value) {
	var elem;
	var vis;

	if( document.getElementById ) { // this is the way the standards work
    elem = document.getElementById( whichLayer );
	}
  else if( document.all ) { // this is the way old msie versions work      
    elem = document.all[whichLayer];  
	}
  else if( document.layers ) { // this is the way nn4 works
    elem = document.layers[whichLayer];
	}

	vis = elem.style;
 	vis.display = display_value;
}

function show_calc1(whichLayer) {
	var ulsd_cost = document.calculator1.ulsd_cost.value;

	toggle_layer(whichLayer, 'block');
	
	document.calculator1.calc_value.value =  ulsd_cost;
}

function reset_values1(whichLayer) {
	toggle_layer(whichLayer, 'none');

	for(i=0; i<document.calculator1.elements.length; i++) {
		if(document.calculator1.elements[i].type=="text") document.calculator1.elements[i].value="";
	}
}

function print_calc (form_name, calc) {
	var winleft = (screen.width - 930) / 2;
	var winUp = (screen.height - 500) / 2;
	var theURL;
	var Name;

	winProp = 'width=500,height=600,left='+winleft+',top='+winUp+',scrollbars=yes,resizable=yes';

	if (form_name == "calculator1") {
		var uc = document.calculator1.ulsd_cost.value;

		theURL = 'act_print_calculator.cfm?c=' + calc + '&uc=' + uc;
	}
	else {
		var uc = eval('document.' + form_name + '.ulsd_cost.value');
		var pc = eval('document.' + form_name + '.pdr_cost.value');
		var cm = eval('document.' + form_name + '.cur_mpg.value');
		var pm = eval('document.' + form_name + '.pdr_mpg.value');
		var tm = eval('document.' + form_name + '.truck_miles.value');
		var fn = eval('document.' + form_name + '.fleet_number.value');

	 	theURL = 'act_print_calculator.cfm?c=' + calc + '&uc=' + uc + '&pc=' + pc + '&cm=' + cm + '&pm=' + pm + '&tm=' + tm + '&fn=' + fn;
	}
	
	Name = 'PrintReport'

	Win = window.open(theURL, Name, winProp);
	Win.window.focus();
}

