function calcfreight(area, fullcase, halfcase) {

     if(halfcase > 0){
     var tot_bot_case = (halfcase / 12);
	 
     var fin = parseInt(tot_bot_case);
	
     var end_case_bot = (halfcase % 12);

     if(end_case_bot > 0){

     var frei_charge_num = (fin + 1);

     var frei = parseInt(frei_charge_num);

     } else{
		frei = fin;
	 }

     }

     else{

     frei = 0;

     

     }

     var mel = 6.00;

     var mel_half=6.00;

     

     var reg = 12.00;

     var reg_half=12.00;

     

     var sa = 20.00;

     var sa_half=20.00;

     

     var qld = 25.00;

     var qld_half=25.00;

if(area == 1){  

     var fulltot = (fullcase * mel);     

     var halftot = (mel_half * frei);     

     var alltot = (fulltot + halftot);     

  }

  else if(area == 2){

     var fulltot = (fullcase * reg);     

     var halftot = (reg_half * frei);     

     var alltot = (fulltot + halftot);     



  }

  else if(area == 3){

     var fulltot = (fullcase * sa);     

     var halftot = (sa_half * frei);     

     var alltot = (fulltot + halftot);     

  }

  else if(area == 4){

     var fulltot = (fullcase * qld);     

     var halftot = (qld_half * frei);     

     var alltot = (fulltot + halftot);     

  }

  else{

     var alltot = 0.00;     

  }

  return alltot;



}

function isInteger(value) {

  return (parseInt(value) == value);

}



function validatenums(){

   		<!--2008 Pinot Gris - Full Case-->
   if(document.form1.pinotgris08_case_qnt.value.length > 0){

     if(!isInteger(document.form1.pinotgris08_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.pinotgris08_case_qnt.value = ''; }
     }
	 
	 	<!--2008 Pinot Gris - Half Case -->
	if(document.form1.pinotgris08_half_qnt.value.length > 0){

     if(!isInteger(document.form1.pinotgris08_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.pinotgris08_half_qnt.value = '';

      }

     if(document.form1.pinotgris08_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.pinotgris08_half_qnt.value = '';

      }

   }
 
 		<!--2005 Sauvignon Blanc - Full Case -->
 	if(document.form1.sauv_case_qnt.value.length > 0){

     if(!isInteger(document.form1.sauv_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.sauv_case_qnt.value = ''; }
     }
	 
	 	<!--2005 Sauvignon Blanc - Half Case -->
	if(document.form1.sauv_half_qnt.value.length > 0){

     if(!isInteger(document.form1.sauv_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.sauv_half_qnt.value = '';

      }

     if(document.form1.sauv_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.sauv_half_qnt.value = '';

      }

   }   
   
   <!--2008 Sauvignon Blanc - Full Case -->
 	if(document.form1.sauv08_case_qnt.value.length > 0){

     if(!isInteger(document.form1.sauv08_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.sauv08_case_qnt.value = ''; }
     }   
   
	<!--2008 Sauvignon Blanc - Half Case -->
	if(document.form1.sauv08_half_qnt.value.length > 0){

     if(!isInteger(document.form1.sauv08_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.sauv08_half_qnt.value = '';

      }

     if(document.form1.sauv08_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.sauv08_half_qnt.value = '';

      }

   }      
   
   
   <!-- 2005 Chardonnay - Full Case -->
 	if(document.form1.chard_case_qnt.value.length > 0){

     if(!isInteger(document.form1.chard_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.chard_case_qnt.value = ''; }
     }  
	<!-- 2005 Chardonnay - Half Case -->
	if(document.form1.chard_half_qnt.value.length > 0){

     if(!isInteger(document.form1.chard_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.chard_half_qnt.value = '';

      }

     if(document.form1.chard_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.chard_half_qnt.value = '';

      }

   }      
  
   <!--2006 Chardonnay - Full Case -->
 	if(document.form1.chard06_case_qnt.value.length > 0){

     if(!isInteger(document.form1.chard06_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.chard06_case_qnt.value = ''; }
     }   
	 
	<!-- 2006 Chardonnay - Half Case -->
	if(document.form1.chard06_half_qnt.value.length > 0){

     if(!isInteger(document.form1.chard06_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.chard06_half_qnt.value = '';

      }

     if(document.form1.chard06_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.chard06_half_qnt.value = '';

      }

   }     
   
   <!--2005 Rose - Full Case-->
	if(document.form1.rose_case_qnt.value.length > 0){

     if(!isInteger(document.form1.rose_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.rose_case_qnt.value = ''; }
     }   
	 
	<!-- 2005 Rose - Half Case -->
	if(document.form1.rose_half_qnt.value.length > 0){

     if(!isInteger(document.form1.rose_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.rose_half_qnt.value = '';

      }

     if(document.form1.rose_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.rose_half_qnt.value = '';

      }

   }
   
   <!--2006 Pinot Noir - Full Case -->
	if(document.form1.pin_case_qnt.value.length > 0){

     if(!isInteger(document.form1.pin_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.pin_case_qnt.value = ''; }
     }   
	 
	<!-- 2006 Pinot Noir - Half Case -->
	if(document.form1.pin_half_qnt.value.length > 0){

     if(!isInteger(document.form1.pin_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.pin_half_qnt.value = '';

      }

     if(document.form1.pin_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.pin_half_qnt.value = '';

      }

   }   
       
   <!--2005 Shiraz - Full Case -->
	if(document.form1.shiraz05_case_qnt.value.length > 0){

     if(!isInteger(document.form1.shiraz05_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.shiraz05_case_qnt.value = ''; }
     }   
	 
	<!-- 2005 Shiraz - Half Case -->
	if(document.form1.shiraz05_half_qnt.value.length > 0){

     if(!isInteger(document.form1.shiraz05_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.shiraz05_half_qnt.value = '';

      }

     if(document.form1.shiraz05_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.shiraz05_half_qnt.value = '';

      }

   }         
   <!--2005 Merlot - Full Case -->
   if(document.form1.merlot_case_qnt.value.length > 0){

     if(!isInteger(document.form1.merlot_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.merlot_case_qnt.value = ''; }
     }   
	 
	<!-- 2005 Merlot - Half Case -->
	if(document.form1.merlot_half_qnt.value.length > 0){

     if(!isInteger(document.form1.merlot_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.merlot_half_qnt.value = '';

      }

     if(document.form1.merlot_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.merlot_half_qnt.value = '';

      }

   }         
   <!--2005 Quatrain - Full Case -->
	if(document.form1.quatrain_case_qnt.value.length > 0){

     if(!isInteger(document.form1.quatrain_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.quatrain_case_qnt.value = ''; }
     }   
	 
	<!-- -2005 Quatrain  - Half Case -->
	if(document.form1.quatrain_half_qnt.value.length > 0){

     if(!isInteger(document.form1.quatrain_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.quatrain_half_qnt.value = '';

      }

     if(document.form1.quatrain_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.quatrain_half_qnt.value = '';

      }

   }            
   <!--2005 Petit Verdot-->
   if(document.form1.petitVerdot_case_qnt.value.length > 0){

     if(!isInteger(document.form1.petitVerdot_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.petitVerdot_case_qnt.value = ''; }
     }   
	 
	<!-- 2005 Merlot - Half Case -->
	if(document.form1.petitVerdot_half_qnt.value.length > 0){

     if(!isInteger(document.form1.petitVerdot_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.petitVerdot_half_qnt.value = '';

      }

     if(document.form1.petitVerdot_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.petitVerdot_half_qnt.value = '';

      }

   }         
   <!--2005 Keith Charles Vintage Port - Full Case -->
    if(document.form1.port_case_qnt.value.length > 0){

     if(!isInteger(document.form1.port_case_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.port_case_qnt.value = ''; }
     }   
	 
	<!-- 2005 Keith Charles Vintage Port - Half Case -->
	if(document.form1.port_half_qnt.value.length > 0){

     if(!isInteger(document.form1.port_half_qnt.value)) {

      alert("You have not entered a valid number. Please enter a valid number.");

      document.form1.port_half_qnt.value = '';

      }

     if(document.form1.port_half_qnt.value >= 12) {

      alert("You have selected a full case or more than a full case. Please select full cases then the remainder as bottles should you require it.");

      document.form1.port_half_qnt.value = '';

      }

   }         

}

function validate(){

   

if(document.form1.total.value.length < 1){

      alert("You have not selected wine. Please select a wine.");

      return false;



   }

if(document.form1.freight_cost.value.length < 1){

      alert("You have not selected a delivery area. Please select a delivery area.");

      return false;



   }

   

if(document.form1.name.value.length < 1){

      alert("You have not entered your name. Please enter your name.");

      return false;



   }

   if(document.form1.email.value.length < 1){

      alert("You have not entered your email address. Please enter your email address.");

     return false;



   }

   if(document.form1.phone.value.length < 1){

      alert("You have not entered your phone number. Please enter your phone number.");

     return false;



   }   

   if(document.form1.cardnumber.value.length < 1){

      alert("You have not entered your card number. Please enter your card number.");

     return false;



   }   

   if(document.form1.ccv.value.length < 1){

      alert("You have not entered your 3 digit validation number. Please enter your card validation number number.");

     return false;



   }   

 return true;

}





function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;

}



function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}



function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}



function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}



function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function YY_CalculateForm() { 
  var args=YY_CalculateForm.arguments;
  var myResult=""; var myField="0";
  if (document.all){eval("args[0]=args[0].replace(/.layers/gi, '.all');");}
  for (var i=3; i<args.length; i++){
    if(args[i].charAt(0)!="#"){
      myResult=myResult+args[i];
    }else{
      myField=eval(args[0]+'.'+ args[i].substring(1)+'.value'); if(myField=="")myField="0";
      myResult=myResult+myField;
    }
  }
  with (Math) {
    var myExpo = pow(10, args[2]);
    var myResult = round(myExpo*eval(myResult)/1)/myExpo+"";
    myResult += (myResult.indexOf('.')==-1)?".":"";
    var missingZ = (args[2] - myResult.length + myResult.indexOf('.') + 1);
    for (var i=0; i<missingZ; i++){ myResult += "0" }
  }
  var myReturn = eval(args[0]+'.'+args[1]);
  myReturn.value = myResult;
}


function YY_checkform() { 
  var args = YY_checkform.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;
  var myForm = MM_findObj(args[0]);
  for (var i=1; i<args.length;i=i+4){
    if (args[i+1].charAt(0)=='#'){var myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
    var myObj = MM_findObj(args[i].replace(/\[\d+\]/ig,""));
    myV=myObj.value;
    if (myObj.type=='text'||myObj.type=='password'){
      if (myReq&&myObj.value.length==0){addErr=true}
      if ((myV.length>0)&&(args[i+2]==1)){ //fromto
        if (!(myV/1)||myV<args[i+1].split('_')[0]/1||myV > args[i+1].split('_')[1]/1){addErr=true}
      }
      if ((myV.length>0)&&(args[i+2]==2)&&!myV.match("^[\\w\\.=-_]+@[\\w\\.-_]+\\.[a-z]{2,4}$")){addErr=true}// email
      if ((myV.length>0)&&(args[i+2]==3)){ // date
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);
        if(myAt){
          var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
          var myDate=new Date(myY,myM,myD);
          if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
        }else{addErr=true}
      }
      if ((myV.length>0)&&(args[i+2]==4)){ // time
        var myMa=args[i+1].split("#"); var myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
      }
      if (myV.length>0&&args[i+2]==5){ // check this 2
            var myObj1 = MM_findObj(args[i+1].replace(/\[\d+\]/ig,""));
            if(!myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,"")].checked){addErr=true}
      }
      if (myV.length>0&&args[i+2]==6){ // the same
            var myObj1 = MM_findObj(args[i+1]);
            if(myV!=myObj1.value){addErr=true}
      }
    }else
    if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
          var myTest = args[i].match(/(.*)\[(\d+)\].*/i);
          var myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
      if (args[i+2]==1&&myObj1&&myObj1.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
      if (args[i+2]==2){
        var myDot=false;
        for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
        if(!myDot){myErr+='* ' +args[i+3]+'\n'}
      }
    }else
    if (myObj.type=='checkbox'){
      if(args[i+2]==1&&myObj.checked==false){addErr=true}
      if(args[i+2]==2&&myObj.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
    }else
    if (myObj.type=='select-one'||myObj.type=='select-multiple'){
      if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
    }else
    if (myObj.type=='textarea'){
      if(myV.length<args[i+1]){addErr=true}
    }
    if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
  }
  if (myErr!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+myErr)}
  document.MM_returnValue = (myErr=='');
}





function init()
{
	$('select.CalcFreight').change(get_freight);

$('input.CuveHalf').change(Cuve);
$('input.CuveCase').change(Cuve);

$('input.PinotGrisHalf').change(PinotGris);
$('input.PinotGrisCase').change(PinotGris);

$('input.Sauv05Half').change(Sauv05);
$('input.Sauv05Case').change(Sauv05);

$('input.Sauv08Half').change(Sauv08);
$('input.Sauv08Case').change(Sauv08);
 
$('input.Chard05Half').change(Chard05);
$('input.Chard05Case').change(Chard05);
 
$('input.Chard06Half').change(Chard06);
$('input.Chard06Case').change(Chard06);

$('input.Rose05Half').change(Rose05);
$('input.Rose05Case').change(Rose05);

$('input.Pinot06Half').change(Pinot06);
$('input.Pinot06Case').change(Pinot06);
 
$('input.Shiraz05Half').change(Shiraz05);
$('input.Shiraz05Case').change(Shiraz05);

$('input.Merlot05Half').change(Merlot05);
$('input.Merlot05Case').change(Merlot05);

$('input.Quat05Half').change(Quat05);
$('input.Quat05Case').change(Quat05);

$('input.Petit05Half').change(Petit05);
$('input.Petit05Case').change(Petit05);

$('input.Port05Half').change(Port05);
$('input.Port05Case').change(Port05);
}


function get_freight()
{
	full = $('input[title="full_case"]').sum().toFixed(2);
	$('input#full_cases').val(full);
	half = $('input[title="half_case"]').sum().toFixed(2);
	freight = calcfreight($('select#area').val(), full, half);
	$('input#freight_cost').val(freight.toFixed(2));
	
	$('input#subtotal_cost').val($('input[title="total"]').sum().toFixed(2));
	$('input#total').val($("input#subtotal_cost, input#freight_cost").sum().toFixed(2));
}

function calc_total()
{
	validatenums();
	get_freight();
}

function PinotGris()
{
	$('input#pinotgris08cost').val((($('input#pinotgris08_half_qnt').val() * 16.00) + ($('input#pinotgris08_case_qnt').val() * 176.00)).toFixed(2));
	calc_total();
}

function Sauv05()
//This is really for Rose 2009, just couldn't be bothered changing all the variables and function references
{
	$('input#sauvcost').val((($('input#sauv_half_qnt').val() * 15.00) + ($('input#sauv_case_qnt').val() * 165.00)).toFixed(2));
	calc_total();
}
	

function Sauv08()
{
	$('input#sauv08cost').val((($('input#sauv08_half_qnt').val() * 16.00) + ($('input#sauv08_case_qnt').val() * 176.00)).toFixed(2));
	calc_total();
}

function Chard05()
{
	$('input#chardcost').val((($('input#chard_half_qnt').val() * 20.00) + ($('input#chard_case_qnt').val() * 220.00)).toFixed(2));
	calc_total();
}

function Chard06()
{
	$('input#chard06cost').val((($('input#chard06_half_qnt').val() * 28.00) + ($('input#chard06_case_qnt').val() * 308.00)).toFixed(2));
	calc_total();
}

function Rose05()
//This is really for Cab 2006, just couldn't be bothered changing all the variables and function references
{
	$('input#rosecost').val((($('input#rose_half_qnt').val() * 18.00) + ($('input#rose_case_qnt').val() * 198.00)).toFixed(2));
	calc_total();
}

function Pinot06()
{
	$('input#pincost').val((($('input#pin_half_qnt').val() * 25.00) + ($('input#pin_case_qnt').val() * 275.00)).toFixed(2));
	calc_total();
}

function Merlot05()
{
	$('input#merlotcost').val((($('input#merlot_half_qnt').val() * 32.00) + ($('input#merlot_case_qnt').val() * 350.00)).toFixed(2));
	calc_total();
}

function Shiraz05()
{
	$('input#shiraz05cost').val((($('input#shiraz05_half_qnt').val() * 25.00) + ($('input#shiraz05_case_qnt').val() * 275.00)).toFixed(2));
	calc_total();
}

function Quat05()
{
	$('input#quatraincost').val((($('input#quatrain_half_qnt').val() * 35.00) + ($('input#quatrain_case_qnt').val() * 385.00)).toFixed(2));
	calc_total();
}

function Petit05()
{
	$('input#petitVerdotcost').val((($('input#petitVerdot_half_qnt').val() * 25.00) + ($('input#petitVerdot_case_qnt').val() * 275.00)).toFixed(2));
	calc_total();
}

function Port05()
{
	$('input#portcost').val((($('input#port_half_qnt').val() * 26.00) + ($('input#port_case_qnt').val() * 280.00)).toFixed(2));
	calc_total();
}

function Cuve()
{
	$('input#cuvecost').val((($('input#cuve_half_qnt').val() * 35.00) + ($('input#cuve_case_qnt').val() * 385.00)).toFixed(2));
	calc_total();
}