var theForm = '';
var theField = '';

function sameAsBilling(theForm) {
	var flds = Array('firstName', 'lastName', 'email', 'company', 'address', 'address2', 'city', 'country', 'state', 'postalCode', 'phone', 'phone2');
	for (var i=0; i<flds.length; i++) {
		if (theForm['B' + flds[i]] && theForm['S' + flds[i]])
			theForm['S' + flds[i]].value = theForm['B' + flds[i]].value;
		if (flds[i]=='country')
			populateState('S');
	}
}

function confirmContinue(theForm) {
	window.location='/catalog/';
}

function proceedToCheckout(theForm) {
	var res = false;
	if (Summit.Form.ValidateForm(theForm)) {
		res = true;
	}
	return res;
}

function expressCheckout(theForm) {
	var res = false;
	if (Summit.Form.ValidateForm(theForm)) {
		send(AJAX_URL + '?do=validateEmail&' + $('expressCheckoutFrm').serialize(), 'expressCheckoutResponse');
	}
	return res;
}

function expressCheckoutResponse() {
	if (XmlHttp.readyState==4){
		result = XmlHttp.responseText;
		XmlHttp=null;
		if (result==1) {
			window.location = '/actions/continueWithoutLogin/' + document.forms['expressCheckoutFrm']['email'].value + '/';
		} else {
			$('expressCheckoutEmailUsed').show();
		}
	}
}

function membersCheckout(theForm) {
	var res = false;
	if (Summit.Form.ValidateForm(theForm)) {
		send(AJAX_URL + '?do=validateMember&' + $('membersCheckoutFrm').serialize(), 'membersCheckoutResponse');
	}
	return res;
}

function membersCheckoutResponse() {
	if (XmlHttp.readyState==4){
		result = XmlHttp.responseText;
		XmlHttp=null;
		if (result==1) {
			window.location = '/order/address/';
		} else {
			$('membersCheckoutUserNotFound').show();
		}
	}
}

function doSignup(theForm) {
	if (!$('signUpForm')) return false;
	if (Summit.Form.ValidateForm(theForm)) {
		if (theForm['password'].value!=theForm['passwordConf'].value) {
			alert('Confirmation password is incorrect');
		} else {
			send(AJAX_URL + '?do=shopSignup&' + $('signUpForm').serialize(), 'signupResponse');
		}
	}
}

function signupResponse() {
	if (XmlHttp.readyState==4){
		result = XmlHttp.responseText;
		XmlHttp=null;
		if (result==1) {
			window.location = '/order/address/';
		} else {
			alert('Error: unable to create an account.');
		}
	}
}

function validateEmail(el) {
	if (!el.value) return true;
	if (!$('validateEmailResponse')) return false; 
	theForm = el.form;
	theField = el.name;
	send(AJAX_URL + '?do=validateEmail&email=' + el.value, 'validateEmailResponse');
}

function validateEmailResponse() {
	if (XmlHttp.readyState==4){
		result = XmlHttp.responseText;
		XmlHttp=null;
		if (result==1) {
			$('validateEmailResponse').hide();
		} else {
			$('validateEmailResponse').show();
		}
	}
}

function recalculateOrder(el) {
	var theForm = document.forms['orderFrm'];
	if (!theForm) return false;
	var subTotal = theForm['subTotal'].value*1;
	var sShipping = 'N/A'; var sTax1 = 'N/A'; var sTax2 = 'N/A'; var sTotal = 'N/A';
	var vShipping = 0; var vTax1 = 0; var vTax2 = 0; var vTotal = 0;
	var shipRadio = validateRadioButton(theForm['shippingType']);
	if (shipRadio>0) {
		shipRadio--;
		vShipping = theForm['shippingType'][shipRadio].getAttribute('shipPrice')*1;
		if (vShipping===0)
			sShipping = 'FREE';
		else
			sShipping = '$' + vShipping;
	}
	if (vShipping>=0) {
		vTotal = subTotal + vShipping;
		if (theForm['tax1prcnt']) {
			if ((theForm['tax1prcnt'].value*1)>0) {
				vTax1 = vTotal * (theForm['tax1prcnt'].value*1) / 100;
				vTax1 = Summit.Form.MakeCurrency(vTax1)*1;
				sTax1 = '$' + Summit.Form.MakeCurrency(vTax1);
				if (theForm['tax2prcnt']) {
					if ((theForm['tax2prcnt'].value*1)) {
						if (theForm['taxado'].value*1) {
							vTax2 = (vTotal + vTax1) * (theForm['tax2prcnt'].value*1) / 100;
						} else {
							vTax2 = vTotal * (theForm['tax2prcnt'].value*1) / 100;
						}
						vTax2 = Summit.Form.MakeCurrency(vTax2)*1;
						sTax2 = '$' + Summit.Form.MakeCurrency(vTax2);
					}
				}
			}
		}
		vTotal += vTax1; vTotal += vTax2;
		vTotal = Summit.Form.MakeCurrency(vTotal);
		sTotal = '$' + vTotal;
		theForm['shipping'].value = vShipping;
		if (theForm['tax1']) theForm['tax1'].value = vTax1;
		if (theForm['tax2']) theForm['tax2'].value = vTax2;
		theForm['total'].value = vTotal;
	}
	if ($('sShipping')) $('sShipping').update(sShipping);
	if ($('sTax1')) $('sTax1').update(sTax1);
	if ($('sTax2')) $('sTax2').update(sTax2);
	if ($('sTotal')) $('sTotal').update(sTotal);
}

function validatePlaceOrder(theForm) {
	var res = false;
	res = confirm('The order will now be placed.\nContinue?');
	return res;
/*	if (validateRadioButton(theForm['shippingType'])>0) {
		if (Summit.Form.ValidateForm(theForm)) {
			if (theForm['total'].value*1>0)
				res = confirm('The order will now be placed.\nContinue?');
		}
	} else {
		alert('Please select shipping type.')
	}
	return res; */
}

function checkPaypalStatus() {
	var res = false;
	send(AJAX_URL + '?do=checkPaypalResponse&orderID=' + validateOrderID, 'checkPaypalStatusResponse');
	return res;
}

function checkPaypalStatusResponse() {
	if (XmlHttp.readyState==4){
		result = XmlHttp.responseText;
		XmlHttp=null;
		if (result=='1') {
			window.location = '/actions/finalize/';
		} else {
			setTimeout('checkPaypalStatus()', 1000);
		}
	}
}
