// *************************************
// IN BOTH

function SelectAdd( sobj, opt_obj){
try {
  /* this will work for IE, Gecko will throw an Exception... */
  sobj.add(opt_obj);
}
catch (ex) {
  /* ... catch it and do it the right way */
  sobj.add(opt_obj, null);
}
}

//   Online ONly,    In Couny, Rest of state, Out of State
//      		3 months, 6 mnths, on year
//
// **************************** THIS IS NOW CONTROLLED IN COMPANY DATA **********************************
//
var rates_list = [[8.99,14.99,24.99],[8.99,14.99,24.99],[10.99,18.99,30.99],[12.99,21.99,36.99]]

function fix_order_amount( order_amount ){
	//var order_amount = document.getElementByID("_Order Amount")
	var sa_text = document.getElementById("_OrderTotal")
	var sa_value = document.getElementById("OrderTotal")
	sa_value.value = '0'
	var a_mount = 0
	if ( order_amount != null ) { a_mount = order_amount.valueOf()  }
	if (( a_mount == null ) || ( a_mount.NaN ) || ( a_mount ==  0 ) || ( a_mount == '' )){
		sa_value.value = 0
		sa_text.innerHTML = "<font color=red>$0.00</font>";
	} else {
		sa_value.value = order_amount
		sa_text.innerHTML = "<font color=blue>$" + FixNum(order_amount,2) + "</font>";
	}
}

function update_select_summary(){
	var type_select = document.getElementById('sub_type');
	var rate_select = document.getElementById('sub_duration');
	var region_select = document.getElementById('sub_region');
	var ss_text = document.getElementById("ss_text")
	var order_amount = document.getElementById("OrderTotal")
	var txt
	txt = '';


	if ( region_select.options[0].selected ) { 
		ss_text.innerHTML = "Please Select A Subscription <b>Region</b>";
		for (var i=1;i<rate_select.options.length;i++) { 
			rate_select.options[i].text = rate_select.options[i].value
		}
		rate_select.options[0].selected = true; 
		fix_order_amount(0)
		return;
	}

	/*
	switch  (region_select.value) {
	case "In County" :	use_rates = incounty; break;
	case "Rest of State" : use_rates = restofstate; break;
	case "Out of State" : use_rates = outofstate; break;
	}
	*/
	for (var i=1;i<rate_select.options.length;i++) { 
		rate_select.options[i].text = rate_select.options[i].value + " $" + rates_list[region_select.selectedIndex-1][i-1]; // use_rates[i-1]
	}
	
	if ( rate_select.options[0].selected ) { 
		ss_text.innerHTML = "Please Select A Subscription <b>Duration</b>";
		fix_order_amount(0)
		return;
	}


	if ( type_select.options[0].selected ) { 
		ss_text.innerHTML = "Please Select A Subscription <b>Type</b>";
		fix_order_amount(0)
		return;
	}

	fix_order_amount(rates_list[region_select.selectedIndex-1][rate_select.selectedIndex-1]) //use_rates[rate_select.selectedIndex-1])
	txt = type_select.options[type_select.selectedIndex].value;
	txt = txt + ", " + region_select.options[region_select.selectedIndex].value 
	txt = txt + ", " + rate_select.options[rate_select.selectedIndex].value
	txt = txt + " $" + rates_list[region_select.selectedIndex-1][rate_select.selectedIndex-1];  //use_rates[rate_select.selectedIndex-1]
	ss_text.innerHTML = txt

}

// *************************************
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   while (retValue.search('\n')>=0){  retValue = retValue.replace('\n',' ');}
   while (retValue.search('\r')>=0){  retValue = retValue.replace('\r','');}
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user

} // Ends the "trim" function


function FixNum( NumtoFix, dg ){
// NumtoFix = number to fix
// dg = digits to force (truncate)

   var numstr = NumtoFix.toString();
   var l = numstr.length;
   var a = numstr.indexOf(".")+1;

   // If there is no decimal to start, append zeroes and return
   if(a<=0){
     if(dg>0){
        numstr += ".";
        for(i=0;i<dg;i++){numstr += "0";}
     }
     return numstr;
   }

   // decimal to start, do we delete or add?
   var plc = l - a;
   dg = (dg==0)?-1:dg; // gets rid of trailing decimal
   if(dg>plc){
     for(i=plc;i<dg;i++){numstr += "0";}
     return numstr;
   }else{
     return numstr.substr(0,dg+a);
   }
}


function Store_StateValue( f ){
  if(f.name == "_State") document.subscriber["State"].value = f.value;
}  

function Build_StateMenu(ThisElement,ThisName){
var sname = "_" + ThisName;
var CurrentState = document.subscriber[ThisName].value;
if ( CurrentState == '' ) {  CurrentState = 'TX' }
var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY"
var USList = USStateCodes.split(USStateCodeDelimiter);
var txt = "<select name='" + sname + "' size=1 onchange='Store_StateValue(this);' >";
  for(i=0;i<USList.length;i++){
    txt += "<option value=" + USList[i] 
    txt += (( CurrentState == USList[i] ) ? " selected " : " " );
    txt += ">" + USList[i] + "</option>";
  }
  txt += "</select>";
  ThisElement.innerHTML=txt;
}



var frmvalidator
//
function PrepareValidation(){
 // TEMPORARY FOR DEVELOPMENT
 // REMOVE THESE THREE LINES
 frmvalidator  = new Validator("subscriber");
 with (frmvalidator){
   addValidation("Subscriber Region","req","Please Select a Subscription Region");
   addValidation("Subscription Duration","req","Please Select a Subscription Duration");
   addValidation("Subscription Type","req","Please Select a Subscription Type");
   addValidation("Firstname","req","Please enter you First Name");
   addValidation("Lastname","req","Please enter the Last Name and any suffixes like JR, SR, etc.");
   addValidation("Address","req","Please enter the Address");
   addValidation("City","req","Please enter the City");
   addValidation("State","req","Please enter the State");
   addValidation("Zip Code","req","Please enter the Zip Code");
   addValidation("Email Address","req","An Email Address Is Required For Online Subscription Processing");
   addValidation("Email Address","email","It appears your email is not in a valid format");
   addValidation("_Email Address","req","Please re-enter your password to confirm");
   addValidation("Password","req","A Password is Required for Online Subscription Processing");
   addValidation("_Password","req","Please re-enter your password to confirm");
   addValidation("_confirm_button","button","");
   
 }
}

function NewEnough(){
 if ( !document.getElementById ){
    alert("This page requires Netscape 6+ or Internet Explorer 5+ to work correctly");
    return false;
 }
 return true;
}


function verifySame( thisobj, verify_id ){
	var verifyobj = document.getElementById(verify_id)
	
	if (( thisobj.value.toLowerCase() != verifyobj.value.toLowerCase())) {
		return false;
	}	else {
		return true;
	}
}

function finalVerify(){
  var tobj = document.getElementById("_Email Address")
    if ( !  verifySame( tobj,"Email Address") ){
		alert("Email entries do not match. This is required to ensure the validity of your email address. Please re-enter your email address.")
		tobj.focus()
		tobj.select()
		return false;
	}
	tobj = document.getElementById("_Password")
	if ( !  verifySame( tobj,"Password") ){
		alert("Password entries do not match. This is required to ensure the validity of your password. Please re-enter your password.")
		tobj.focus()
		tobj.select()
		return false;
	}

	return true;
}

function doReset(){
	try {
	document.location.href = document.location.href 
	return true;
	} catch(er) {
		return true;
	}
}
function starter( slct){
	var s
	try{ s = slct.toLowerCase();
	} catch(er) {  s = '' 
	}
	switch (s){
	case 'confirm' :		
		var hme = document.getElementById('transaction.company.url.home');
		document.getElementById('payment').submit(); 
		break;
	default:	
		var mstate = document.getElementById("mstate");
		Build_StateMenu(mstate,"State");
		update_select_summary();
		PrepareValidation();
		document.getElementById('_confirm_button').style.visibility='visible';
		break;
	}
}
