




function open_large_image(url) {
    var  new_url, name, features, new_window;
    new_url = "http://www.affordablewindowshades.com/show_image.php?file=" + url;
    name = "Product_Image";
    features = "width=400,height=550,fullscreen=no,location=no,resizable=yes,status=no,scrollbars=yes,toolbar=no";
    new_window = window.open(new_url, name,features);
    new_window.focus();

}




function open_calc() {
    var  new_url, name, features, new_window;
    new_url = "http://www.affordablewindowshades.com/multi-headrail-calculator.php";
    name = "Multi_headrail_calculator";
    features = "width=550,height=140,fullscreen=no,location=no,resizable=yes,status=no,scrollbars=auto,toolbar=no";
    new_window = window.open(new_url, name,features);
    new_window.focus();

}

function show_tooltip(tooltip_id,e) {
	try {
		
		
		var xcoord, ycoord;
	
		if( !e ) {
			e = window.event;
			return;
		}
		if( typeof( e.pageX ) == 'number' ) {
			xcoord = e.pageX;
			ycoord = e.pageY;
		} else if( typeof( e.clientX ) == 'number' ) {
			xcoord = e.clientX;
			ycoord = e.clientY;
		}
/*		
		if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			xcoord += document.body.scrollLeft;
			ycoord += document.body.scrollTop;
		}
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		xcoord += document.documentElement.scrollLeft;
		ycoord += document.documentElement.scrollTop;
		}
		else { return; }
*/
		var tip = document.getElementById(tooltip_id);
		var w = tip.clientWidth;
		
		tip.style.display = "";
		tip.style.left = xcoord - w;
		tip.style.top = ycoord;
		
	} catch(e)	{
		
	}
	
	
}

function hide_tooltip(tooltip_id)	{
	try {
		var tip = document.getElementById(tooltip_id);
		tip.style.display = "none";
	} catch(e) {
		
	}
}


function callAHAH(url, pageElement, callMessage, errorMessage)

{

document.getElementById(pageElement).innerHTML=callMessage;

try{ req=new XMLHttpRequest(); }

catch(e){

try { req = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */ }

catch (f) {

try{ req = new ActiveXObject("Microsoft.XMLHTTP");/* some versions IE */ }

catch (E){ req = false;}

}}

req.onreadystatechange = function()

{ responseAHAH(pageElement, errorMessage); }

req.open("GET",url,true); 

req.send(null); }



function responseAHAH(pageElement, errorMessage) {

if(req.readyState == 4) { if(req.status == 200) { 

output = req.responseText;

document.getElementById(pageElement).innerHTML = output; 

} else { 

document.getElementById(pageElement).innerHTML = errorMessage+"\n"+responseText; } 

} }



function makeactive(page, tag)

{

  callAHAH(page, tag, '<br/><br/><span style="color:#FF0000"><strong>Loading Color Chart. Please wait...</strong></span><br/><br/>', 'Error');

}

function openColorSamplePopup(url) {
    var  new_url, name, features, new_window;
    new_url = "http://www.affordablewindowshades.com/popup.php?file=" + url;
    name = "Option_Sample";
    features = "width=400,height=550,fullscreen=no,location=no,resizable=yes,status=no,scrollbars=auto,toolbar=no";
    new_window = window.open(new_url, name,features);
    new_window.focus();

}

function openColorSamplePopup2(url,op_label_id) {
    var  new_url, name, features, new_window;
    new_url = "http://affordablewindowshades.com/popup.php?file=" + url + "&op=" + op_label_id;
    name = "Option_Sample";
    features = "width=400,height=550,fullscreen=no,location=no,resizable=yes,status=no,scrollbars=auto,toolbar=no";
    new_window = window.open(new_url, name,features);
    new_window.focus();
}

function openSampleWindow(sample_id) {

    //$url = "http://www.affordablewindowshades.com/t.php?addSample=" + $sample_id;
    var url, name, features;
	url = "http://www.affordablewindowshades.com/" + sample_id;
    name = "Sample_Order_Form";

    features = "width=850,height=590,fullscreen=yes,location=no,resizable=yes,status=no,scrollbars=yes,toolbar=no";

    new_window = window.open(url, name, features);

    new_window.focus();

}



// validate ZIP and ZIP+4 codes

function validateZipCode($zip_field) {

    var $valid_chars = "0123456789-";

    var $hyphen_count = 0;



    if ($zip_field.length != 5 && $zip_field.length != 10) {

        return false;

    }

    for (var i=0; i < $zip_field.length; i++) {

        $zc = "" + $zip_field.substring(i, i+1);

        if ($zc == "-") $hyphen_count++;

        if ($valid_chars.indexOf($zc) == "-1") {

            return false;

        }

        if (($hyphen_count > 1) || (($zip_field.length==10) && ""+$zip_field.charAt(5)!="-")) {

            return false;

        }

    }

    return true;

}



// Larger Sample Window

function popupWindow(url) {

  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')

}
var added_tax = false;
function calculate_total() {
	var selected_state =  document.getElementById("state_billing");
	var display_tax =  document.getElementById("state_tax");
	var tax_state =  document.getElementById("sales_tax_state");
	var sales_tax =  document.getElementById("sales_tax");
	var total = document.getElementById("total");
    var total_show = document.getElementById("total_show");
	
	if ((selected_state.value == tax_state.value) && (added_tax == false)) {
		display_tax.style.display = "";
		total.value = parseFloat(total.value) + parseFloat(sales_tax.value);
		var num1 = new Number(total.value);
		total.value = num1.toFixed(2);
        total_show.innerHTML = total.value;
		added_tax = true;

	}
	else if ((selected_state.value != tax_state.value) && (added_tax == true)) 
	{
		display_tax.style.display = "none";
		total.value = parseFloat(total.value)  - parseFloat(sales_tax.value);
		var num2 = new Number(total.value);
		total.value = num2.toFixed(2);
        total_show.innerHTML = total.value;
		added_tax = false;
	}
}

function copyFields() {
 		 var s_first_name = document.getElementById("first_name");
 		 var b_first_name = document.getElementById("firstName");
 		 s_first_name.value = b_first_name.value;
 		 
 		 var s_last_name = document.getElementById("last_name");
 		 var b_last_name = document.getElementById("lastName");
 		 s_last_name.value = b_last_name.value;
 		 
 		 var s_address = document.getElementById("address");
 		 var b_address = document.getElementById("address1");
 		 s_address.value = b_address.value;
 		 
 		 var s_city = document.getElementById("city");
 		 var b_city = document.getElementById("billing_city");
 		 s_city.value = b_city.value;

 		 var s_state = document.getElementById("state");
 		 var b_state = document.getElementById("state_billing");
 		 s_state.value = b_state.value;
 		 
 		 
 		 var s_zip = document.getElementById("zip_code");
 		 var b_zip = document.getElementById("zip");
 		 s_zip.value = b_zip.value;
 		  
 }
 
 var form_submitted = false;
 
 function hideButton() {
 	
 	result = validate_form();
 	if (result == false) {
 		return false;
 	}
 	
 	var but = document.getElementById("submitButton");
 	but.style.display = "none";
 	var showBut = document.getElementById("showButton");
 	showBut.style.display = "";
 	var result = false;
 	
 
 	if ( form_submitted )	{
   		 alert ( " Processing. Please wait..." );
    		return false;
  	}	else	{
    		form_submitted = true;
    		return true;
  	}
  
 	
 }
 
 function validate_form() {
 		var message = " Please enter \n";
 		var errors  = 0;
 		var first_name = document.getElementById("first_name");
 		if (first_name.value.toString().length < 2) {
 			errors++;
 			message = message + " First Name \n";
 		}
 		
 		var last_name = document.getElementById("last_name");
 		if (last_name.value.toString().length < 2) {
 			errors++;
 			message = message + " Last Name \n";
 		}
 		
 		var address = document.getElementById("address");
 		if (address.value.toString().length < 2) {
 			errors++;
 			message = message + " Address \n";
 		}
 		
 		var city = document.getElementById("city");
 		if (city.value.toString().length < 2) {
 			errors++;
 			message = message + " City \n";
 		}
 		
 		var state = document.getElementById("state");
 		if (state.value ==  "") {
 			errors++;
 			message = message + " State \n";
 		}
 		
 		var zip_code = document.getElementById("zip_code");
 		if (zip_code.value.toString().length < 2) {
 			errors++;
 			message = message + " Zip code \n";
 		}
 		
 		var phone = document.getElementById("phone");
 		if (phone.value.toString().length < 2) {
 			errors++;
 			message = message + " Day Phone \n";
 		}
 		
 		var email = document.getElementById("email");
 		if (email.value.toString().length < 2) {
 			errors++;
 			message = message + " Email \n";
 		}
 		
 		
 		if (errors > 0) {
 			alert(message);
 			return false;
 		}
 		
 		return true;
 }
 /*
There are three functions in this set for credit card validation.
The main function is:
validateCard(creditCardNumber,creditCardType,expDateMonth,expDateYear)
	parameters:
		all paramaters are string values.
		Month & Year come from the select input fields in the form, so they are defined.
		creditCardType can be:
			'a' for American Express
			'd' for Discover
			'm' for MasterCard
			'v' for Visa
	description:
		This function will check string length, valid characters, specific credit card prefixes and test
		the Mod 10 (LUHN Formula) for validating possible credit card numbers. This function can only
		authorize that the given card data is potentially valid. You would still need to run actual
		card validation routines to verify the actual account.
	returns:
		this function returns true if the card number could be valid for the card type and expiration date.
		false otherwise.	
supporting functions:
mod10( creditCardNumber )
	parameters:
		this function takes the text string card number and runs the Mod 10 formula on its respective digits.
	description:
		Mod 10 is the check digit formula for the supported cards these functions attempt to validate.
	returns:
		this function returns true if the number passes the check digit test.
		false otherwise.
expired( expDateMonth, expDateYear )
	parameters:
		this function takes the text string values given by the html form.
	description:
		this function basically will check to make sure todays date is less than the expiration date the user inputs.
		this function is not locked into using 2 digit dates.
	returns:
		this fucntion returns true if the card is expired.
		false otherwise.
*/
function mod10( creditCardNumber )	// LUHN Formula for validation of credit card numbers.
{ 
	var ar = new Array( creditCardNumber.length );
	var i = 0,sum = 0;

   	for( i = 0; i < creditCardNumber.length; ++i )
	{
   		ar[i] = parseInt(creditCardNumber.charAt(i));
	}
	for( i = ar.length -2; i >= 0; i-=2 )	// you have to start from the right, and work back.
	{ 
		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
	}										 // if the double digit is > 9, add those individual digits together 
	for( i = 0; i < ar.length; ++i )
	{
		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
	}
    return (((sum%10)==0)?true:false);	 	
}

function expired( month, year )
{
	var now = new Date();							// this function is designed to be Y2K compliant.
	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
	if( now.getTime() < expiresIn.getTime() )
	return false;
	return true;									// then we get the miliseconds, and do a long integer comparison
}


function validateCard(creditCardNumber,creditCardType,expDateMonth,expDateYear)
{
	if (document.billingform.paymethod.value == "Check/Money Order")
	{
		return true;
	}
	if( creditCardNumber.length == 0 )	//most of these checks are self explanitory
	{						
		alert("Please enter a valid card number.");
		return false;				
	}
	/*
	for( var i = 0; i < creditCardNumber.length; ++i )	// make sure the number is all digits.. (by design)
	{		
		var c = creditCardNumber.charAt(i);
		if( c < '0' || c > '9' )
		{
			alert("Please enter a valid card number. Use only digits. Do not use spaces or hyphens.");
			return false;
		}
	}
	*/
	var length = creditCardNumber.length;	//perform card specific length and prefix tests

	switch( creditCardType )
	{
		case 'Amex':

			if( length != 15 ) {
				alert("Please enter a valid American Express Card number.");
				return false;
			}
			var prefix = parseInt( creditCardNumber.substring(0,2));
			if( prefix != 34 && prefix != 37 ) {
				alert("Please enter a valid American Express Card number.");
				return false;
			}
			break;
		case 'Discover':

			if( length != 16 ) {
				alert("Please enter a valid Discover Card number.");
				return false;
			}
			var prefix = parseInt( creditCardNumber.substring(0,4));
			if( prefix != 6011 ) {
				alert("Please enter a valid Discover Card number.");
				return false;
			}
			break;
		case 'MasterCard':

			if( length != 16 ) {
				alert("Please enter a valid MasterCard number.");
				return false;
			}
			var prefix = parseInt( creditCardNumber.substring(0,2));
			if( prefix < 51 || prefix > 55) {
				alert("Please enter a valid MasterCard Card number.");
				return false;
			}
			break;
		case 'Visa':

			if( length != 16 && length != 13 ) {
				alert("Please enter a valid Visa Card number.");
				return false;
			}
			var prefix = parseInt( creditCardNumber.substring(0,1));
			if( prefix != 4 ) {
				alert("Please enter a valid Visa Card number.");
				return false;
			}
			break;
	}
	if( !mod10( creditCardNumber ) )	// run the check digit algorithm
	{ 		
		alert("Sorry! This is not a valid credit card number.");
		return false;
	}
	if( expired( expDateMonth, expDateYear ) )	// check if entered date is already expired.
	{							
		alert("Sorry! The expiration date you have entered would make this card invalid.");
		return false;
	}
	return true; // at this point card has not been proven to be invalid
}
function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number. The number should be without dashes or empty spaces\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function submitMe(product_id) {
    var formPrice = 'product_price_' + product_id;
    document.getElementById(formPrice).submit();
}
