/*================================================================================================================================
Company: AppleTreeSoft
Application: javascript utility
Author: Jacob Park
Description: common validation 
Dates: 4/20/2006
==================================================================================================================================*/


function ValidateContact(frm){

	if (frm.CustName.value == "") {
		alert("Please enter your name.");
		frm.CustName.focus();
		return false;
	}

	if (frm.Email.value == "") {
		alert("Please enter your e-mail address.");
		frm.Email.focus();
		return false;
	}
	
	if (!CheckEmail(frm.Email.value))  {
		frm.Email.select();
		frm.Email.focus();
		return false;
	}

	if (frm.Phone.value == "") {
		alert("Please enter your main phone number.");
		frm.Phone.focus();
		return false;
	}

	if (frm.Subject.value == "") {
		alert("Please enter the subject you would like to discuss about.");
		frm.Subject.focus();
		return false;
	}
	
	if (frm.Msg.value == "") {
		alert("Please enter the message you would like to discuss about.");
		frm.Msg.focus();
		return false;
	}

	return true;
}
	
	
	
	
function ValidateCust(frm) {

	if (frm.CompanyName.value == "") {
		alert("Please enter company name.");
		frm.CompanyName.focus();
		return false;
	}
	
	if (frm.ContactName.value == "") {
		alert("Please enter contact name.");
		frm.ContactName.focus();
		return false;
	}
	
	if (frm.Street.value == "") {
		alert("Please enter your street.");
		frm.Street.focus();
		return false;
	}
	
	if (frm.City.value == "") {
		alert("Please enter your city.");
		frm.City.focus();
		return false;
	}
	
	if (frm.State.selectedIndex == 0) {
		alert("Please select your state/province.");
		frm.State.focus();
		return false;
	}
		
	if (frm.Zip.value == "") {
		alert("Please enter your zip/postal code.");
		frm.Zip.focus();
		return false;
	}
		
	if (frm.Phone.value == "") {
		alert("Please enter your office phone number.");
		frm.Phone.focus();
		return false;
	}
	
	if (frm.Fax.value == "") {
		alert("Please enter your office fax number.");
		frm.Fax.focus();
		return false;
	}
	
	if (frm.Email.value != "") {
		if (!CheckEmail(frm.Email.value))  {
			frm.Email.select();
			frm.Email.focus();
			return false;
		}
	}

	return true;
}




//-----------------------------------------------------------------------------------------
function ValidateLogin(frm){

	if (frm.Email.value == "") {
		alert("Please enter your e-mail address.");
		frm.Email.focus();
		return false;
	}
	
	if (!CheckEmail(frm.Email.value))  {
		frm.Email.select();
		frm.Email.focus();
		return false;
	}


	if (frm.Pwd.value == "") {
		alert("Please enter your password.");
		frm.Pwd.focus();
		return false;
	}
	
	return true;
}


//-----------------------------------------------------------------------------------------






//***********************************************/
// functions for displaying larger images...
function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function overlay(curobj, subobj, offset){
if (document.getElementById){
var subobj=document.getElementById(subobj)
subobj.style.left=getposOffset(curobj, "left")+offset+"px"
subobj.style.top=getposOffset(curobj, "top")+offset+"px"
subobj.style.display="block"
return false
}
else
return true
}

function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}
//***********************************************/





//functions for calculing sub total
function Calc(i) {	
	var frm = document.frm;
	var ItemDiv = parseInt(frm.ItemDiv[i].value);
	var OriginalSellPrice = frm.OriginalSellPrice[i].value;
	var SellPrice = frm.SellPrice[i].value;
	var Qty = frm.Qty[i].value;
	var Instock = parseInt(frm.Instock[i].value);
	var LineAmt = 0;
	
	if ((SellPrice != "") && (Qty != "")) {
		if ((!isNaN(SellPrice)) && (!isNaN(Qty))) {
			if (parseInt(Qty) > Instock) {
				alert('Order quantity is greater than available quantity instock. \nPlease adjust the order quantity entered.');
				
				frm.Qty[i].value = Instock;
				frm.Qty[i].focus();
				frm.Qty[i].select();
			}
			Qty = frm.Qty[i].value;
			if (ItemDiv == 2){
				if (parseInt(Qty) <= 5){
					SellPrice = OriginalSellPrice;
				}
				else if (parseInt(Qty) >=6 && parseInt(Qty) <= 11){
					SellPrice = OriginalSellPrice * 0.95;
				}
				else if (parseInt(Qty) >= 12){
					SellPrice = OriginalSellPrice * 0.9;
				}
			}else{
				SellPrice = OriginalSellPrice;
			}
			frm.SellPrice[i].value = FormatCurrency(SellPrice);
			frm.LineAmt[i].value = FormatCurrency(FormatCurrency(SellPrice)*Qty);
			
			CalcTotal();
		}
		else {
			alert ('Please enter numeric characters for the quantity field.');
			frm.Qty[i].value = '';
			frm.Qty[i].focus();
			frm.Qty[i].select();
		}
	}
	else {
		if (Qty == ""){
			//frm.Qty[i].value = 0;
			frm.SellPrice[i].value = FormatCurrency(frm.OriginalSellPrice[i].value);
		}
		frm.LineAmt[i].value = "0.00";
		CalcTotal();
	}
}

//functions for check field if empty
function Check(i) {	
	var frm = document.frm;

	if (frm.Qty[i].value == "") {
		frm.Qty[i].value = 0;
		frm.SellPrice[i].value = FormatCurrency(frm.OriginalSellPrice[i].value);
		CalcTotal();
	}

}




//**************************************************************************************************************************
function CalcTotal() {
	var frm = document.frm;
	var SubTotal = 0;
	if (frm.SubTotal) {
		for (i=0; i < frm.LineAmt.length; i++) {
			if (frm.LineAmt[i].value != "") 
				SubTotal = FormatCurrency((parseFloat(SubTotal) + parseFloat(frm.LineAmt[i].value)));
		}

		frm.SubTotal.value =  FormatCurrency(SubTotal);
	}
}


//**************************************************************************************************************************
function Checkout() {
	var frm = document.frm;
	var SubTotal = frm.SubTotal.value;
	if (SubTotal == '0') {
		alert ('No order quantity was entered. Please enter quantity for the item you want to buy.');
		return false;
	}
/*			
	for (i=0; i < frm.LineAmt.length; i++) {
		if (frm.LineAmt[i].value != "") 
			frm.SellPrice[i].value = FormatCurrency(frm.OriginalSellPrice[i].value);
	}
*/
	return true;
}


