/*
 ** File name: ctcmodule.js
 ** Creation Date: 01/28/2008
 ** Description: Master Javascript file that uses namespace
 ** Revision	: $Revision: 1.1 $
 ** Modified by	: $Author: app1ssh $ $Date:
 ** Checkout Tag	: $Name: V01_00_07 $
 **
 ** The use, disclosure, reproduction, modification, transfer, or transmittal
 ** of this work for any purpose in any form or by any means without the
 ** written permission of United Parcel Service is strictly prohibited.
 **
 ** Confidential, Unpublished Property of United Parcel Service.
 ** Use and Distribution Limited Solely to Authorized Personnel.
 **
 ** Copyright 2009 United Parcel Service of America, Inc. All Rights Reserved.
 */
///////////////////////////////////////////////////////////////////////////////////

(function(){


function shipFromCountryChange()
{
       var originCountry = null;
       var destCountry = null;
	   var imsFromKey=null;
	   var imsToKey = null;
	   var weightUnit= null;
	   var isMetricFlag = false;
	   
       var selectedType=document.inputForm.ctcShipmentType.selectedIndex;
       var type=document.inputForm.ctcShipmentType.options[selectedType].value;

		//user state 3 and 4
	   if (document.inputForm.loginStatus.value == "03" || document.inputForm.loginStatus.value == "04")

	   {
			var selectedFromKey=document.inputForm.fromAddressBook.selectedIndex;
            imsFromKey=document.inputForm.fromAddressBook.options[selectedFromKey].value; 
       		var selectedToKey=document.inputForm.toAddressBook.selectedIndex;
       		imsToKey=document.inputForm.toAddressBook.options[selectedToKey].value;
       		//imsToKey.replace(/^\s+|\s+$/g, '');
       		 
			
			if (imsFromKey != null && imsFromKey != "00")
			{
				//add "a" so if key is numeric only, we still can get country. TT 29870
				imsFromKey=imsFromKey+"a";
				originCountry = document.inputForm.elements[imsFromKey].value; 
				

			}
			
			else if (imsFromKey== "00") 
			// when user select profile address, since we do not have key as hidden, have to call ims to get profile country
			{
				// profile address
	    		originCountry = document.inputForm.profileCountry.value;
			
			}
			
			if (imsToKey != null && imsToKey != "00")
			{
				imsToKey=imsToKey+"a";
				destCountry= document.inputForm.elements[imsToKey].value;
			}
			
			else if (imsToKey== "00") //shipTO does not have profile address as default. default to select one.
			{
				destCountry =  document.inputForm.loc.value.substring(3, 5);
			}

	   }
	   else	if (document.inputForm.loginStatus.value == "01" ) //nonlogin

	  {
			originCountry = document.inputForm.origCountry.value;
			destCountry = document.inputForm.destCountry.value;
	  }

	 else if (document.inputForm.loginStatus.value == "NA" ) //login but IMS exception
	 {
			originCountry = document.inputForm.loc.value.substring(3, 5);
			destCountry = document.inputForm.loc.value.substring(3, 5);	 
	 }
	 
	 
	 //when letter is selected, ship from country change, weight show or hide logic
	 if(type=="letter")
	 {
		  if(originCountry == "US" && destCountry == "US")
          {
          	document.getElementById("ctc.weight_field").style.display = 'none';
        	
          }
          else
          {

		  	document.getElementById("ctc.weight_field").style.display = '';

		  }
	 }
	 //end letter
     if(originCountry=="PL" && destCountry=="PL"){
     var optn = document.createElement("OPTION");
      optn.text = document.inputForm.pallettext.value;
	  optn.value = "pallet";//packageListArray[i];
	  document.inputForm.ctcShipmentType.options.add(optn);
	 }



	 if(originCountry !="PL" || destCountry !="PL"){
	 var t= document.inputForm.ctcShipmentType.options.length-1;
	 var checkValue=document.inputForm.ctcShipmentType.options[t].value;
	 
      if(checkValue=="pallet"){
		 document.inputForm.ctcShipmentType.remove(t);
		 document.inputForm.ctcShipmentType.selected = document.inputForm.ctcShipmentType.options[0].value;
	  }
	 
	 }

	isMetricFlag = window.com_ups_rif.isMetric(originCountry);
	if (isMetricFlag== true) 
	{
		weightUnit = "kg";

	}
	
	else
	{
		weightUnit="lbs";
	} 

	checkWeight(weightUnit, originCountry);

}


function shipToCountryChange()
{

       var originCountry = null;
       var destCountry = null;
       var addrDetail = "";
	   var addrArray = null;
       var selectedType=document.inputForm.ctcShipmentType.selectedIndex;
       var type=document.inputForm.ctcShipmentType.options[selectedType].value;
		//user state 3 and 4
	   if (document.inputForm.loginStatus.value == "03" || document.inputForm.loginStatus.value == "04")

	   {
			var selectedFromKey=document.inputForm.fromAddressBook.selectedIndex;
            imsFromKey=document.inputForm.fromAddressBook.options[selectedFromKey].value; 
       		var selectedToKey=document.inputForm.toAddressBook.selectedIndex;
       		imsToKey=document.inputForm.toAddressBook.options[selectedToKey].value; 
			
			if (imsFromKey != null && imsFromKey != "00")
			{
				imsFromKey=imsFromKey+"a";
				originCountry = document.inputForm.elements[imsFromKey].value;
			}
			
			else if (imsFromKey== "00") 
			// when user select profile address, since we do not have key as hidden, have to call ims to get profile country
			{
				// profile address get from hidden
	    		originCountry = document.inputForm.profileCountry.value;
	    			
			}
			
			if (imsToKey != null && imsToKey != "00")
			
			{	
			    imsToKey=imsToKey+"a";	
				destCountry= document.inputForm.elements[imsToKey].value;

			}
			
			else if (imsToKey== "00") //shipTO does not have profile address as default. default to select one.
			{
				destCountry =  document.inputForm.loc.value.substring(3, 5);
			}

	   }
	   
	   else	if (document.inputForm.loginStatus.value == "01" ) //nonlogin

	  {
			originCountry = document.inputForm.origCountry.value;
			destCountry = document.inputForm.destCountry.value;		  
		  
	  }
	  
	 else if (document.inputForm.loginStatus.value == "NA" ) //login but IMS exception
	 {
			originCountry = document.inputForm.loc.value.substring(3, 5);
			destCountry = document.inputForm.loc.value.substring(3, 5);	 
	 }	  

	 //when letter is selected, ship from country change, weight show or hide logic
	 if(type=="letter")
	 {
		  if(originCountry == "US" && destCountry == "US")
          {
          	document.getElementById("ctc.weight_field").style.display = 'none';

        	
          }
          else
          {

		  	document.getElementById("ctc.weight_field").style.display = '';

		  }
	 }
	 //end letter
     if(originCountry=="PL" && destCountry=="PL"){
    var optn = document.createElement("OPTION");
      optn.text = document.inputForm.pallettext.value;
	  optn.value = "pallet";//packageListArray[i];
	  document.inputForm.ctcShipmentType.options.add(optn);
	 }

    if(originCountry !="PL" || destCountry !="PL"){
	 var t= document.inputForm.ctcShipmentType.options.length-1;

	  var checkValue=document.inputForm.ctcShipmentType.options[t].value;

      if(checkValue=="pallet"){
		 document.inputForm.ctcShipmentType.remove(t);
		 document.inputForm.ctcShipmentType.selected = document.inputForm.ctcShipmentType.options[0].value;
	  }
	 
	 
	 }
}

			

function checkWeight(weight,originCountry){

var weightUOM=weight;



 	 if(originCountry == "CA")

  	 {

	 	document.getElementById("weightDropDown").style.display = "";
	 	document.getElementById("kgsStatic").style.display = 'none';
	 	document.getElementById("lbsStatic").style.display = 'none';

	 }

	 if(originCountry != "CA")

	 {

	 	if(weightUOM=="kg")

	 	{  
	 	
	 	  //document.inputForm.weightType.value="kgs";
	 	  var selectedWeightKey=document.inputForm.weightDropDown.selectedIndex;
          document.inputForm.weightDropDown.options[selectedWeightKey].value ="kgs";; 

	 	  document.getElementById("lbsStatic").style.display ="none";

		  document.getElementById("weightDropDown").style.display = "none";

	      document.getElementById("kgsStatic").style.display ="";
	      
		

	 	}

	 	else if(weightUOM=="lbs")

	 	{
 		  //document.inputForm.weightType.value="lbs";
 		 var selectedWeightKey=document.inputForm.weightDropDown.selectedIndex;
         document.inputForm.weightDropDown.options[selectedWeightKey].value ="lbs";
         
	 	 document.getElementById("lbsStatic").style.display ="";
		 document.getElementById("kgsStatic").style.display ="none";
         document.getElementById("weightDropDown").style.display = "none";
		

	 	}



     }





}


function clickContinue(){



       var selectedType=document.inputForm.ctcShipmentType.selectedIndex;
       var type=document.inputForm.ctcShipmentType.options[selectedType].value;
       var origCountry = null;
       var destCountry = null;
       var destResident=null;
 
	   var weightType = null;
	   var status =  document.inputForm.loginStatus.value;
	   //source ID for reporting 
		var pPID = null;
		var metaArray = document.getElementsByTagName("meta");
		for(var i=0; metaArray[i]; i++) {
		  if(metaArray[i].getAttribute("name") == "DCSext.pPID") {
		  	  pPID	= metaArray[i].getAttribute("content");
			  break;
		  }
		}
		
		if(pPID != null)
			document.inputForm.WBPM_lid.value = pPID + "_pnl_ctc";	 
	   //end reporting
	   var LID = document.inputForm.WBPM_lid.value;
	  //weightType =document.inputForm.weightType.value;
	  if (status == "03" || status == "04") //login
	  {

			var selectedFromKey=document.inputForm.fromAddressBook.selectedIndex;
            imsFromKey=document.inputForm.fromAddressBook.options[selectedFromKey].value; 
       		var selectedToKey=document.inputForm.toAddressBook.selectedIndex;
       		imsToKey=document.inputForm.toAddressBook.options[selectedToKey].value; 
			
			if (imsFromKey != null && imsFromKey != "00")
			{
				imsFromKey=imsFromKey+"a";
				//we stored countryCode as hidden when we built the nickName list
				origCountry = document.inputForm.elements[imsFromKey].value; 
				
			}
			
			else if (imsFromKey== "00") 
			// when user select profile address, since we do not have key as hidden, have to call ims to get profile country
			{
				// profile address
	    		origCountry = document.inputForm.profileCountry.value;
			
			}
			
			if (imsToKey != null && imsToKey != "00")
			{
			    imsToKey=imsToKey+"a";
				destCountry= document.inputForm.elements[imsToKey].value;
			}
			
			else if (imsToKey== "00") //shipTO does not have profile address as default. default to select one.
			{
				destCountry =  document.inputForm.loc.value.substring(3, 5);
			}

		
	 }

	

	else //non loging 

	{
		origCountry = document.inputForm.origCountry.value;
		destCountry = document.inputForm.destCountry.value;
	}
       //var status =  document.inputForm.loginStatus.value;
       if ( type == "package" || type == "letter" || type == "pallet")
       {
       

		//domestic
       	if(origCountry == "US" && destCountry == "US")

       	{                          
			 if (type == "letter")

			 {
			 	document.inputForm.weight.value= "1"; //TO DO this is only for letter, for pacakge, need to get value

			 }

			 else

			 {

			    document.inputForm.weight.value=document.inputForm.weight.value;

			 }

			 document.inputForm.quickQuoteTypePackageLetter.value=type;
      		 document.inputForm.quoteType.value= "quickTimeCost";

      		 if (status == "01" || status == "NA") //non-login

      		 {

				if(document.inputForm.residentialBox.checked == true){

			 		document.inputForm.destResident.value="01";

             	}

             	else if (document.inputForm.residentialBox.checked == false)

             	{

             		document.inputForm.destResident.value="02";

             	}      		 			              


             }

             

             else if (status == "03" || status == "04")

             {             
   			    document.inputForm.origCountry.value= origCountry; 
	

				document.inputForm.destCountry.value = destCountry;
	
				document.inputForm.destResident.value=destResident; 


             }


              document.inputForm.action="https://wwwapps.ups.com/ctc/request?WBPM_lid="+LID;
             document.inputForm.submit();

	   	}//end if domestic

	   	else //international 

        {
      		 if (status == "01" || status == "NA") //non-login

      		 {

				if(document.inputForm.residentialBox.checked == true){

			 		document.inputForm.destResident.value="01";
          
             	}

             	else if (document.inputForm.residentialBox.checked == false)

             	{

             		document.inputForm.destResident.value="02";

             	} 
               
             }

             

             else if (status== "03" || status== "04")

             {
 

   			    document.inputForm.origCountry.value= origCountry; 
				
				document.inputForm.destCountry.value = destCountry;
				
				document.inputForm.destResident.value=destResident;

             }
             

              document.inputForm.action="https://wwwapps.ups.com/ctc/request?WBPM_lid="+LID;


        }

	   }


}


function clickFreightContinue(){


        var freightOfferings=document.freightForm.freightOfferings.selectedIndex;
        var freightOfferingstype=document.freightForm.freightOfferings.options[freightOfferings].value;
        var loc= document.freightForm.loc.value;
	   //source ID for reporting 
		var pPID = null;
		var metaArray = document.getElementsByTagName("meta");
		for(var i=0; metaArray[i]; i++) {
		  if(metaArray[i].getAttribute("name") == "DCSext.pPID") {
		  	  pPID	= metaArray[i].getAttribute("content");
			  break;
		  }
		}
		
		if(pPID != null)
			document.inputForm.WBPM_lid.value = pPID + "_pnl_ctc";	 
	   //end reporting
	   var LID = document.inputForm.WBPM_lid.value;

	    document.freightForm.action="https://wwwapps.ups.com/fctc/timeandcost?loc="+loc+"&FREIGHT_TYPE="+freightOfferingstype+"&WBPM_lid="+LID;;		
		document.freightForm.submit();

}


function hideShowFreightInit()

{
	      document.getElementById("ctc.default_look").style.display = '';
	      document.getElementById("ctc.freight_look").style.display = 'none';
}


 function hideShowFreight() //when user changes shipment type, show/hide fields accordingly

  {

       var selectedType=document.inputForm.ctcShipmentType.selectedIndex;

       var type=document.inputForm.ctcShipmentType.options[selectedType].value;
       
       var origCountry = null;
       var destCountry = null;
       var status =  document.inputForm.loginStatus.value;
       

	  if (status == "03" || status == "04") //login
	  {

			var selectedFromKey=document.inputForm.fromAddressBook.selectedIndex;
            imsFromKey=document.inputForm.fromAddressBook.options[selectedFromKey].value; 
       		var selectedToKey=document.inputForm.toAddressBook.selectedIndex;
       		imsToKey=document.inputForm.toAddressBook.options[selectedToKey].value; 
			
			if (imsFromKey != null && imsFromKey != "00")
			{
				//we stored countryCode as hidden when we built the nickName list
				imsFromKey=imsFromKey+"a";
				origCountry = document.inputForm.elements[imsFromKey].value; 
				
			}
			
			else if (imsFromKey== "00") 
			// when user select profile address, since we do not have key as hidden, have to call ims to get profile country
			{
				// profile address
	    		origCountry = document.inputForm.profileCountry.value;
			
			}
			
			if (imsToKey != null && imsToKey != "00")
			{
				destCountry= document.inputForm.elements[imsToKey].value;
			}
			
			else if (imsToKey== "00") //shipTO does not have profile address as default. default to select one.
			{
				destCountry =  document.inputForm.loc.value.substring(3, 5);
			}

		
	 }

	else //non loging 

	{
		origCountry = document.inputForm.origCountry.value;
		destCountry = document.inputForm.destCountry.value;
	}       

        if(type == "package") //package selected

        {

		  document.getElementById("ctc.ship_type_field").style.display = '';

		  document.getElementById("ctc.weight_field").style.display = '';



	      if(document.getElementById("ctc.shipfrom_field") != null)

	      {

          	document.getElementById("ctc.shipfrom_field").style.display = '';

          }

          
          if(document.getElementById("ctc.shipto_field") != null)

          {

	      	document.getElementById("ctc.shipto_field").style.display = '';

	      }



	  	  document.getElementById("ctc.button1").style.display = '';

 

          if(document.getElementById("ctc.shipto_login_field") != null)

          {

	  	  	document.getElementById("ctc.shipto_login_field").style.display = '';

	  	  }

	  	  if(document.getElementById("ctc.shipfrom_Login_field") != null)

	  	  {

	  	  	document.getElementById("ctc.shipfrom_Login_field").style.display = '';

	  	  }



	      if(document.getElementById("ctc.freight_look") != null)

	      {



		  	document.getElementById("ctc.freight_look").style.display = 'none';

		  }	 

 

        }

        else if (type == "freight")   //freight selected

        {



		  document.getElementById("ctc.ship_type_field").style.display = '';

		  document.getElementById("ctc.weight_field").style.display = 'none';



	      if(document.getElementById("ctc.shipfrom_field") != null)

	      {

          	document.getElementById("ctc.shipfrom_field").style.display = 'none';

          }

          



          if(document.getElementById("ctc.shipto_field") != null)

          {

	      	document.getElementById("ctc.shipto_field").style.display = 'none';

	      }



	  	  document.getElementById("ctc.button1").style.display = 'none';

 

          if(document.getElementById("ctc.shipto_login_field") != null)

          {

	  	  	document.getElementById("ctc.shipto_login_field").style.display = 'none';

	  	  }

	  	  if(document.getElementById("ctc.shipfrom_Login_field") != null)

	  	  {

	  	  	document.getElementById("ctc.shipfrom_Login_field").style.display = 'none';

	  	  }



	      if(document.getElementById("ctc.freight_look") != null)

	      {

	

		  	document.getElementById("ctc.freight_look").style.display = '';

		  }	  	  

	  	 

        }

		 else if(type=="letter")

		 {

		  document.getElementById("ctc.ship_type_field").style.display = '';
		  
		  if(origCountry == "US" && destCountry == "US")
          {
          	document.getElementById("ctc.weight_field").style.display = 'none';

        	
          }
          else
          {

		  	document.getElementById("ctc.weight_field").style.display = '';

		  }



	      if(document.getElementById("ctc.shipfrom_field") != null)

	      {

          	document.getElementById("ctc.shipfrom_field").style.display = '';

          }

          



          if(document.getElementById("ctc.shipto_field") != null)

          {

	      	document.getElementById("ctc.shipto_field").style.display = '';

	      }



	  	  document.getElementById("ctc.button1").style.display = '';

 

          if(document.getElementById("ctc.shipto_login_field") != null)

          {

	  	  	document.getElementById("ctc.shipto_login_field").style.display = '';

	  	  }

	  	  if(document.getElementById("ctc.shipfrom_Login_field") != null)

	  	  {

	  	  	document.getElementById("ctc.shipfrom_Login_field").style.display = '';

	  	  }



	      if(document.getElementById("ctc.freight_look") != null)

	      {

 	

		  	document.getElementById("ctc.freight_look").style.display = 'none';

		  }	 

 



		 }

		 else if(type=="pallet")

		 {

		  document.getElementById("ctc.ship_type_field").style.display = '';

		  document.getElementById("ctc.weight_field").style.display = '';



	      if(document.getElementById("ctc.shipfrom_field") != null)

	      {

          	document.getElementById("ctc.shipfrom_field").style.display = '';

          }

          



          if(document.getElementById("ctc.shipto_field") != null)

          {

	      	document.getElementById("ctc.shipto_field").style.display = '';

	      }



	  	  document.getElementById("ctc.button1").style.display = '';

 

          if(document.getElementById("ctc.shipto_login_field") != null)

          {

	  	  	document.getElementById("ctc.shipto_login_field").style.display = '';

	  	  }

	  	  if(document.getElementById("ctc.shipfrom_Login_field") != null)

	  	  {

	  	  	document.getElementById("ctc.shipfrom_Login_field").style.display = '';

	  	  }



	      if(document.getElementById("ctc.freight_look") != null)

	      {

	

		  	document.getElementById("ctc.freight_look").style.display = 'none';

		  }	 

 	      

		 }	

  	

  }



var ctcmodule = window.ctcModuleJavaScript = function() {};
ctcmodule.shipFromCountryChange=shipFromCountryChange;
ctcmodule.checkWeight=checkWeight;
ctcmodule.clickContinue=clickContinue;
ctcmodule.clickFreightContinue=clickFreightContinue;
ctcmodule.hideShowFreightInit=hideShowFreightInit;
ctcmodule.hideShowFreight=hideShowFreight;
ctcmodule.shipToCountryChange=shipToCountryChange;


})();
