function TrimChar (inputString, removeChar){
var returnString = inputString;
if (removeChar.length){
while(''+returnString.charAt(0)==removeChar){
returnString=returnString.substring(1,returnString.length);
}
while(''+returnString.charAt(returnString.length-1)==removeChar){ returnString=returnString.substring(0,returnString.length-1); 
  }
 }
return returnString;
}

//=========================================
function CheckEmail(Fld) {
var tmp;
Fld.value=TrimChar(Fld.value," ");
tmp=Fld.value;
if (tmp=="") { 
alert("Please provide the email address");
Fld.focus(); return false;
   }
else if ( (tmp.indexOf('@',0)==-1) || (tmp.indexOf('.')== -1 ) ){ 
alert("Invalid email address format!");
Fld.focus(); 
return false;
   }
else {return true; }
}

//=======================================
function CheckField(Fld,Msge){
Fld.value=TrimChar(Fld.value," ");
if(TrimChar(Fld.value," ")==""){
alert(Msge);Fld.focus(); return false;} 
else { return true;}  
}

//==========================================================
function CheckNumber(Fld,Fldname){
var tmp;
Fld.value=TrimChar(Fld.value," ");
tmp=Fld.value;
if(tmp==""){ alert("Please provide "+Fldname+"?");  Fld.focus();return false;}
else if (isNaN(tmp)){ 
alert("Invalid Entry for "+ Fldname+"!\n"+
      "Must be number. No comma please!"); 
Fld.focus();  
return false;} 
else{ return true;}
}

//===================================================
function GetOrderPrice(qty, bagtype){
var k, price;
price=0.00;

k=parseInt(qty);

if(bagtype=='1/2') {document.paypalform.amount.value=5; }
if(bagtype=='3/4') {document.paypalform.amount.value=9; }
document.paypalform.quantity.value=qty;

return true;
}



//===================================================
function CheckOrder(){
var tmp, tmp1, bagtype;

tmp=eval('document.orderform.coffee_origin');

k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee origin');
tmp.focus();
return false;
}
else if(k=='Bella Blend'){
//Bella Blend only 3/4lbs bags
document.orderform.coffeebag.selectedIndex=2;
}


tmp=eval('document.orderform.roast_level');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee roast level');
tmp.focus();
return false;
}


tmp=eval('document.orderform.bean_option');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee bean option');
tmp.focus();
return false;
}


tmp=eval('document.orderform.coffeebag');
bagtype=tmp.options[tmp.selectedIndex].value;

if(bagtype==""){
	alert("Please select a coffee bag");
	tmp.focus();
	return false;
	}

tmp=eval('document.orderform.item_qty');
k=tmp.value;
if(!CheckNumber(tmp, 'quantity')){tmp.focus(); return false;  }

document.orderform.submit();
return true;
}


//================================================
function OrderTShirt(){
var k, tmp, tmp1, tmp2, price;

tmp=eval('document.orderform.tshirt_color');
k=tmp.options[tmp.selectedIndex].value;

if(k==''){
	alert('Please select the T-Shirt Color');
	tmp.focus();
	return false;
}


tmp=eval('document.orderform.tshirt_size');
k=tmp.options[tmp.selectedIndex].value;

if(k==''){
	alert('Please select the T-Shirt Size');
	tmp.focus();
	return false;
}


tmp=eval('document.orderform.item_qty');
if(!CheckNumber(tmp, 'quantity')){ return false; }
else {tmp.value=parseInt(tmp.value); }

document.orderform.submit();
return true;
	
}


//===================================================

function GetRecurrentPrice(){
var tmp, tmp1, tmp2, j, k;
tmp=eval('document.paypalform.a3');

tmp1=eval('document.orderform.delivery_location');
j=tmp1.selectedIndex;

tmp2=eval('document.orderform.item_qty');
k=tmp2.options[tmp2.selectedIndex].value;


     if(j==1 && k==1){tmp.value=16;  } // inside SF && 1 lb
else if(j==1 && k==2){tmp.value=30;  } // inside SF && 2 lbs
else if(j==2 && k==1){tmp.value=24;  } // outside SF 1 lb
else if(j==2 && k==2){tmp.value=44;  } // outside SF 2 lbs

	
return true;	
}

//===================================================
function CheckRecurrentOrder(){
var tmp;

tmp=eval('document.orderform.coffee_origin');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee origin');
tmp.focus();
return false;
}


tmp=eval('document.orderform.roast_level');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee roast level');
tmp.focus();
return false;
}

tmp=eval('document.orderform.bean_option');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the coffee bean option');
tmp.focus();
return false;
}


tmp=eval('document.orderform.delivery_location');
k=tmp.options[tmp.selectedIndex].value;
if(k==''){
alert('Please select the delivery location');
tmp.focus();
return false;
}

tmp=eval('document.orderform.bags');
k=tmp.options[tmp.selectedIndex].value;

if(k==''){
	alert('Please select the quantity'); 
	tmp.focus();
return false; 
}

document.orderform.submit();
return true;
}


//=============================================
function GetMug(){
var tmp=eval('document.orderform.item_qty');
if(!CheckNumber(tmp, 'quantity')){return false;}
else { tmp.value=parseInt(document.orderform.item_qty.value) }

var tmp=eval('document.orderform.item_number');
k=tmp.options[tmp.selectedIndex].value;

if(k==''){ alert("Please choose a mug"); tmp.focus(); return false; }

document.orderform.submit();
return true;
}

