// -------------------------------------------------------------------
// ---- Form Validation and Querystring Transfers with JS
// -------------------------------------------------------------------
// ---- Designed by Active Media, 2001
// ---- (Daniel Saw; Freelance Mouse Pilot - Aust)
// ---- http://www.active-media.com.au/
// ---- and created using the scripting services of Steven Border
// ---- If you use this script, include the above credits.
// -------------------------------------------------------------------
//**Start Encode**

// Set Line Selection Totals on Order Form
function lineItemTotal(productcode) {
	if(!prodCodeExists(productcode)){
		return formatCurrency(0)
	} else {
		var oForm = document.mnorderform
		var intQty = eval("parseInt(oForm."+productcode+"[oForm."+productcode+".selectedIndex].value)");
		var dcPrice = eval("parseFloat(prodPriceReturn('"+productcode+"'))");
		eval("oForm."+productcode+"total.value = formatCurrency(intQty*dcPrice)");
		return parseFloat(intQty*dcPrice)
	}
}
// -------------------------------------------------------------------

// Write to form "grandtotalorder" field value
function grandtotal(){
	var lineItemTotalAdd
	var oForm = document.mnorderform
	lineItemTotalAdd = 0
	for (var i=0;i < prodCode.length;i++){
		lineItemTotalAdd += lineItemTotal(prodCode[i])
		}

	// If first, add Australian freight else, If second, add Asian freight else add Worldwide freight
	if(oForm.delivery[0].checked){
		lineItemTotalAdd+=19.75
		}else{
		if(oForm.delivery[1].checked){
			lineItemTotalAdd+=74
			}else{
			lineItemTotalAdd+=95
			}
		}

	oForm.grandtotalorder.value = formatCurrency(lineItemTotalAdd)
	return lineItemTotalAdd
	}
// -------------------------------------------------------------------

// Write options to a select list (max) rows
function writeQuantitySelectList(max) {
	// Write first row selected
        document.write("<option value='0' selected>0<\/option>");
	// Write additional rows up to (max) amount
	if(isNaN(max)){
		max = 50;
		};
	for(var i = 1;i < max;i++){
		document.write("<option value='"+i+"'>"+i+"<\/option>");
		}
	}
// -------------------------------------------------------------------

// Validation script for Order form; called on submit (validate contact)
function ContactValidate(){
	var oForm = document.mnorderform;
	var msg = '';
	if(oForm.realname.value==''){msg+='- Contact Name\n'}
	if(oForm.email.value.indexOf('@')==-1 || oForm.email.value.indexOf('\.')==-1){msg+='- Contact Email Address\n'}
	if(oForm.order_address.value==''){msg+='- Delivery Address\n'}
	if(oForm.order_address2.value==''){msg+='- Delivery Suburb\n'}
	if(oForm.order_country.value==''){msg+='- Delivery Country\n'}
	if(oForm.order_postcode.value==''){msg+='- Delivery Postcode or Zip\n'}

	if(msg!=''){
		window.alert('The following required field\/s\nmust be completed:\n\n'+msg)
		return false
		}
	return true
	}
// -------------------------------------------------------------------

// Validation script for Payment form; called on submit
function PayValidate(){
	var oForm = document.mnorderform
	var msg = ''
	if(oForm.order_card_name.value==''){msg+='- Cardholder Name\n'}
	if(oForm.order_card_type.value==''){msg+='- Credit Card Type\n'}
	if(oForm.order_card_number.value==''){msg+='- Credit Card Number\n'}
	if(oForm.order_card_type.selectedIndex==2&&oForm.order_card_amexid.value==''){msg+='- American Express ID\n'}
	if(oForm.order_card_exp_month.value==''||oForm.order_card_exp_year.value==''){msg+='- Card Expiry Date\n'}
	if(oForm.order_card_exp_month.value!=''&&oForm.order_card_exp_year.value!=''){
		objNow = new Date();
		objDate = new Date();
		objDate.setMonth(oForm.order_card_exp_month.value-1);
		objDate.setYear(oForm.order_card_exp_year.value);
		if(objDate < objNow){
			msg+='\nNote: Your Card Has Expired\n';
			}
		}

	if(msg!=''){
		window.alert('Please complete these required field\/s:\n\n'+msg);
		return false
		}

	return true
	}
// -------------------------------------------------------------------


// Write order details as a string concatenated to redirect field (Url)
function writeSSLstring() {

	var success = ContactValidate()
	if(!success){
		return false
		}
	
	var oForm = document.mnorderform
	oForm.unique.value = new String(new Date().toUTCString());	
	oForm.subject.value += oForm.unique.value

	var mn_name = escape(oForm.realname.value)
	var mn_email = escape(oForm.email.value);
	var mn_address = escape(oForm.order_address.value);
	var mn_address2 = escape(oForm.order_address2.value);
	var mn_country = escape(oForm.order_country.value);
	var mn_postcode = escape(oForm.order_postcode.value);
	var mn_fax = escape(oForm.order_fax.value);
	var mn_work = escape(oForm.order_work_phone.value);
	var mn_ah = escape(oForm.order_ah_phone.value);
	var mn_total = escape(oForm.grandtotalorder.value.replace('$',''));
	var mn_unique = escape(oForm.unique.value);

	var mn_ssl_string = "https:\/\/wic052u.server-secure.com\/VS72818_secure\/order.html?mn_name="+mn_name
	mn_ssl_string += "&mn_email="+mn_email;
	mn_ssl_string += "&mn_address="+mn_address;
	mn_ssl_string += "&mn_address2="+mn_address2;
	mn_ssl_string += "&mn_country="+mn_country;
	mn_ssl_string += "&mn_postcode="+mn_postcode;
	mn_ssl_string += "&mn_fax="+mn_fax;
	mn_ssl_string += "&mn_work="+mn_work;
	mn_ssl_string += "&mn_ah="+mn_ah;
	mn_ssl_string += "&mn_total="+mn_total;
	mn_ssl_string += "&mn_unique="+mn_unique;
    	
	oForm.redirect.value = mn_ssl_string;
	
	return true
	}
// -------------------------------------------------------------------

// Collect Querystring values from Url for a given "key"
function QueryString(key){
	var params = new String(document.location);
	var startIndex = params.indexOf(key+'=')
	var endIndex = params.indexOf('&',startIndex + key.length +1)
	if(endIndex==-1){
		endIndex = params.length
		}
	if(startIndex==-1){
		return ''
		}else{
		return unescape(params.substring(startIndex + key.length +1,endIndex))
		}
	}
// -------------------------------------------------------------------

// Populate Payment Form with contents of Url using QueryString and "key"
function PopulateForm(){
	document.mnorderform.order_lasttname.value = QueryString("mn_name")
	document.mnorderform.order_email.value = QueryString("mn_email")
	document.mnorderform.order_address.value = QueryString("mn_address")
	document.mnorderform.order_address2.value = QueryString("mn_address2")
	document.mnorderform.order_country.value = QueryString("mn_country")
	document.mnorderform.order_postcode.value = QueryString("mn_postcode")
	document.mnorderform.order_fax.value = QueryString("mn_fax")
	document.mnorderform.order_work_phone.value = QueryString("mn_work")
	document.mnorderform.order_ah_phone.value = QueryString("mn_ah")
	document.mnorderform.order_comments.value = formatCurrency(QueryString("mn_total"))
	document.mnorderform.order_position.value = QueryString("mn_unique")
	}
// -------------------------------------------------------------------
