// JavaScript Document

function find_rate(key)
{
   for (i=0; i<keys.length; i++)
      {
	           if (keys[i] == key) break;
      }
      //alert("key " + keys[i] + " " + key_values[i]);
      return key_values[i];
}

function generate_premium_Main()
{

    /* formcheck(document.quote_customer,false);*/
    // if (b_OK)
     // {
            myform = document.quote_customer;
			var global_uplift = myform.global_uplift.value;
			//alert(global_uplift);
            // storage rate is 0.0375
            var rate = 0.0375;
            // else use the goods rate
            if (myform.transports_id.value >0)
            {
                  rate = 0.3;
                  rate = myform.goods_id.value.split('_')[1];
                  //alert(rate);
                  if (!rate)
                  {
                        rate = 0.3;
                  }
            }
            var tax1 = 0;
            tax1 = myform.quote_shipment_from_id.value.split('_')[1];
            //alert(tax1);
            if (!tax1)
            {
                  tax1 = 0;
            }
            var tax2 = 0;
            tax2 = myform.quote_shipment_to_id.value.split('_')[1];
            //alert(tax1);
            if (!tax2)
            {
                  tax2 = 0;
            }
            // ipt taxes are set only for uk, if both are set then calculate ipt due
            var tax = 0;
            if (tax1 > 0 && tax2 >0)
            {
                  tax = tax1;
            }
            // now get the rates for goods / locations
            var rate_from = 0;
            var goods_id = myform.goods_id.value.split('_')[0];
            var location_id = myform.quote_shipment_from_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
            //alert("key:" + key);
            rate_from = find_rate(key);
            //alert(rate_from);
            if (!rate_from)
            {
                  rate_from = 0;
            }
            var rate_to = 0;
            location_id = myform.quote_shipment_to_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
            rate_to = find_rate(key);
            //alert(rate_to);
            if (!rate_to)
            {
                  rate_to = 0;
            }
            var rate_shipment = 0;
            rate_shipment = (rate_from*1 > rate_to*1)? rate_from : rate_to;
            //alert("rs " + rate_shipment);
            //return false;
            var v = myform.quote_shipment_value.value;
            // rate of goods %
            var res = v*rate/100;
            //alert("rate " + rate);
            //alert("res " + res);
            // rate of shipment
            var res_shipment = v*rate_shipment/100;
            //alert("res " + res);
            res += res_shipment;
            if (res<25)
            {
                  res=25;
            }
            //alert("res " + res);
            var ipt = 0;
            myform.quote_shipment_ipt.value = 0;
            myform.quote_shipment_premium_msg.value = "";
            if (tax>0)
            {
            //alert("1");
                  // IPT tax %
                  ipt = (res*tax / 100);
                  myform.quote_shipment_ipt.value = ipt;
                  //res = res + (res*tax / 100);
                  myform.quote_shipment_premium_msg.value = "Includes " + tax + "% IPT";
            }
            if (!res>0)
            {
            //alert("2");
                  myform.quote_shipment_premium_view.value = "";
                  myform.quote_shipment_premium_orig.value = res = "";
                  myform.quote_shipment_premium.value = res = "";
                  myform.quote_shipment_ipt.value = "";
                  myform.quote_shipment_premium_msg.value = "";
                  return false;
            }
            // ipt
            numberField = ipt; // Field where the number appears
            rlength = 2; // The number of decimal places to round to
            newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
            ipt = newnumber;
            myform.quote_shipment_ipt.value = ipt;         
            //format to 2 dec places
            res_orig = res + ipt;
            // premium
            var numberField = res_orig; // Field where the number appears
            var rlength = 2; // The number of decimal places to round to
            var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
	        res_orig = newnumber;
	        res = format(res);
	        // add on ipt
            res = (res+ipt);       
            // premium
            var numberField = res; // Field where the number appears
            var rlength = 2; // The number of decimal places to round to
            var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
            res = newnumber;
//alert(res);    
            myform.quote_shipment_premium_view.value = res;
            myform.quote_shipment_premium_orig.value = res_orig;
            myform.quote_shipment_premium.value = res;
			//alert(res);
    //  }
}


function generate_premium()
{
	//alert("generate_premium()");
	
			myform = document.quote_customer;
			//Az Global Uplift is set at backend for all direct (non agent) customer
			var global_uplift = myform.global_uplift.value;
            //Az Goods rate
            if (myform.transports_id.value >0)
            {
                  rate = myform.goods_id.value.split('_')[1];
            }

			//Az IPT Tax from Database set with each location in Location table
            var tax1 = 0;
            tax1 = myform.quote_shipment_from_id.value.split('_')[1];
            //alert('taxFrom: ' + tax1);
            if (!tax1)
            {  tax1 = 0; }
            
			var tax2 = 0;
            tax2 = myform.quote_shipment_to_id.value.split('_')[1];
            //alert('taxTo: ' + tax2);
            if (!tax2)
            { tax2 = 0; }
            // ipt taxes are set only for uk, if both are set then calculate ipt due
			//tax1 = FROM UK
            var tax = 0;
            if (tax1 > 0 && tax2 >0)
            { 
			   tax = tax1;
            }
//alert("tax:"+ tax);			
            // now get the rates for goods / locations
            var rate_from = 0;
            var goods_id = myform.goods_id.value.split('_')[0];
            var location_id = myform.quote_shipment_from_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
            //alert("key:" + key);
            rate_from = find_rate(key);
            if (!rate_from)
            {
                  rate_from = 0;
	        }

            var rate_to = 0;
            location_id = myform.quote_shipment_to_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
			//alert('key string: '+key)
            rate_to = find_rate(key);
            if (!rate_to)
            {
                  rate_to = 0;
            }

			//Az FIND MAX BASE RATE AMONG TWO LOCATIONS
            var rate_shipment = 0;
            rate_shipment = (rate_from*1 > rate_to*1)? rate_from : rate_to;

			
			rate_shipment = parseFloat(rate_shipment) + parseFloat(rate);
//alert("rate_shipment:" + rate_shipment);			
			//return false;
            var v = myform.quote_shipment_value.value;
            // rate of goods %
            var res = v*rate/100;

            // rate of shipment
            var res_shipment = v*rate_shipment/100;
            //alert("res " + res);
            //res += res_shipment;
			res = res_shipment;
			var basic_res = res;
//alert("basis_res:" + basic_res);			
			//Az 1. Minimum premium is £15. if less then that then make it $15
			if (res<15){
				res	= 15;
				basic_res = res;
			}
			//Az HERE WRITE BASIC PREMIUM TO SHARE
			myform.quote_shipment_premium_basic.value = res;
			//Az 2. Apply Global Uplift for Direct Customer (non agent)
			var gu;
//alert("global_uplift:" + global_uplift);			
			if(global_uplift > 0){
				gu = (basic_res * global_uplift)/100;
				//alert(basic_res);
				reswith_gu = res + gu;	
			}else{reswith_gu = res;}
						
			reswith_af = reswith_gu;
//alert("reswith_af:" + reswith_af);			

			var ipt = 0;
            myform.quote_shipment_ipt.value = 0;
			//
			//alert("Step 7: Insurance TAX (%): " + tax)
			if (tax>0)
            {
				  ipt = (basic_res*tax / 100);
                  myform.quote_shipment_ipt.value = ipt;
            	  reswith_af = reswith_af + ipt;
				  myform.quote_shipment_countrytax.value = ipt; 
            }
//alert("ipt:" + ipt);	
			inc_ipt = basic_res + ipt;
//alert("inc_ipt:" + inc_ipt);

			inc_ipt_plus_uplift = inc_ipt + gu;
//alert("inc_ipt_plus_uplift:" + inc_ipt_plus_uplift);

			total_plus_admin = inc_ipt_plus_uplift + 10;
//alert("total_plus_admin:" + total_plus_admin);

			//Az 3. ADD £10 Admin Fees into Final Premium
			reswith_af = reswith_gu + 10;
//alert("reswith_af:" + reswith_af);

			// PSJ
			reswith_af = total_plus_admin;

            if (!reswith_af>0)
            {
                 myform.quote_shipment_premium_view.value = "";
                 //myform.quote_shipment_premium_orig.value = res = "";
                 myform.quote_shipment_premium.value = res = "";
                 myform.quote_shipment_ipt.value = "";
				 myform.quote_shipment_countrytax.value = ""; 
                 return false;
            }
			myform.quote_shipment_ipt.value = tax;
			//alert(ipt_5);
			//
			//reswith_ipt = reswith_af + ((reswith_af*5)/100);
			//
			//reswith_ipt = Math.round(reswith_ipt*100)/100; //rounding up
            //myform.quote_shipment_premium_view.value = res;
            //myform.quote_shipment_premium_orig.value = res_orig;
            //myform.quote_shipment_premium.value = res;

			var total = reswith_af; 

//alert("total:" + total);			
			myform.quote_shipment_premium_view.value = total.toFixed(2);
			myform.quote_shipment_premium.value = total.toFixed(2);
			//alert(reswith_ipt);
			//alert("Step 7: Final Premium: GBP" + total);
}


function generate_premium_agent()
{
            var myform = document.quote_customer;
			var agent_uplift = myform.agent_uplift.value;
			var agent_own_uplift = Number(myform.agent_own_uplift.value);
            // else use the goods rate
            if (myform.transports_id.value >0)
            {
                  //rate = 0.3;
                  rate = myform.goods_id.value.split('_')[1];
                  //alert(rate);
            }
            var tax1 = 0;
            tax1 = myform.quote_shipment_from_id.value.split('_')[1];
            //alert(tax1);
            if (!tax1)
            {
                 tax1 = 0;
            }
            var tax2 = 0;
            tax2 = myform.quote_shipment_to_id.value.split('_')[1];
            //alert(tax1);
            if (!tax2)
            {
                 tax2 = 0;
            }
            // ipt taxes are set only for uk, if both are set then calculate ipt due
            var tax = 0;
            if (tax1 > 0 && tax2 >0)
            {
                 tax = tax1;
            }
			//alert("Approved General Merchandise (%):" + tax)
            // now get the rates for goods / locations
            var rate_from = 0;
            var goods_id = myform.goods_id.value.split('_')[0];
            var location_id = myform.quote_shipment_from_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
            //alert("key:" + key);
            rate_from = find_rate(key);
            if (!rate_from)
            {
                  rate_from = 0;
            }
            var rate_to = 0;
            location_id = myform.quote_shipment_to_id.value.split('_')[0];
            var key = goods_id + "_" + location_id;
            rate_to = find_rate(key);
            //alert(rate_to);
            if (!rate_to)
            {
                  rate_to = 0;
            }
            var rate_shipment = 0;
            rate_shipment = (rate_from*1 > rate_to*1)? rate_from : rate_to;
			//alert("Goods Rate: " + rate_shipment);
			rate_shipment = parseFloat(rate_shipment) + parseFloat(rate);
            //alert("rs " + rate_shipment);
            //return false;
            var v = myform.quote_shipment_value.value;
            // rate of goods %
            var res = v*rate/100;
            //alert("rate " + rate);
            //alert("res " + res);
            // rate of shipment
            var res_shipment = v*rate_shipment/100;
            //alert("res " + res);
            //res += res_shipment;
			res = res_shipment;
			var res_basic = res;
            //if (res<25)
            //{
			//	  res=25;
            //}
            //alert("res " + res);
            // ipt
            //numberField = ipt; // Field where the number appears
            //rlength = 2; // The number of decimal places to round to
            //newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
            //ipt = newnumber;
            //myform.quote_shipment_ipt.value = ipt;         
            //format to 2 dec places
            //res_orig = res + ipt;
            // premium
            //var numberField = res_orig; // Field where the number appears
            //var rlength = 2; // The number of decimal places to round to
            //var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
	        //res_orig = newnumber;
	        //res = format(res);
	        // add on ipt
            //res = (res+ipt);       
            // premium
            //var numberField = res; // Field where the number appears
            //var rlength = 2; // The number of decimal places to round to
            //var newnumber = Math.round(numberField*Math.pow(10,rlength))/Math.pow(10,rlength);
            //res = newnumber;
			//alert(res);    
			//Az 1. Minimum premium is £15. if less then that then make it $15
			if (res<15){
				//alert('b4'+res);
				res	= 15;	
				//alert('after'+res);
			}
			//Az HERE WRITE BASIC PREMIUM TO SHARE BETWEEN A&B and P&O
			myform.quote_shipment_premium_basic.value = res;
			//alert(res);
			//Az 2. Apply Agent Uplift (set by Administrator when register Agent)
			if(agent_uplift>0){
				reswith_au = res + ((res_basic*agent_uplift)/100);	
			}else{ reswith_au = res; }
			//alert('with agent uplift: '+reswith_au);
			//Az 3. Agent set UPLIFT for Himself while generating premium
			//reswith_aou = reswith_au + ((reswith_au*agent_own_uplift)/100);	
			reswith_aou = reswith_au + agent_own_uplift;
//alert("agent_own_uplift:" + agent_own_uplift);			
			//alert('with agent OWN uplift: '+reswith_aou);
			//Az 4. ADD £10 Admin Fees into Final Premium
			reswith_af = reswith_aou + 10;
			//alert(reswith_af);
			//Az 5. INSURANCE PREMIUM TAX 5%
			//ipt_5 = (reswith_af*5)/100;
			//ipt_5 = Math.round(ipt_5*100)/100;  //rounding up
			//myform.quote_shipment_ipt.value = ipt_5.toFixed(2);
			var ipt = 0;
            myform.quote_shipment_ipt.value = 0;
//alert("tax:" + tax);			
            if (tax>0)
            {
            // IPT tax %
                  //ipt = (reswith_af*tax / 100);
				  ipt = (res/100) * tax;
                  myform.quote_shipment_ipt.value = ipt;
                  reswith_af = reswith_af + (reswith_af*tax / 100);
				  myform.quote_shipment_countrytax.value = ipt; 
            }
//alert("res:" + res);			
//alert("ipt:" + ipt);	
			inc_ipt = res + ipt;
//alert("inc_ipt:" + inc_ipt);
//alert("agent_uplift:" + agent_uplift);
			val_agent_uplift = (res/100) * agent_uplift;
//alert("val_agent_uplift:" + val_agent_uplift);			

			inc_ipt_plus_agent_uplift = inc_ipt + val_agent_uplift
//alert("inc_ipt_plus_agent_uplift:" + inc_ipt_plus_agent_uplift);

			total_plus_admin = inc_ipt_plus_agent_uplift + 10;
//alert("total_plus_admin:" + total_plus_admin);
			total_plus_admin_plus_own_uplift = total_plus_admin + agent_own_uplift;
//alert("total_plus_admin_plus_own_uplift:" + total_plus_admin_plus_own_uplift);
            if (!res>0)
            {
                 myform.quote_shipment_premium_view.value = "";
                 //myform.quote_shipment_premium_orig.value = res = "";
                 myform.quote_shipment_premium.value = reswith_af = "";
                 myform.quote_shipment_ipt.value = "";
				 myform.quote_shipment_countrytax.value = ""; 
                 return false;
            }
			
			myform.quote_shipment_ipt.value = tax;
			//
			//reswith_ipt = reswith_af + ((reswith_af*5)/100);
			//reswith_ipt = Math.round(reswith_ipt*100)/100;  //rounding up
			//
            //myform.quote_shipment_premium_orig.value = res_orig;
			//var total = reswith_af; 
			var total = total_plus_admin_plus_own_uplift; 
			myform.quote_shipment_premium_view.value = total.toFixed(2);
			myform.quote_shipment_premium.value = total.toFixed(2);
}

function freight_quote(){

		 myform = document.fre_quote;
		 myform.quote_shipment_premium_view.value = '';
		 //
		 myform.anyclaim.value;  //has to be NO
		 myform.criminal.value; // has to be NO
		 myform.desc_goods.value; // It has to be general cargo
		 myform.subcontactors.value;//IF IT IS YES THEN FOLLOWING Qs subcontactors_limit HAS TO BE YES OTHER WISE NO IS ALSO EXCEPTED
		 myform.subcontactors_limit.value; //DEPEDS ON subcontactors VALUE
		 myform.conditionsofcarriage.value; //Contratual Term Or Uaual Condition Of Carriage
		 myform.ukreg.value; //DEPEDS ON subcontactors VALUE
		 //
		 var premium = 0;
		  // now get the rates for vehicle / carriage
            var rate_from = 0;
            var vehicle_id = myform.vehicles.value.split('_')[0];
            var carriage_id = myform.conditionsofcarriage.value.split('_')[0];
            var key = vehicle_id + "_" + carriage_id;
            //alert("key:" + key);
            rate_from = find_rate(key);
            if (!rate_from)
            {
                  rate_from = 0;
	        }
			premium = rate_from;
			//alert("Base rate is: " + rate_from);
		 //
		ipt = premium*5/100;
		 //Az 2. ADD £10 Admin Fees into Final Premium
		 premium = premium + 10;
		 //Az 3. Calculate 5% IT
		 
		 //
		 premium = premium + ipt;
		 //
		 if(myform.anyclaim.value == 'no' && myform.criminal.value =='no' && myform.desc_goods.value == 'general cargo' && myform.ukreg.value =='yes'){
			if(myform.subcontactors.value == 'yes' && myform.subcontactors_limit.value == 'yes'){
				myform.quote_shipment_premium_view.value = premium.toFixed(2);
				myform.quote_shipment_ipt.value = ipt.toFixed(2);
			}else if(myform.subcontactors.value == 'no'){
				myform.quote_shipment_premium_view.value = premium.toFixed(2);
				myform.quote_shipment_ipt.value = ipt.toFixed(2);
			}else{
				myform.eligibility.value = 'no';
				myform.quote_shipment_premium_view.value = premium.toFixed(2);
				myform.quote_shipment_ipt.value = ipt.toFixed(2);
			}	 
		 }else{
			myform.eligibility.value = 'no';
			myform.quote_shipment_premium_view.value = premium.toFixed(2);
			myform.quote_shipment_ipt.value = ipt.toFixed(2);
   		 }
}


