

$(function() {

   $( "#p60_dob_picker, #jsd_picker, #p45_dob_picker, #p45_dl_picker").datepicker({

      showOn: "button",

      buttonImage: "images/calender_icon2.jpg",

      buttonImageOnly: true,

      beforeShow: function(input) {},

      dateFormat: "dd-mm-yy",

      changeYear: true,

      yearRange: 'c-90:c',

      onSelect: function(dateText, inst) {

         var thisId = $(this).attr('id');

         if(thisId=="p60_dob_picker") var fieldStr = "p60_day|p60_month|p60_year";

         if(thisId=="jsd_picker") var fieldStr = "day|month|year";

         if(thisId=="p45_dob_picker") var fieldStr = "p45_day|p45_month|p45_year";

         if(thisId=="p45_dl_picker") var fieldStr = "p45_dlday|p45_dlmonth|p45_dlyear";



         filldates(dateText, fieldStr);

      }

   });



    $( "#psrequired_dt_picker").datepicker({

      showOn: "button",

      buttonImage: "images/calender_icon2.jpg",

      buttonImageOnly: true,

      beforeShow: function(input) {},

      dateFormat: "dd/mm/yy",

      changeYear: true,

      yearRange: '1980:c+2',

      onSelect: function(dateText, inst) {

         var existing_dts = $("#payslip_dates").val();

         if(existing_dts!="") dateText = existing_dts + ", "+dateText;

         $("#payslip_dates").val(dateText);

      }

   });



   //this is to clone already filled home address, send to original fields if requuired fields are empty

   $("#chkbxcopyhmaddress").change(function() {

       if(document.frmorder.elements.chkcopyaddchk.checked) { 

         if($("#address1").val()=="") {$("#address1").focus();return;}
                              
                         
      $("#delivery_droptitle").val($("#droptitle").val()).attr("selected", "selected");

         
		  $("#delivery_fname").val($("#fname").val());

          $("#delivery_lname").val($("#lname").val());

		 $("#delivery_add1").val($("#address1").val());

         $("#delivery_add2").val($("#address2").val());

         if($("#town_city").val()=="") {$("#town_city").focus();return;}

         $("#delivery_city").val($("#town_city").val());

         if($("#postcode").val()=="") {$("#postcode").focus();return;}

         $("#delivery_postcode").val($("#postcode").val());

         if($("#country").val()=="") {$("#country").focus();return;}

         $("#delivery_country").val($("#country").val());

       }  else  {

         $("#delivery_fname,#delivery_lname, #delivery_add1, #delivery_add2, #delivery_city, #delivery_postcode").val("");

         $("#delivery_country").val('United Kingdom');

       }

     }

   );



   //toggling payslip box

   $("#payslipradiobtn").click(function(){$("#divpayslipradio").toggle()});



   //toggling company address field set

   $("#nocompaddress").click(function()   {

      $("#div1").slideUp();

      $("#nocompaddress").val(3);

   });



   //toggling company address field set

   $("#yescompaddress").click(function(){

      $("#div1").slideDown();

      $("#nocompaddress").val(0);

   });



   $(".MultiFile-remove").click(function(){

      var clickobj = $(this);

      var id_params = $(this).attr('id').split('-');

      var file_id = id_params[1];

      var files_type = id_params[0];



      if(files_type=="logo_files") var elemSelector = "#logo-files-available";

      if(files_type=="contact_files") var elemSelector = "#contact-files-available";

      if(files_type=="cf_files") var elemSelector = "#cf-files-available";



      $.get("remove_uploaded_file.php", {file_id: file_id, files_type: files_type},

        function(data) {

          if(data==1)  {

            var current_allowed_files = $(elemSelector).val(); //number

            var current_max_files_class = "max-" + current_allowed_files;

            var new_num_files = parseInt(current_allowed_files)+1;

            if(new_num_files <= 5 )  { //current limit is 5

               $(elemSelector).MultiFile({

                  max: new_num_files

               });

               clickobj.parent().eq(0).remove();

            }

          }

      });

      return false;

   });

});





/* Global php functions and variable related to checkout form only*/



function filldates(dateText, fieldobj_str)   {

   var dmy = dateText.split("-");

   var fieldobj = fieldobj_str.split("|");

   $("#"+fieldobj[0]).val(dmy[0]);

   $("#"+fieldobj[1]).val(dmy[1]);

   $("#"+fieldobj[2]).val(dmy[2]);

}



function hidep60empdiv()  { 

   document.getElementById('divp60emp').style.display = 'none';

}

function showp60empdiv()  { 

   document.getElementById('divp60emp').style.display = '';

}



function hidep60div()  { 



//document.getElementById('divp60').style.visibility = 'hidden';

   document.getElementById('divp60').style.display = 'none';

}

function showp60div()  { 



//document.getElementById('divp60').style.visibility = 'visible';

   document.getElementById('divp60').style.display = '';

}





function showpayslipradiodiv()

{

document.getElementById('divpayslipradio').style.display = '';

}





function hidediv()  { 



   document.getElementById('div1').style.visibility = 'hidden';

   document.getElementById('div1').style.display = 'none';

}

function showdiv()  {

   document.getElementById('div1').style.visibility = 'visible';

}

// This code makes the jump from textbox one to textbox two -->

function checkjobstartday()  {

var letters = document.frmorder.day.value.length +1;

if (letters <= 2)

{

   document.frmorder.day.focus()}

else

{

   document.frmorder.month.focus()}

}

function checkjobstartmonth()  {

var letters2 = document.frmorder.month.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.month.focus()}

else

{

   document.frmorder.year.focus()}

}                   

function checktxtchr1()  {

var letters = document.frmorder.chr1.value.length +1;

if (letters <= 2)

{

   document.frmorder.chr1.focus()}

else

{

   document.frmorder.chr2.focus()}

}

function checktxtchr2()  {

var letters = document.frmorder.chr2.value.length +1;

if (letters <= 6)

{

   document.frmorder.chr2.focus()}

else

{

   document.frmorder.chr3.focus()}

}       

//This code makes the jump from textbox two to text box three -->

function check()  {

var letters = document.frmorder.p45_day.value.length +1;

if (letters <= 2)

{

   document.frmorder.p45_day.focus()}

else

{

   document.frmorder.p45_month.focus()}

}

function check2()  {

var letters2 = document.frmorder.p45_month.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.p45_month.focus()}

else

{

   document.frmorder.p45_year.focus()}

}

function check3()  {

var letters2 = document.frmorder.p45_dlday.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.p45_dlday.focus()}

else

{

   document.frmorder.p45_dlmonth.focus()}

}

function check4()  {

var letters2 = document.frmorder.p45_dlmonth.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.p45_dlmonth.focus()}

else

{

   document.frmorder.p45_dlyear.focus()}

}

function check5()  {

var letters2 = document.frmorder.p60_day.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.p60_day.focus()}

else

{

   document.frmorder.p60_month.focus()}

}

function check6()  {

var letters2 = document.frmorder.p60_month.value.length +1;

if (letters2 <= 2)

{

   document.frmorder.p60_month.focus()}

else

{

   document.frmorder.p60_year.focus()}

}       

function checkp45officeref1()  {

var letters = document.frmorder.p45_officeref1.value.length +1;

if (letters <= 3)

{

   document.frmorder.p45_officeref1.focus()}

else

{

   document.frmorder.p45_officeref2.focus()}

}



function checkp60officeref1()  {

var letters = document.frmorder.p60_officeref1.value.length +1;

if (letters <= 3)

{

   document.frmorder.p60_officeref1.focus()}

else

{

   document.frmorder.p60_officeref2.focus()}

}





//checkout form buttons to submit or alteration (three function, TODO: review later)

function submitfrm(pid) {

   document.getElementById("delete"+pid).value=pid;

   needToConfirm = false;

   document.cart_quantity.submit();

}

function submitfrm2(pid){

   document.getElementById("change"+pid).value=pid;

   needToConfirm = false;

   document.cart_quantity.submit();

}

function submitfrm3(pid){

   document.getElementById("change"+pid).value=pid;

   if(document.getElementById("grp_change_flag"+pid)) {

      document.getElementById("grp_change_flag"+pid).value = pid;

   }

   needToConfirm = false;

   document.cart_quantity.submit();

}



//bookmark script

function bookmarksite(title,url){

if (window.sidebar) // firefox

    window.sidebar.addPanel(title, url, "");

else if(window.opera && window.print){ // opera

    var elem = document.createElement('a');

    elem.setAttribute('href',url);

    elem.setAttribute('title',title);

    elem.setAttribute('rel','sidebar');

    elem.click();

} 

else if(document.all)// ie

    window.external.AddFavorite(url, title);

}


function changeDMethod(obj){

  if(obj.checked == true)

    document.getElementById('delivery_method').options['7'].selected = true;

  else {
     document.getElementById('delivery_method').options['0'].selected = true;
  
  }
}

function selectOneDMethod(){
  var dtype = document.getElementById('delivery_method').value;
  var newDvalue = 0;

  document.getElementById('delivery_officechk').checked = false;
  
  switch(dtype){
    case '0':
      newDvalue = 0;
    break;
    case '1':
      newDvalue = 0;
    break;
    case '2':
      newDvalue = 9;
    break;    
    case '3':
      newDvalue = 14;
    break;    
    case '4':
      newDvalue = 12;
    break;
    case '5':
      newDvalue = 10;
    break;
    case '6':
      newDvalue = 30;
    break;
    case '7':
      document.getElementById('delivery_officechk').checked = true;
      newDvalue = parseFloat(document.getElementById('delivery_officechk_price').value);
    break;
    default:                
      document.getElementById('delivery_method').value = '0';
      newDvalue = 0;
    break;
  }

  var pricelen = document.getElementById("total_price").innerHTML.length;
  var intprice = (document.getElementById("total_price").innerHTML).substring(1,pricelen);
  var dpricelen = document.getElementById("right_delivery").innerHTML.length;
  var dintprice = (document.getElementById("right_delivery").innerHTML).substring(1,dpricelen);
  var righttpricelen = document.getElementById("right_total_price").innerHTML.length;
  var righttintprice = (document.getElementById("right_total_price").innerHTML).substring(1,righttpricelen);
 
  var oldDprice = document.getElementById("oldDprice").value;
  
  if(pricelen > 0){
    productsPrice = parseFloat(intprice);
    oldDprice= parseFloat(oldDprice);
    dintprice=parseFloat(dintprice);
    
    productsPrice = productsPrice - oldDprice;
    dintprice = dintprice - oldDprice;
    righttintprice = righttintprice - oldDprice;
    productsPrice = productsPrice + newDvalue;
    //alert(intprice+' : '+productsPrice+ ' -> '+newDvalue);
    dintprice = dintprice + newDvalue;
    righttintprice = righttintprice + newDvalue;
    document.getElementById("oldDprice").value = newDvalue;
    document.getElementById("total_price").innerHTML ='&pound;'+ productsPrice.toFixed(2);
    document.getElementById("right_delivery").innerHTML ='&pound;'+ dintprice.toFixed(2);
    document.getElementById("right_total_price").innerHTML ='&pound;'+ righttintprice.toFixed(2);
  }
}


function SetCheckboxForAddiotional(obj , str,price)
{
   var checkbox = document.getElementById(str);
  
   if(obj.value!="")
   {
	   checkbox.checked = true;
	  
	  }else{
	   checkbox.checked = false;
	  }
	  
	 totalCounter(price, checkbox, 'right_others');
  
}

function totalCounter(price, obj, rightid){

  var objeid=obj.name;
  var dedgtype = document.getElementById("deduction_global_type").value;
  var dedgvalue = document.getElementById("deduction_global_value").value;
 
 
  var dedgar=new Array('pension_deductionchk','medical_insurancechk', 'student_loanchk', 'courtorderchk', 'child_supportchk', 'subscriptionchk','other_deductionchk','bonuschk', 'commissionchk', 'overtimechk', 'additional_hourschk', 'travel_allowancechk','car_allowancechk','london_allowancechk','expense_allowancechk','holiday_paychk','advance_paychk','statutory_paychk','other_incomechk','dept_name_price', 'cost_code_price', 'holiday_allowance_price');
  
 
 
 
  if (dedgtype==1 && (objeid=='pension_deductionchk' || objeid=='medical_insurancechk' || objeid=='student_loanchk' || objeid=='courtorderchk' || objeid=='child_supportchk' || objeid=='subscriptionchk' || objeid=='other_deductionchk' || objeid=='bonuschk' || objeid=='commissionchk' || objeid=='overtimechk' || objeid=='additional_hourschk' || objeid=='travel_allowancechk' || objeid=='car_allowancechk' || objeid=='london_allowancechk' || objeid=='expense_allowancechk' || objeid=='holiday_paychk' || objeid=='advance_paychk' || objeid=='statutory_paychk' || objeid=='other_incomechk' || objeid=='dept_name_price' || objeid=='cost_code_price' || objeid=='holiday_allowance_price'))
  {
    price=0;
    di=0;
    for (var i=0; i <= dedgar.length-1; i++)
    { 
      if (document.getElementById(dedgar[i])!=null && document.getElementById(dedgar[i]).checked==true)
        di=di+1;
    }
    if ((di<=1 && obj.checked == true) || di==0)
      price=parseFloat(dedgvalue);
    
  }
  

  var pricelen = document.getElementById("total_price").innerHTML.length;

  var intprice = (document.getElementById("total_price").innerHTML).substring(1,pricelen);

  if(rightid != ''){

    var rpricelen = document.getElementById(rightid).innerHTML.length;

    var rintprice = (document.getElementById(rightid).innerHTML).substring(1,rpricelen);    

    var righttpricelen = document.getElementById("right_total_price").innerHTML.length;

    var righttintprice = (document.getElementById("right_total_price").innerHTML).substring(1,righttpricelen);

  }

  if(pricelen > 0){

    productsPrice = parseFloat(intprice);
    rintprice = parseFloat(rintprice);
    righttintprice = parseFloat(righttintprice);



    if(obj.checked == true){

      productsPrice = productsPrice + price;

      if(rightid != ''){

        rintprice = rintprice + price;

        righttintprice = righttintprice + price;

      }

    }else{

      productsPrice = productsPrice - price;    

      if(rightid != ''){

        rintprice = rintprice - price;

        righttintprice = righttintprice - price;

      }

    }

    document.getElementById("total_price").innerHTML ='&pound;'+ productsPrice.toFixed(2);

    if(rightid != ''){

      document.getElementById(rightid).innerHTML ='&pound;'+ rintprice.toFixed(2);

      document.getElementById("right_total_price").innerHTML ='&pound;'+ righttintprice.toFixed(2);

    }

   }    

}


function popupRefer()
{
  var ddd=document.getElementById('chkreferfrndchk').checked;

  if (ddd)
    window.open('referafriend.php','welcome','width=600,height=500,scrollbars=yes');
}
