var s_fields= "Desc,CompanyName,WellLoc,CalcHPReq,TotalWD,TotalFP,ElecHPReq,ElecMotCost,ElecMHU,ElecMRC,ElecCost,ElecSurCh,ElecLineReq,FeetReq,CostFeet,ArrowGEL,GASMotHU,GASMotM,GASSold,GASCost,engtype";
var s_fieldslist = s_fields.split(",")

//set the expire date to one year
var cookie_expires = new Date();
y1 = cookie_expires.getYear();
if (y1 < 100) y1 = y1 + 1900;
cookie_expires.setYear(y1 + 1);


function setCookie(name, value, expires){
 document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString();
}

function getCookie(name){
var cname = name + "=";
var dc = document.cookie;
    if (dc.length > 0) {
    begin = dc.indexOf(cname);
        if (begin != -1) {
        begin += cname.length;
        end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        }
    }
return null;
}

function delCookie(name) {
  expireDate = new Date;
  expireDate.setDate(expireDate.getDate()-1);
  document.cookie = name + "=; expires="+ expireDate.toGMTString();
}

function GetWorkingCookie() {
  //returns the working cookie ID as String( 1,2,3... etc.)
  work = getCookie("PMCWork");
  working = "";
  if (work != null) {
 	working = getCookie("PMCComp" + work);
  } else {
 	working = null;
  }
  return working;
}


function SetWorkingCookie(in_newcookie) {
  // set's the new working cookie - the whole string with the parameters
  // if there wasn't any WorkID before, sets it to 1
  work = getCookie("PMCWork");
  if (work==null) work="1";
  setCookie("PMCWork",work,cookie_expires);
  setCookie("PMCComp"+work,in_newcookie,cookie_expires);
}

function GetCookieConfig(in_cookie,in_param) {
  // Get parameter from cookie, the parameter name is the field name
  // Parameters : 	in_cookie is the whole working cookie ( not just the ID )
  //			in_param is the parameter to get
  // Returns a String
  ret = null;
  c = in_cookie.split("|");
  for (i=0;i<c.length;i++) {
    if (in_param == s_fieldslist[i]) {
 	  ret = new String(c[i]);
 	  ret = ReplaceAll(ret,"+"," ");
    }
  }
  return ret;
}

function ReplaceAll(in_string,s1,s2) {
  while (in_string.indexOf("+") != -1) {
    in_string = in_string.replace(s1,s2);
  }
  return in_string;
}

//Get number of PMC cookies defined, starts with 1 for 1 cookie
function NumOfWorkCookies() {
  i=0;
  ret = "";
  while (ret != null & i < 1000) {
  	x = i + 1;
 	ret = getCookie("PMCComp" + x);
 	i = i + 1;
  }

  return i - 1;

}

function setPMCWork(in_value){
  setCookie("PMCWork",in_value,cookie_expires);
}

//validate for numbers only
function val_num(fieldv) {
  var valid = "0123456789."
  var ok = "yes";
  var temp;
  for (var i=0; i<fieldv.length; i++) {
	temp = "" + fieldv.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
  }
  if (ok == "no") return false;
  return true;
}

function trimstring(in_str) {
  onestr = in_str.substr(0,1);
  while (onestr ==" ") {
 	in_str = in_str.substr(1,in_str.length - 1);
 	onestr = in_str.substr(0,1);
  }
  onestr = in_str.substr(in_str.length - 1,1);
  while (onestr ==" ") {
 	in_str = in_str.substr(0,in_str.length - 1);
	onestr = in_str.substr(in_str.length - 1,1);
  }
  return in_str;
}

function QSpar(in_str) {
	loc = new String(location.href);
	q = loc.indexOf("?");
	qs = new String(loc.substring(q + 1));
	spl = qs.split("&");
	for (x=0;x<spl.length;x++) {
		spl2 = spl[x].split("=");
		if (spl2[0] == in_str) {
			return spl2[1];
		}
	}
	return "";
}

function CurrencyString(in_num) {
 cur = new String(fixed(in_num,2));
 sup = cur.substr(cur.length - 2);
 dec = cur.substr(0,cur.length - 3);
 spl = dec.split("");
 y = 0;
 res = ""
 for (x=spl.length - 1;x>=0;x--) {
   if (y == 3) {
   	res = spl[x] + "," + res;
   	y = 0;
   } else {
 	res = spl[x] + res;
   }
   y++;
 }
 return res+"."+sup
}

function fixed(frmObj,iValue)
{
 var data = ""
 var decimal = false
 var j = 0
 if (frmObj.length != 0)
  {
        for(var i = 0 ; i < frmObj.length ; i++)
         {
          if (frmObj.substring(i,i+1) == '.')
            {
              i = frmObj.length
              decimal = true
            }
          j = j + 1
         }
        if(frmObj.length - j >= iValue)
         {
          data = frmObj.substring(0,j+iValue)
         }
        else
         {
          if (decimal == false)
           {
            data = frmObj + "."
            for(var i = 0 ; i < iValue ; i++)
             {
              data = data + "0"
                  }
           }
          else
           {
            data = frmObj
            for(var i = 0 ; i < iValue ; i++)
             {
              data = data + "0"
              if (data.length == j + iValue)
                {
                 i = iValue
                }
                 }
           }
         }
 }
 return data
}

function WriteCurrency(in_value) {
	val = new String(in_value);
	document.write("$" + CurrencyString(val));

	//document.write("$" + fixed(val,2));
}

function formatCurrency(in_value) {
	val = new String(in_value);
	return ("$" + CurrencyString(val));
}

function formatNumber(in_value) {
	val = new String(in_value);
	return (CurrencyString(val));
}

function getEngineVar(v, engine, rpm, in_array) {
//This function returns the value passed to "v" from the array passed to "in_array."
//Variables "engine" and "rpm" are used as selection criteria.
ret = "";
for (z=0; z<in_array.length; z++) {
	if (v == "HP"){
		if (in_array[z].engine == engine && in_array[z].rpm == rpm){
			ret = in_array[z].hp;
			break   
		}
	} else if (v == "Displacement") {
		if (in_array[z].Name == engine) {
			ret = in_array[z].Displacement;
			break
		}
	}	
}
return ret
}

function getEngineFromQS() {
//Gets the engine from the query_string if present.
		search_str = "?engine=";
		ret = "";
		if (loc.indexOf(search_str) != -1){
			ret = loc.substring(loc.indexOf(search_str) + search_str.length,loc.length)
		} else {
			ret = ""
		}
return ret
}

function enteredZero(checkFields) {
//Checks to see if any of the fields named in the checkFields array contain a value of 0.
				 		field_val = "";
		for (i=0; i<checkFields.length; i++) {
				field = eval("document.forms[0]." + checkFields[i]);
				if (field.type == "text") {
					 field_val = field.value
				} 
		if (field_val == 0) { 
			 return true; 		
		  }
		}	 
		return false
	}

function round(number,x) {
//This function rounds the number passed to the function and returns
//the number of decimal places passed to variable 'x' which defaults
//to 2.
		 x = (!x ? 2 : x);
		 return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
}

function validateNum(v) {
//This validation script applies to the slide rule application.  This function allows
//more characters than the val_num function above.

	var valid = "-0123456789.";
	var ok = "yes";
	var temp;
	for (var i=0; i<v.length; i++) {
		temp = "" + v.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		return false;
	}
	return true
}


function checkNumRange(fieldname, low, high, decplaces, fieldtitle) {

/* This function takes a field handle, a low and high range 
and a comment and verifies that it falls within the specified range.
		fieldname = a handle to the field we are calculating against
		low = the low end range that the field value may NOT be less than.
		high = the high end range that the field value may NOT be greater than.
		decplaces = the number of decimal places to round to. 
		fieldtitle = the label of the field.
*/
					var is_dollar_amt = false;		 
					val = fieldname.value;
					
					if (val.indexOf("$") != -1) {
						 val = val.substring(1, val.length);
						 is_dollar_amt = true
						 }
					if (validateNum(val) == false) {
						 alert ('Please supply a numeric value for ' + fieldtitle + ' between ' + low + ' and ' + high + '.');
						 fieldname.focus();
						 fieldname.select();
						 return false;
					}	else	{
					 	if(val < low || val > high) {
						 alert ('The ' + fieldtitle + ' must be between ' + low + ' and ' + high + '.');
						 fieldname.focus();
						 fieldname.select();
						 return false;
						 }
						 if (is_dollar_amt == true)fieldname.value = formatCurrency(val)
						 else fieldname.value = round(val, decplaces);
						 
					}
						return true; 
}

function getEngineByHP(hp) {
engine = "";
if (hp >= 0 && hp < 7) engine = GE[0].Name; 
if (hp >= 7 && hp < 13) engine = GE[1].Name; 
if (hp >= 13 && hp < 18) engine = GE[2].Name;
if (hp >= 18 && hp < 25) engine = GE[3].Name;
if (hp >= 25 && hp < 40) engine = GE[4].Name + " , or " + GEM[0].Name;
if (hp >= 40 && hp < 67) engine = GE[5].Name + " , or " + GEM[0].Name;
if (hp >= 67 && hp < 79) engine = GE[6].Name + " , or " + GEM[0].Name;
if (hp >= 79 && hp < 126) engine = GE[7].Name + " , or " + GEM[0].Name;
if (hp >= 126 && hp < 156) engine = GE[8].Name;
return engine;
}

//function getEngineByHP(hp) {
//engine = "";
//if (hp >= 2 && hp < 10) engine = GE[0].Name; 
//if (hp >= 10 && hp < 14) engine = GE[1].Name;
//if (hp >= 14 && hp < 20) engine = GE[2].Name;
//if (hp >= 20 && hp < 32) engine = GE[3].Name + " , or " + GEM[0].Name;
//if (hp >= 32 && hp < 55) engine = GE[4].Name + " , or " + GEM[0].Name;
//if (hp >= 55 && hp < 65) engine = GE[5].Name + " , or " + GEM[0].Name;
//if (hp >= 65 && hp < 125) engine = GE[6].Name + " , or " + GEM[0].Name;
//if (hp >= 125 && hp < 155) engine = GE[7].Name;
//return engine;
//}

function isEmpty(field) {
	var i;
	var whitespace = " \t\n\r";
	if ((field == null) || (field.length == 0)) {
		return true;
	}
	//search string for characters that are not whitespace
	for (i = 0; i < field.length; i++) {
		var c = field.charAt(i);
		if (whitespace.indexOf(c) == -1) {
			return false;
		}
	}
	//all characters are whitespace
	return true;
}