// JavaScript Document


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function estimateCalc(frm) {
    var order_total = 0
	var item_price_exterior = 4.00
	var item_price_interior = 3.00
	var item_price_mirrorD = 10
	var item_price_scr = 1
	var item_price_chan = 35
	var item_price_out = 15
	var item_price_mDoor = 5
	var item_price_mBath = 10
	var item_price_hard_w = 70
	var item_price_french = 15
	var item_fuel_charge = 10

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

		
        // Get the current field
        form_field = frm.elements[i]

        // Get the field's name
        form_name = form_field.name

        // Is it a "product" field?
        if (form_name.substring(0,7) == "interio") {

            // If so, extract the price from the name
            item_price = item_price_interior

            // Get the quantity
            item_quantity = parseInt(form_field.value)
		
            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "exterio") {

            // If so, extract the price from the name
            item_price = item_price_exterior

            // Get the quantity
            item_quantity = parseInt(form_field.value)
		
            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "French_") {

            // If so, extract the price from the name
            item_price = item_price_french

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "screens") {

            // If so, extract the price from the name
            item_price = item_price_scr

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		 if (form_name.substring(0,7) == "chandel") {

            // If so, extract the price from the name
            item_price = item_price_chan

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		 if (form_name.substring(0,7) == "outside") {

            // If so, extract the price from the name
            item_price = item_price_out

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "mirrorD") {

            // If so, extract the price from the name
            item_price = item_price_mirrorD

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "mirrorB") {

            // If so, extract the price from the name
            item_price = item_price_mBath

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		if (form_name.substring(0,7) == "hard_wa") {

            // If so, extract the price from the name
            item_price = item_price_hard_w

            // Get the quantity
            item_quantity = parseInt(form_field.value)

            // Update the order total
            if (item_quantity >= 0) {
                order_total += item_quantity * item_price
            }
        }
		
    }
	
order_total += 10
    // Display the total rounded to two decimal places
    frm.ESTIMATE.value = round_decimals(order_total, 2)
}



function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function checkForm()
{
		
		if(!checkDate())
		{
			alert('Sorry this day has passed. Please request a new Date');
			return false;
		}
			
	return true;
}
function checkDate() {

	var current_date = new Date();
	var day = current_date.getDate();
	var month = current_date.getMonth();
	var year = current_date.getYear();	 
	day=day+1;
    var today = new Date(year,month,day);	 
    var date1 = getServiceDate();
	 return ( today <= date1 ); 	
}
function getServiceDate() { 
	var dd = document.forms[0].When_service_day.value;
    var mmyy = document.forms[0].When_service_month.value; 
	var mm = mmyy.substring( 0, mmyy.indexOf( '-') ); 				
	var curr_date = new Date();	

	var yy = mmyy.substring( (mmyy.indexOf( '-')+1), mmyy.length );	
	var serviceDate = new Date( yy, mm-1, dd); 	
	return serviceDate; 
} 
function getCurrDate(){

	var monthArray= new Array(12) 
	monthArray[0]="Jan";
	monthArray[1]="Feb";
	monthArray[2]="Mar";
	monthArray[3]="Apr";
	monthArray[4]="May";
	monthArray[5]="Jun";
	monthArray[6]="Jul";
	monthArray[7]="Aug";
	monthArray[8]="Sep";
	monthArray[9]="Oct"; 
	monthArray[10]="Nov";
	monthArray[11]="Dec";
	monthArray[12]="Jan";
	
	var current_date = new Date();
	var day = current_date.getDate();
	var month = current_date.getMonth();
	var monthCurr = current_date.getMonth();
	var year = current_date.getYear();	
	var yearCurr = current_date.getYear();

	for(var i=0; i<12; i++){
		var newOpt = document.createElement("OPTION");					
		newOpt.text=monthArray[month];
		month = (month+1)%12;
		if(month == 0) month=12;
		if(monthCurr > month)year= yearCurr+1;
		newOpt.value= month + '-' + year;
		try{
				document.forms['form1'].When_service_month.add(newOpt);
			}
			catch(exception)
			{
				document.forms['form1'].When_service_month.add(newOpt,null);
			}
		
	}

	document.forms['form1'].elements['When_service_month'].options[1].selected=true;
	document.forms['form1'].elements['When_service_day'].options[day+1].selected=true;
	
	return;
}
function val(){alert("here");}
function validate(field)
{

	if (field.name == "Name")
	{
	FieldNameText = "your Name."
	}
	
	if (field.name == "Address")
	{
	FieldNameText = "the Address."
	}

	if (field.name == "City")
	{
	FieldNameText = "the City."
	}

	if (field.name == "State")
	{
	FieldNameText = "the State."
	}

	if (field.name == "Zip")
	{
	FieldNameText = "the Zip Code."
	}
	if (field.name == "Dayphone")
	{
	FieldNameText = "the daytime phone number."
	}
	if (field.name == "Evephone")
	{
	FieldNameText = "the evening phone number."
	}
	if (field.name == "Stories")
	{
	FieldNameText = "the number of stories."
	}
	// Validate Field Values
	var checkFieldValues = field.value;
	newcheckFieldValue = checkFieldValues.replace(/\s/g, "");

	if (newcheckFieldValue == "")
	{
		alreadyValidating = true;
		alert("You must enter a value for " + FieldNameText);
		field.focus();
		return false;
	} // end if
	return true;
	
} // end function validate()

function validateRead(field)
{
	
var checkFieldValues = field.value;
	newcheckFieldValue = checkFieldValues.replace(/\s/g, "");

	if (newcheckFieldValue == "" || newcheckFieldValue == "NO")
	{
		alreadyValidating = true;
		alert("Please read the above explanation of window pane costs and confirm you have done so by answering YES to question #1");
		field.focus();
		return false;
	} // end if	
	return true;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->
