function swap( img, isrc )
{
  if( !document.images ) return;
  document.images[img].src = isrc;
}

function popWindow( uri, width, height )
{
    var wnd = window.open( uri,
			   '_blank', 
			   'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=no,resizable=no,toolbar=no'
			   );
    wnd.focus();
} // popWindow

// this function shows/hides the text hints in the window status bar
function textHint( frm, element, event, text, hint )
{
    obj = document.forms[frm].elements[element];

    if( obj )
    {
	if( event == 'focus' )
	{
	    if( hint )
	    {
		window.status = hint;
	    }
	}
	else if( event == 'blur' )
	{
	    if( hint )
	    {
		window.status = '';
	    }
	}
    }
} // textHint()

function updateStateDropdown( frm, strCountryList, strStateList )
{
    var divUS, divUK, divCA, divOther;
    var lstCountry, strCountryVal;
    
    // get the Country listbox object
    lstCountry = document.forms[frm].elements[strCountryList];

    // get the various DIVs to show/hide
    divUS = document.getElementById( 'contact_state_us' );
    divUK = document.getElementById( 'contact_state_uk' );
    divCA = document.getElementById( 'contact_state_ca' );
    divOther = document.getElementById( 'contact_state_other' );

    if( lstCountry )
    {
	// get the string value of the selected country
	strCountryVal = lstCountry.options[lstCountry.selectedIndex].value;

	// show/hide the appropriate DIVs
	if( strCountryVal == "USA" )
	{
	    divUS.style.display = 'block';
	    divUK.style.display = 'none';
	    divCA.style.display =  'none';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your state from the State/Province list.' );
	}
	else if( strCountryVal == "IRELAND" || strCountryVal == "UNITED KINGDOM" )
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'block';
	    divCA.style.display = 'none';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your county from the State/Province list.' );
	}
	else if( strCountryVal == "CANADA" )
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'none';
	    divCA.style.display = 'block';
	    divOther.style.display = 'none';

	    //alert( 'Please choose your province from the State/Province list.' );
	}
	else
	{
	    divUS.style.display = 'none';
	    divUK.style.display = 'none';
	    divCA.style.display = 'none';
	    divOther.style.display = 'block';

	    //alert( 'Please enter your state/province in the State/Province field.' );
	}
    }
} // updateStateDropdown()

// validate quotation form
function qtValidate(d) {
	var e;
	// first name
	e = d.fname.value;
	if (e == "") {
		alert("Please enter a value for the \"First Name\" field.");
		d.fname.focus();
		return false;
	}
	// last name
	e = d.lname.value;
	if (e == "") {
		alert("Please enter a value for the \"Last Name\" field.");
		d.lname.focus();
		return false;
	}
	// company
	e = d.company.value;
	if (e == "") {
		alert("Please enter a value for the \"Company\" field.");
		d.company.focus();
		return false;
	}
	// address
	e = d.address.value;
	if (e == "") {
		alert("Please enter a value for the \"Address\" field.");
		d.address.focus();
		return false;
	}
	// address 2 {not required}
	//e = d.address2.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Address 2\" field.");
	//	d.address2.focus();
	//	return false;
	//}
	// city
	e = d.city.value;
	if (e == "") {
		alert("Please enter a value for the \"City\" field.");
		d.city.focus();
		return false;
	}
	// state
	e = d.state.value;
	if (e == "") {
		alert("Please enter a value for the \"State\" field.");
		d.state.focus();
		return false;
	}
	// zip code
	e = d.zipcode.value;
	if (e == "") {
		alert("Please enter a value for the \"Zip Code\" field.");
		d.zipcode.focus();
		return false;
	}
		// country
	e = d.country.value;
	if (e == "") {
		alert("Please enter a value for the \"Country\" field.");
		d.country.focus();
		return false;
	}
	// telephone
	e = d.telephone.value;
	if (e == "") {
		alert("Please enter a value for the \"Telephone\" field.");
		d.telephone.focus();
		return false;
	}
	// fax {not required}
	//e = d.fax.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Fax\" field.");
	//	d.fax.focus();
	//	return false;
	//}
	// Email
	e = d.email.value;
	if (e == "") {
		alert("Please enter a value for the \"Email\" field.");
		d.email.focus();
		return false;
	}
	// ship address
	e = d.ship_address.value;
	if (e == "") {
		alert("Please enter a value for the \"Address\" field.");
		d.ship_address.focus();
		return false;
	}
	// ship address 2 {not required}
	//e = d.ship_address2.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Address 2\" field.");
	//	d.ship_address2.focus();
	//	return false;
	//}
	// ship city
	e = d.ship_city.value;
	if (e == "") {
		alert("Please enter a value for the \"City\" field.");
		d.ship_city.focus();
		return false;
	}
	// ship state
	e = d.ship_state.value;
	if (e == "") {
		alert("Please enter a value for the \"State\" field.");
		d.ship_state.focus();
		return false;
	}
	// ship zip code
	e = d.ship_zipcode.value;
	if (e == "") {
		alert("Please enter a value for the \"Zip Code\" field.");
		d.ship_zipcode.focus();
		return false;
	}
		// ship country
	e = d.ship_country.value;
	if (e == "") {
		alert("Please enter a value for the \"Country\" field.");
		d.ship_country.focus();
		return false;
	}
	// telephone
	e = d.ship_telephone.value;
	if (e == "") {
		alert("Please enter a value for the \"Telephone\" field.");
		d.ship_telephone.focus();
		return false;
	}
	// fax {not required}
	//e = d.fax.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Fax\" field.");
	//	d.fax.focus();
	//	return false;
	//}
	// asmi price
	e = d.asmi_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"ASMI Price\" field.");
		d.asmi_price.focus();
		return false;
	}
	else {
		if (e=="" || e < 11500) {
			alert("The \"ASMI Price\" field cannot be less than $11,500.");
			d.asmi_price.value = 11500;
			return false;
		}
	}
	// treatment table price
	e = d.table_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Mantis Sports Treatment Table Price\" field.");
		d.table_price.focus();
		return false;
	}
	else {
		if (e=="") {
			d.table_price.value = 0;
		}
	}
	// treatment table price 2
	e = d.table_price2.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Mantis Deluxe Treatment Table Price\" field.");
		d.table_price2.focus();
		return false;
	}
	else {
		if (e=="") {
			d.table_price.value = 0;
		}
	}

	// air compressor price
	e = d.air_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Air Compressor Price\" field.");
		d.air_price.focus();
		return false;
	}
	else {
		if (e=="") {
			d.air_price.value = 0;
		}
	}
	// portable trolley price
	e = d.trolley_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Portable Trolley Price\" field.");
		d.trolley_price.focus();
		return false;
	}
	else {
		if (e=="") {
			d.trolley_price.value = 0;
		}
	}
	// replacement handset price
	e = d.handset_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Replacement Handset Price\" field.");
		d.handset_price.focus();
		return false;
	}
	else {
		if (e=="") {
			d.handset_price.value = 0;
		}
	}
	// extended warranty price
	e = d.warranty_price.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Extended Warranty Price\" field.");
		d.warranty_price.focus();
		return false;
	}
	else {
		if (e=="") {
			d.warranty_price.value = 0;
		}
	}
	// shipping cost
	e = d.sandh.value;
	if (e != "" && !IsNumeric(e)) {
		alert("Please enter a numeric value for the \"Shipping Cost\" field.");
		d.sandh.focus();
		return false;
	}
	else {
		if (e=="") {
			d.sandh.value = 0;
		}
	}

	return true;
}

// check if value passed in is a number
function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
         }
      }
   return IsNumber;
}

// set company address and shipping address to the same. Uses company address.
function setShippingAddr(){
	var d=document.forms(0);
	if (d.ship_pre_fill.checked) {
		d.ship_address.value = d.address.value;
		d.ship_address2.value = d.address2.value;
		d.ship_city.value = d.city.value;
		d.ship_state.value = d.state.value;
		d.ship_zipcode.value = d.zipcode.value;
		d.ship_country.value = d.country.value;
		d.ship_telephone.value = d.telephone.value;
		d.ship_fax.value = d.fax.value;
	}
	else {
		d.ship_address.value = "";
		d.ship_address2.value = "";
		d.ship_city.value = "";
		d.ship_state.value = "";
		d.ship_zipcode.value = "";
		d.ship_country.value = d.country.value;
		d.ship_telephone.value = "";
		d.ship_fax.value = "";
	}
}

// calculate the cost summary
function calcCostSummary() {
	var d=document.forms(0);
	var e=0;
	var p=0;
	var t=0;
	var s=0;
	// calculate cost
	// asmi price
	e = d.asmi_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// treatment table price
	e = d.table_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// deluxe treatment table price
	e = d.table_price2.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// air compressor price
	e = d.air_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// portable trolley price
	e = d.trolley_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// replacement handset price
	e = d.handset_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	// extended warranty price
	e = d.warranty_price.value;
	if (e != "" && IsNumeric(e)) { p=(parseFloat(p)+parseFloat(e)); }
	d.cost.value = p;
	document.getElementById("_cost").innerHTML = p;

	// calculate the tax
	// to do: devise system for calculating the tax automatically
	// for now do manually
	if(d.state.value.toUpperCase() == "NY") {
		t = parseFloat((p*.0875)); // NYS sales tax rate
		d.tax.value = Math.round(t*100)/100;
		document.getElementById("_tax").innerHTML = Math.round(t*100)/100;
	}
	else {
		d.tax.value = 0;
		document.getElementById("_tax").innerHTML = 0.00;
	}
	
	// get shipping cost if any and calculate the total cost
	s = d.sandh.value;
	if (s != "" && s != 0 && IsNumeric(s)) { 
		// calculate the total cost with shipping price
		d.total_cost.value = (p+t)+parseFloat(s);
		document.getElementById("_total_cost").innerHTML = (p+t)+parseFloat(s);
	}
	else {
		// calculate total cost without shipping price
		d.total_cost.value = parseFloat((p+t));
		document.getElementById("_total_cost").innerHTML = parseFloat((p+t));
	}
}

// login validation
function lgnValidate(d) {
	if (d.userid.value == "") {
		alert("Enter the \"User ID\"");
		d.userid.focus();
		return false;
	}
	if (d.password.value == "") {
		alert("Enter the \"Password\"");
		d.password.focus();
		return false;
	}
	return true;
}

// redirect to the status page
function sendToQTStatus() {
	document.location.href = "status.asp";
}

// submit form data to edit page
function sendToQTEdit() {
	document.forms(0).action = "edit.asp";
	document.forms(0).submit();
}


// validate quotation form
function qtReqValidate(d) {
	var e;
	// first name
	e = trim(d.fname.value, "");
	if (e == "") {
		alert("Please enter a value for the \"First Name\" field.");
		d.fname.focus();
		return false;
	}
	// last name
	e = trim(d.lname.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Last Name\" field.");
		d.lname.focus();
		return false;
	}
	// company
	e = trim(d.company.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Company\" field.");
		d.company.focus();
		return false;
	}
	// address
	e = trim(d.address.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Address\" field.");
		d.address.focus();
		return false;
	}
	// address 2 {not required}
	//e = d.address2.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Address 2\" field.");
	//	d.address2.focus();
	//	return false;
	//}
	// city
	e = trim(d.city.value, "");
	if (e == "") {
		alert("Please enter a value for the \"City\" field.");
		d.city.focus();
		return false;
	}
	// state
	e = trim(d.state.value, "");
	if (e == "") {
		alert("Please enter a value for the \"State\" field.");
		d.state.focus();
		return false;
	}
	// zip code
	e = trim(d.zipcode.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Zip Code\" field.");
		d.zipcode.focus();
		return false;
	}	
	// country
	e = trim(d.country.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Country\" field.");
		d.country.focus();
		return false;
	}
	// telephone
	e = trim(d.telephone.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Telephone\" field.");
		d.telephone.focus();
		return false;
	}
	// fax {not required}
	//e = d.fax.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Fax\" field.");
	//	d.fax.focus();
	//	return false;
	//}
	// Email
	e = trim(d.email.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Email\" field.");
		d.email.focus();
		return false;
	}
	// ship address
	e = trim(d.ship_address.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Address\" field.");
		d.ship_address.focus();
		return false;
	}
	// ship address 2 {not required}
	//e = d.ship_address2.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Address 2\" field.");
	//	d.ship_address2.focus();
	//	return false;
	//}
	// ship city
	e = trim(d.ship_city.value, "");
	if (e == "") {
		alert("Please enter a value for the \"City\" field.");
		d.ship_city.focus();
		return false;
	}
	// ship state
	e = trim(d.ship_state.value, "");
	if (e == "") {
		alert("Please enter a value for the \"State\" field.");
		d.ship_state.focus();
		return false;
	}
	// ship zip code
	e = trim(d.ship_zipcode.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Zip Code\" field.");
		d.ship_zipcode.focus();
		return false;
	}
	// ship country
	e = trim(d.ship_country.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Country\" field.");
		d.ship_country.focus();
		return false;
	}
	// telephone
	e = trim(d.ship_telephone.value, "");
	if (e == "") {
		alert("Please enter a value for the \"Telephone\" field.");
		d.ship_telephone.focus();
		return false;
	}
	// fax {not required}
	//e = d.fax.value;
	//if (e == "") {
	//	alert("Please enter a value for the \"Fax\" field.");
	//	d.fax.focus();
	//	return false;
	//}
	
	// getting ready to send mail change the text on the submit button
	d.submit.value = "Please wait while we generate the quote...";
	d.submit.disabled = true;
	
	return true;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}