// JavaScript Document
//Main calculation
//constants
var NN4 = document.layers? true : false; //Netscape Navigator 4.x.
var NN6 = document.getElementById? true : false; //Netscape Navigator 6.x
var IE = document.all? true : false; // IE version 4 and above.
	if(NN6 && !(IE)) 
	 style = "";
	else
	 style = "style."

var cuttingCharge = 24;
var siseal = 2.05;

//values to be recieved from opener document
var perYdPrice = parseFloat(opener.document.getElementById("weavePricePerYard").value);
var bindingPrice = parseFloat(opener.document.getElementById("bindingPricePerFoot").value);
var parentBindingID, rugID, miteringCharge, totalYard, maxWidthText, maxWidth;

function initialize(){
//values to be recieved from opener document
 parentBindingID = opener.document.getElementById("parentBindingID").value;
 rugID = opener.document.getElementById("rugID").value;
 rugID = rugID.substring(0,2);
 
 maxWidth = parseFloat(opener.document.getElementById("maxWidth").value);

 	if(isNaN(maxWidth)) maxWidth = 11.833;

	switch(maxWidth){
		case 11.833333333333334 :
			maxWidthText = "11ft. 10in";
			break;
		case 12.833333333333334 :
			maxWidthText = "12ft. 10in";
			break;		
		case 12.916666666666667 :
			maxWidthText = "12ft. 11in";
			break;
		default :
			maxWidthText = "11ft. 10in";
			break;
	}
 
 if(rugID == "ma" || rugID == "la"){
 	document.getElementById("Siseal Required").value = "Not Applicable";
 	document.getElementById("sisealTD").disabled = true;	
	document.getElementById("sisealTD").innerHTML = "Siseal Not Applicable"		
 }

 document.getElementById("Weave").value=opener.document.getElementById("rugName").value;
 document.getElementById("Parent Binding").value=opener.document.getElementById("parentBinding").value;
 document.getElementById("Binding Color").value=opener.document.getElementById("bindingName").value;
 
 if(NN4){
 	document.getElementById("rugInfo").visibility = "visible";		
	document.getElementById("customerInfo").visibility = "hidden";		
 } else{
 	eval("document.getElementById('rugInfo')." + style + "visibility = 'visible'");		
	eval("document.getElementById('customerInfo')." + style + "visibility = 'hidden'");
 }	

 if(parentBindingID == "BINDINGS-WIDE-COTTON-BINDING"){
 	miteringCharge = 75;
	document.getElementById("Binding Corner").options[0] = new Option('Lapped Corner Standard');
	document.getElementById("Binding Corner").options[1] = new Option('Hand Sewn Mitered Corners - Add $100.00');
 }	
 if(parentBindingID == "BINDINGS-SMOOTH-LINEN-BINDING" || parentBindingID == "BINDINGS-SOFT-COTTON-TWILL-BINDING" || parentBindingID == "BINDINGS-WIDE-WOOL-BINDING") {
 	miteringCharge = 100;
	document.getElementById("Binding Corner").options[0] = new Option('Lapped Corner Standard');
	document.getElementById("Binding Corner").options[1] = new Option('Hand Sewn Mitered Corners - Add $100.00');	
 }	
 if(parentBindingID == "BINDINGS-NARROW-COTTON-BINDING" || parentBindingID == "BINDINGS-LEATHER-BINDING")
 	miteringCharge = 0;
}

function checkSize(){
	var widthStatus, widthIsNum, checkWidth, lengthStatus, lengthIsNum, checkLength, width1, width2, length1,length2 ;
	
	width1 = document.getElementById("width1").value;
	width2 = document.getElementById("width2").value;

	length1 = document.getElementById("length1").value;
	length2 = document.getElementById("length2").value;
	
	if(width1 == "" || isNaN(parseFloat(width1))){
		alert("Please Enter a Proper Width");
		document.getElementById("width1").select();		
	}
	else{
		if(width2 == "" || isNaN(parseFloat(width2))){
			alert("Please Enter a Proper Width");
			document.getElementById("width2").select();
		 }else{
		 	  width2 = parseFloat(width2) * 0.083333333333333333333333333333333;
			  //33333333333333333333;
		 	  checkWidth = parseFloat(width1) + width2;
			  document.getElementById("Width").value = checkWidth + " ft.";
			  widthIsNum = "OK";
		}
	}
	
	if(widthIsNum == "OK"){
		if(length1 == "" || isNaN(parseFloat(length1))){
			alert("Please Enter a Proper Length");
			document.getElementById("length1").select();		
		}
		else{
			if(length2 == "" || isNaN(parseFloat(length2))){
				alert("Please Enter a Proper Length");
				document.getElementById("length2").select();
			 }else{
			 	  length2 = parseFloat(length2) * 0.083333333333333333333333333333333;
				  checkLength = parseFloat(length1) + length2;
				  document.getElementById("Length").value = checkLength + " ft.";
				  lengthIsNum = "OK";
			}
		}
	}
	
	if(widthIsNum == "OK" && lengthIsNum == "OK"){
		//alert("len=" + checkLength + ", wid=" + checkWidth + ", maxWidth=" +maxWidth);
		if(checkLength > maxWidth && checkWidth > maxWidth){
			alert("Atleast one Dimension should be less then or equal to " + maxWidthText);
		}else{
			lengthStatus = "OK";
			widthStatus = "OK";
		}
	}
		
	if(lengthStatus == "OK" && widthStatus == "OK"){
		if(checkWidth >= checkLength){
			if(checkWidth <= maxWidth && checkLength <= maxWidth){
				wid1 = maxWidth;
				len1 = checkLength;
			}else{
				wid1 = checkWidth;
				len1 = maxWidth;			
			}
		}
		else{
			if(checkWidth <= maxWidth && checkLength <= maxWidth){
				wid1 = checkWidth;
				len1 = maxWidth;	
			}else{
				wid1 = maxWidth;
				len1 = checkLength;			
			}		
		}
		
		totalYard = (wid1 * len1) / 9;
		//Adding Siseal Price
		if(document.getElementById("Siseal Required").checked)
			sisealPrice = totalYard * siseal;
		else
			sisealPrice = 0;
		//Adding corners price
		if(document.getElementById("Binding Corner").length > 1){
			cornersText = document.getElementById("Binding Corner").options[document.getElementById("Binding Corner").selectedIndex].text;
			if(cornersText == "Lapped Corner Standard")
				cornersPrice = 0;
			else{
				if(cornersText == "Hand Sewn Mitered Corners - Add $60.00")
					cornersPrice = 60;
				if(cornersText == "Hand Sewn Mitered Corners - Add $100.00")
					cornersPrice = 100;

			}
		}else{
			cornersPrice = 0;
		}
		quotePrice = (totalYard * perYdPrice) + (((checkWidth * 2) + (checkLength * 2)) * bindingPrice) + cuttingCharge + sisealPrice + cornersPrice;
		quotePrice = Math.ceil(quotePrice);
		for(var i = 0;  i < 5 ; i++){
			priceval = quotePrice % 5;
			if(priceval == 0)
				break;
			quotePrice = quotePrice + 1;
		}

		document.getElementById("Quoted Price").value = "$" + quotePrice + ".00";

		if(NN4){
			document.layers["rugInfo"].visibility = "hidden";	
			document.layers["customerInfo"].top = "20px";
			document.layers["customerInfo"].left = "10px";		
			document.layers["customerInfo"].visibility = "visible";	
		}else{
			eval("document.getElementById('rugInfo')." + style + "visibility = 'hidden'");	
			eval("document.getElementById('customerInfo')." + style + "top = '20px'");
			eval("document.getElementById('customerInfo')." + style + "left = '10px'");		
			eval("document.getElementById('customerInfo')." + style + "visibility = 'visible'");
		}
	}
}
function goBack(){
 if(NN4){
 	document.layers["rugInfo"].visibility = "show";		
	document.layers["customerInfo"].visibility = "hidden";
 } else{
	eval("document.getElementById('rugInfo')." + style + "visibility = 'visible'");
	eval("document.getElementById('customerInfo')." + style + "visibility = 'hidden'");	
 }		
}


//Form Validation script

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_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') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) 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 == '');
}
