function setFormat(num){
	num = String(num)
	if (num.indexOf('.')==-1){
		num=num+".00"
	}else{
		if (num.indexOf('.')==num.length-2){
			num=num+"0"
		}//end if
	}//end if
	var news = num.replace(/^([\d,]+)\.(\d\d)\d*/,"$1.$2")
	return news
}//end setFormat

function calculateLinePrice(f, line){
	amount = parseFloat(document.getElementById("price"+line).innerHTML)*parseInt(f["qty"+line].value)
	if (isNaN(amount)){
		amount = 0
	}//end if
	if (amount>0){
		f["amount"+line].value = setFormat(amount)
	}else{
		f["amount"+line].value = ""
	}//end if
	return amount
}//end calculateLinePrice

function isNumber(val){
	var e = window.event
	with(e){
		if(((keyCode>=48)&&(keyCode<=57)&&shiftKey==false)||((keyCode>=96)&&(keyCode<=105))||(keyCode==8)||(keyCode==9)||(keyCode==37)||(keyCode==39)||(keyCode==13)){
			if (((keyCode==48)||(keyCode==96))&&(val.length==0)){
				return false;
			}
			return true;
		}else{
			return false;
		}//end if
	}
}//end isNumber
function isNumberZ(val){
	var e = window.event
	with(e){
		if(((keyCode>=48)&&(keyCode<=57)&&shiftKey==false)||((keyCode>=96)&&(keyCode<=105))||(keyCode==8)||(keyCode==9)||(keyCode==37)||(keyCode==39)||(keyCode==13)){
			return true;
		}else{
			return false;
		}//end if
	}
}//end isNumber
function getTotalCrates(f){
	var crates = 0
	for (var i = 1; i<=WINE_COUNT; i++){
		var qty = parseInt(f["qty"+i].value)
		if (!isNaN(qty)) crates+=qty
	}//end for
	return crates
}//end getTotalCrates

function calculateTotalPrice(f){
	var subtotal = 0
	var crates = getTotalCrates(f)
	for (var i = 1; i<=WINE_COUNT; i++){
		subtotal+=calculateLinePrice(f, i)
	}//end for
	f["subtotal"].value=setFormat(subtotal)
	var discount = 0
	if (crates>=DISCOUNT_COUNT){
		discount = DISCOUNT_RATE*subtotal
	}//end if
	discount = parseFloat(setFormat(discount))
	f["discount"].value=setFormat(discount)
	var vat = parseFloat(setFormat((subtotal-discount)*VAT))
	f["vat"].value=setFormat(vat)
	var total = setFormat(subtotal-discount+vat)
	f["total"].value = total
	return total
}//end calculateTotalPrice

function resetPrice(f){
	for (var i = 1; i<=WINE_COUNT; i++){
		f["qty"+i].value=""
		f["amount"+i].value=""
	}//end for
	f["subtotal"].value=""
	f["discount"].value=""
	f["vat"].value=""
	f["total"].value = ""
}//end resetPrice

function disableCheck(f, isCheck){
	f.checknumber.disabled = !isCheck
	document.getElementById("checktitle").style.color = (!isCheck?"gray":"black")
}//end disableCheck

function onMethodChange(f){
	var isCard = (f.cardtype.value>0)&&(f.cardtype.value!="cheque")
	disableCheck(f, !isCard)
	disableCard(f, (f.cardtype.value!="cheque"))
}

function onChequeChange(f){
	var isCard = f.checknumber.value.length==0
	disableCard(f, false)
	if (!isCard){
		setSelect(f.cardtype, "cheque")
	}
}

function onByCheque(ch){
	var f = ch.form
	disableCard(f, !ch.checked)
	disableCheck(f, ch.checked)
}

function disableCard(f, isCard){
	f.cardnumber.disabled = !isCard
	f.cardvalidmonth.disabled = !isCard
	f.cardvalidyear.disabled = !isCard
	f.cardexpirymonth.disabled = !isCard
	f.cardexpiryyear.disabled = !isCard
	f.cardsec.disabled = !isCard
	f.cardtype.disabled = !isCard
	var st = !isCard?"gray":"black"
	document.getElementById("cardnumtitle").style.color = st
	document.getElementById("cardvalidtitle").style.color = st
	document.getElementById("cardexptitle").style.color= st
	document.getElementById("cardsectitle").style.color = st
}

function setSelect(sel, value){
	var o = sel.options
	for (var i=0;i<o.length;i++){
		if (o[i].value==value){
		 	sel.selectedIndex = i
		 }
	}
}

String.prototype.trim = function(){
	var s = this.toString()
	return s.replace(/^\s+|\s+$/g, "")
}//end trim

function checkNameReq(fd){
	if (fd){
		var name = fd.value.trim();
		fd.value = name;
		if(name.length==0){
			return false
		}//end if
	}//end if
	return true
}//end checkNameReq

function checkEmail(fd){
	if (fd){
		var email= fd.value.trim();
		fd.value = email;
		if(!/^[a-zA-Z0-9\.\-_]+\@([a-zA-Z0-9\-\_]+\.)+[a-zA-Z0-9]{2,4}$/.test(email)){
			return false;
		}//end if
	}//end if
	return true;
}//end checkEmail

function showErr(id, err){
	if (err){
		document.getElementById(id).style.color='red';
	}else{
		document.getElementById(id).style.color='black';
	}//end if
}//end showErr

function validate(f){
	var err=""
	var crates = getTotalCrates(f)
	if (!(crates>0)){
		err+="Please choose ordered quantities\r\n";
		showErr("qtytitle", true)
	}else{
		showErr("qtytitle", false)
	}//end if
	if (!checkNameReq(f.firstname)){
		err+="First Name is required\r\n";
		showErr("firstnametitle", true)
	}else{
		showErr("firstnametitle", false)
	}//end if
	if (!checkNameReq(f.surname)){
		err+="Surname is required\r\n";
		showErr("surnametitle", true)
	}else{
		showErr("surnametitle", false)
	}//end if
	if (!checkNameReq(f.address)){
		err+="Address is required\r\n";
		showErr("addresstitle", true)
	}else{
		showErr("addresstitle", false)
	}//end if
	if (!checkNameReq(f.town)){
		err+="Town is required\r\n";
		showErr("townetitle", true)
	}else{
		showErr("townetitle", false)
	}//end if
	if (!checkNameReq(f.postcode)){
		err+="Post Code is required\r\n";
		showErr("postcodetitle", true)
	}else{
		showErr("postcodetitle", false)
	}//end if
	if (!checkNameReq(f.phone)){
		err+="Phone is required\r\n";
		showErr("phonetitle", true)
	}else{
		showErr("phonetitle", false)
	}//end if
	if (checkNameReq(f.email)&&!checkEmail(f.email)){
		err+="Wrong email format\r\n";
		showErr("emailtitle", true)
	}else{
		showErr("emailtitle", false)
	}//end if
	if (f.cardtype.value>0){
		if (!checkNameReq(f.cardnumber)){
			err+="Card Number is required\r\n";
			showErr("cardnumtitle", true)
		}else{
			showErr("cardnumtitle", false)
		}//end if
		var today = new Date()
		var current = (today.getFullYear()-2000)*12+today.getMonth()+1
		if (!checkNameReq(f.cardvalidmonth)||!checkNameReq(f.cardvalidyear)){
			err+="Card Valid Date is required\r\n";
			showErr("cardvalidtitle", true)
		}else{
			if (parseInt(f.cardvalidmonth.value)>12){
				err+="Wrong Card Valid Date\r\n";
				showErr("cardvalidtitle", true)
			}else{
				var valid = parseInt(f.cardvalidyear.value)*12+parseInt(f.cardvalidmonth.value)
				if (valid>current){
					err+="Card Valid Date should be in the past\r\n";
					showErr("cardvalidtitle", true)
				}else{
					showErr("cardvalidtitle", false)
				}//end if
			}//end if
		}//end if
		if (!checkNameReq(f.cardexpirymonth)||!checkNameReq(f.cardexpiryyear)||(parseInt(f.cardexpirymonth.value)>12)){
			err+="Card Expiry Date is required\r\n";
			showErr("cardexptitle", true)
		}else{
			if (parseInt(f.cardexpirymonth.value)>12){
				err+="Wrong Card Expiry Date\r\n";
				showErr("cardexptitle", true)
			}else{
				var expir = parseInt(f.cardexpiryyear.value)*12+parseInt(f.cardexpirymonth.value)
				if (expir<current){
					err+="Card is expired\r\n";
					showErr("cardexptitle", true)
				}else{
					showErr("cardexptitle", false)
				}//end if
			}//end if
		}//end if
		if (!checkNameReq(f.cardsec)){
			err+="Card Security Code is required\r\n";
			showErr("cardsectitle", true)
		}else{
			showErr("cardsectitle", false)
		}//end if
	}else{
		if (!checkNameReq(f.checknumber)){
			err+="Payment Method is undefined\r\n";
		}//end if
	}//end if
	if (err.length>0){
		alert(err);
		return false;
	}//end if
	return confirm(getOrderText(f)+"\r\nSubmit the order?")
}//end validate

function getOrderText(f){
	var text = "Order Details:\r\n"
	for (var i = 1; i<=WINE_COUNT; i++){
		var qty = parseInt(f["qty"+i].value)
		if (!isNaN(qty)){
			text+="   "+document.getElementById("wine"+i).innerHTML+" - "+f["qty"+i].value+"x"+document.getElementById("price"+i).innerHTML+" = "+f["amount"+i].value+"\r\n"
		}//end if
	}//end for
	text += "Total Price: "+f["total"].value+"\r\n"
	text += "\r\nDelivery Details:\r\n"
	text += "   First Name: "+f.firstname.value+"\r\n"
	text += "   Surname: "+f.surname.value+"\r\n"
	text += "   Address: "+f.address.value+"\r\n"
	text += "   Town: "+f.town.value+"\r\n"
	text += "   Post Code: "+f.postcode.value+"\r\n"
	text += "   Phone: "+f.phone.value+"\r\n"
	if (f.email.value.length>0){
		text += "   Email:"+f.email.value+"\r\n"
	}//end if
	text += "\r\nPayment Details:\r\n"
	if (f.cardtype.value>0){
		text += "   Card: "+f.cardtype.options[f.cardtype.selectedIndex].text+"\r\n"
		text += "   Card Number: "+f.cardnumber.value+"\r\n"
		text += "   Valid From: "+f.cardvalidmonth.value+"/"+f.cardvalidyear.value+"\r\n"
		text += "   Expiry Date: "+f.cardexpirymonth.value+"/"+f.cardexpiryyear.value+"\r\n"
		text += "   Security Digits: "+f.cardsec.value+"\r\n"
	} else{
		text += "   Check Number: "+f.checknumber.value+"\r\n"
	}//end if
	return text
}//end getOrderText
