// Javascript functions for price form

var uploaded = false;

// Format a number with leading zeros 

function FormatNum(num) {

	fmtNum = new String();
	fmtNum = (num < 10) ? '0' + String(num) : String(num);
	
	return fmtNum;
}

// Create an order number

function CreateOrderNum(form) {

	orderNum = new String();
	now = new Date();
	yr = now.getFullYear() - 2000;
	orderNum = FormatNum(yr);
	mon = now.getMonth() + 1;
	orderNum += FormatNum(mon);
	day = now.getDate();
	orderNum += FormatNum(day);
	hour = now.getHours();
	orderNum += FormatNum(hour);
	min = now.getMinutes();
	orderNum += FormatNum(min);
	sec = now.getSeconds();
	orderNum += FormatNum(sec);
	form.orderNumber.value = orderNum;
	//form.sessionid.value = orderNum;
}

// Call upload script
function FileUpload(form) {

	var idx = form.FILE1.value.lastIndexOf("/") + 1;
	var len = form.FILE1.value.length - idx;
	if (idx > 0)	
		form.file_1.value = form.FILE1.value.substr(idx, len);

	form.file_submit[0].checked = true;	
	form.action = "https://secure.got.net/domains/mpressdigital.com/cgi-bin/upload.cgi";
	form.encoding = "multipart/form-data";
	form.target = "upload";
	props = 'width=400,height=400,status=yes,toolbar=no,menubar=no,scrollbars=no,resizable=no';
	upWin = window.open("", "upload", props);
	form.submit();
	uploaded = true;
}

// Format display as dollars &cents

function FormatDollar(value) {

   value += 0.005;      // for round-off error

   var valStr = String(value);

   var len = valStr.length;
   dot = valStr.indexOf('.');
   dotlen = dot + 3;

   if ( value == 0 ) {
       newval = ' ';
   }
   else if ( dot == -1 ) {
       newval = valStr + '.00';
   }
   else if ( len > dotlen) {
       newval = valStr.substring(0, dotlen);
   }
   else if ( len < dotlen) {
       newval = valStr + '0';   
   }
   else {
       newval = value;
   }

   return(newval);
}

// Calculate standard postcard price
var stanPrice1 = new Array( 0, 120, 170, 205, 250, 305, 340, 380, 450, 525, 610, 700, 790, 855, 940, 1040, 1090, 1120, 1165, 1195, 1370 );
var stanPrice4 = new Array( 0, 120, 170, 205, 250, 305, 360, 400, 495, 590, 675, 770, 860, 945, 1020, 1060, 1090, 1120, 1165, 1195, 1370 );

function CalcStanPrice(form) {

	idx = form.stanlist.selectedIndex;

	if (form.stanside[0].checked)
		price = stanPrice4[idx];
	else if (form.stanside[1].checked)
		price = stanPrice1[idx];
	else
		price = 0;
		
	form.stanprice.value = (price > 0) ? FormatDollar(price) : ' ';

	// clear non-needed values
	form.deluprice.value = ' ';
	form.delulist.selectedIndex = 0;
	form.deluside[0].checked = false;
	form.deluside[1].checked = false;

	CalcSubTotal(form);
	CalcSalesTax(form);
}

// Fill list with standard values

function FillStanList(form) {

	var count = form.stanlist.length;
	
	form.cardtype.value = "Standard";
	
	// fill in price values for each list item		
	for (i=1; i<count; i++) {
		value = form.stanlist[i].value;
		if (form.stanside[0].checked)
			listext = form.stanlist[i].value + ' - $' + FormatDollar(stanPrice4[i]);
		else if (form.stanside[1].checked)
			listext = form.stanlist[i].value + ' - $' + FormatDollar(stanPrice1[i]);
		else
			listext = form.stanlist[i].value;

		form.stanlist[i].text = listext;
		form.stanlist[i].value = value;
	}

	CalcStanPrice(form);
}

// Calculate deluxe postcard price

var deluPrice1 = new Array( 0, 285, 345, 435, 515, 560, 645, 760, 865, 990, 1120, 1235, 1295, 1370, 1445, 1515, 1580, 1635, 1725, 2075 );
var deluPrice4 = new Array( 0, 285, 345, 435, 515, 595, 675, 835, 990, 1090, 1165, 1235, 1295, 1370, 1445, 1515, 1580, 1635, 1725, 2075 );

function CalcDeluPrice(form) {

	idx = form.delulist.selectedIndex;	

	if (form.deluside[0].checked)
		price = deluPrice4[idx];
	else if (form.deluside[1].checked)
		price = deluPrice1[idx];
	else
		price = 0;

	form.deluprice.value = ( price > 0 ) ? FormatDollar(price) : ' ';

	// clear non-needed values
	form.stanprice.value = ' ';
	form.stanlist.selectedIndex = 0;
	form.stanside[0].checked = false;
	form.stanside[1].checked = false;

	CalcSubTotal(form);
	CalcSalesTax(form);
}


// Fill list with deluxe values

function FillDeluList(form) {

	var count = form.delulist.length;
	
	form.cardtype.value = "Deluxe";
	
	// fill in price values for each list item		
	for (i=1; i<count; i++) {
		value = form.delulist[i].value;
		if (form.deluside[0].checked)
			listext = form.delulist[i].value + ' - $' + FormatDollar(deluPrice4[i]);
		else if (form.deluside[1].checked)
			listext = form.delulist[i].value + ' - $' + FormatDollar(deluPrice1[i]);
		else
			listext = form.delulist[i].value;

		form.delulist[i].text = listext;
		form.delulist[i].value = value;
	}

	CalcDeluPrice(form);
}

// Calculate shipping cost

var stanShipGround = new Array ( 0, 15, 15, 15, 15, 15, 15, 15, 15, 25, 35, 35, 35, 40, 45, 50, 60, 65, 65, 75, 85 );
var stanShipOnite = new Array ( 0, 31.9, 31.9, 38.5, 44, 47.3, 52.8, 60.5, 71.5, 92.4, 130.9, 139.7, 152.9, 163.9, 184.8, 223.3, 232.1, 245.3, 256.3, 277.2, 369.6 );
var deluShipGround = new Array ( 0, 15, 15, 15, 25, 25, 25, 25, 45, 45, 45, 65, 65, 65, 95, 95, 95, 110, 110, 175 );
var deluShipOnite = new Array ( 0, 33.5, 45, 54.5, 66.5, 100, 111.5, 133, 178, 199.5, 244.5, 266, 311, 332.5, 377.5, 399, 444, 465.5, 510.5, 665 );

function CalcShipping(form, foc) {

	sidx = form.stanlist.selectedIndex;	
	didx = form.delulist.selectedIndex;	
	gnd = form.shipmethod[0].checked;
	//onite = form.shipmethod[1].checked;	- used to have overnight shipping
	onite = false;

	if (form.shipping_state.value != 'CA') {
		form.shipping_total.value = 'to be determined';
	}	
	else {
		if (gnd) {
			shipping = Number(stanShipGround[sidx]);
			shipping += Number(deluShipGround[didx]);
		}
		else if (onite) {
			shipping = Number(stanShipOnite[sidx]);
			shipping += Number(deluShipOnite[didx]);
		}
		else {
			shipping = 0;
		}
		form.shipping_total.value = FormatDollar(shipping);

		// do we need to do focus - Microsoft IE kludge
		if (foc == 1) {
			form.shipping_total.focus();
			form.shipping_total.blur();
		}
	}
	
	CalcOrderTotal(form);
}

// Calculate total with proofs

function CalculateProofs(form) {

	CalcSubTotal(form);
	CalcOrderTotal(form);
}

// Calculate the sub-total

function CalcSubTotal(form) {

	value = form.stanprice.value;
	subtotal = (value != ' ') ? Number(value) : 0;
	value = form.deluprice.value;
	subtotal += (value != ' ') ? Number(value) : 0;

	proof = form.proofs.value;
	subtotal += Number(proof);
			
	form.subtotal.value = FormatDollar(subtotal);
}

// Calculate salex tax

var	CA_tax = Number(0.085);

function CalcSalesTax(form) {

	if (form.resale.checked == 1)
		tax = 0;
	else if (form.billing_state.value == 'CA')
		tax = CA_tax * Number(form.subtotal.value);
	else
		tax = 0;
		
	form.sales_tax.value = FormatDollar(tax);
	CalcShipping(form, 0);
	CalcOrderTotal(form);
}

// Calculate the order total

function CalcOrderTotal(form) {

	total = Number(form.subtotal.value);
	if (!isNaN(form.shipping_total.value))
		total += Number(form.shipping_total.value);
	total += Number(form.sales_tax.value);
	
	form.order_total.value = FormatDollar(total);
}

// Check if billing & shipping addresses should be the same

function CheckSameAddress(form) {

	if (form.sameaddr.checked) {
		form.shipping_address.value			= form.billing_address.value;
		form.shipping_city.value			= form.billing_city.value;
		form.shipping_state.selectedIndex	= form.billing_state.selectedIndex;
		form.shipping_zip.value				= form.billing_zip.value;
	}
}

// Verify form input

// Check that there is a text value entered
function TestText(Ctrl) {
	if (Ctrl.value == "")
	    return(false);
	else
	    return(true);
}

//  Prompt the user with an error message
function ValidatePrompt(Item) {
	ErrMsg = Item + " not entered";
	alert(ErrMsg);
	return;
}

// Check that a radio button was checked
function RadioCheck(Ctrl, len) {
	for (i=0; i<len; i++) {
		if (Ctrl[i].checked == "1")
			return(true);
	}
	
	return(false);
}

// Check if credit card number entered

function CreditCardEntered(form) {

	ccOK = false;
	
	if (form.paymethod[3].checked == 1)
		ccOK = true;
	else if (!TestText(form.credit_card_number)) 
		ValidatePrompt("Credit card number");
	else if (!TestText(form.expiration_date))
		ValidatePrompt("Expiration date");
	else
		ccOK = true;
	
	return ccOK;
}

//  Verify the data, if all OK then submit form
function RunSubmit(form) {

	submitOK = false;
	if (!RadioCheck(form.stanside, 2) && !RadioCheck(form.deluside, 2))
		ValidatePrompt("Post card type");
	else if (!TestText(form.customer_name))
	    ValidatePrompt("Name");
	else if (!TestText(form.billing_address))
	    ValidatePrompt("Billing Address");
	else if (!TestText(form.billing_city))
	    ValidatePrompt("City");
	else if (!TestText(form.billing_zip))
	    ValidatePrompt("Zip code");
	else if (!TestText(form.phone))
	    ValidatePrompt("Phone number");
	else if (!TestText(form.email_address))
	    ValidatePrompt("Email address");
	else if (!RadioCheck(form.paymethod, 4))
	    ValidatePrompt("Payment method");
	else if (!CreditCardEntered(form))
		submitOK = false;
	else if (!RadioCheck(form.shipmethod, 3))
	    ValidatePrompt("Shipping method");
	else if (!RadioCheck(form.file_submit, 3))
		ValidatePrompt("File submission");
	else if (form.file_submit[0].checked && !TestText(form.upfile))
		ValidatePrompt('Upload file');
	else if (!form.Agree.checked)
		alert('You must agree to the guidelines.');
	else {
	    submitOK = true;
		CalcSubTotal(form);
		CalcSalesTax(form);
		
		postIt(form);
	}

	return submitOK;
}
