// JScript File

// Store all Make Value In Database

function StoreInDatabase(Make)
{
    var flag = ShowParts.StoreDataInDatabase(Make);
    //alert(flag.value);
    if(flag.value == "false")
    {
        alert('You have already added this Part to the Shopping Cart! '); 
        document.getElementById('panel2').style.display = 'none';
        document.getElementById('panel1').style.display = 'block';     
    }
    else
    {
	    document.getElementById('tblcookies1').style.display = 'none';    
	    document.getElementById('tdsearch').style.display = 'none';    
        document.getElementById('panel2').style.display = 'block';
        document.getElementById('panel1').style.display = 'none';
        location.href = "ShowCart.aspx";
    }
}

function cleartext()
{
    var zipcode=document.getElementById('txtZipCode').value;
    if (zipcode == "ENTER ZIP CODE")
    {
        document.getElementById('txtZipCode').value='';
	  document.getElementById('txtZipCode').style.color = 'black';
    }
}


function ShowItems()
{
      var cartValue = ShowCart.DisplayShopCart();
      //alert(cartValue.value);
      var CountItems = ShowCart.TotalItems();
      var temp = "Shopping Cart:";
      
      if((cartValue.value != null) && (CountItems.value > 0))
      { 
          document.getElementById('divCart').innerHTML=cartValue.value;
          document.getElementById('divCart').style.display='block';
          if(document.getElementById('hidZipCode').value != "0")
          { 
                document.getElementById('txtZipCode').value = document.getElementById('hidZipCode').value;
                CalculateShipCharg();
                document.getElementById('drpShipment').selectedIndex = document.getElementById('hidPrevious').value; 
                shipment();
           }
          document.getElementById('hrShopcart').innerHTML = "<b>" + temp.fontcolor("#FFFFFF") + "</b><br><a href='#' onclick='VisibleCart()'>" + CountItems.value + " items</a>";
      }
      else
      {
        document.getElementById('hrShopcart').innerHTML = "<b>" + temp.fontcolor("#FFFFFF") + "</b><br><a href='#' onclick='VisibleCart()'>0 items</a>";
        document.getElementById('divCart').innerHTML=cartValue.value;
        document.getElementById('divCart').style.display='block';
      }
}

function VisibleCart()
{
    	document.getElementById('tblcookies1').style.display = 'none';    
	    document.getElementById('tdsearch').style.display = 'none';    
        document.getElementById('panel2').style.display = 'block';
        document.getElementById('panel1').style.display = 'none';
        ShowItems();
}


// Calculate Shipping Charges

function  CalculateShipCharg()
{
    var Zipcode;
    document.getElementById('hidZipCode').value = "0";
    ZipCode=document.getElementById("txtZipCode").value;
   
    if(document.getElementById("tdSubTotal").innerHTML == "$0.00" || document.getElementById("tdSubTotal").innerHTML == "$0")
    {
       alert('Please select item')
       document.getElementById('tdState').style.display = 'none'; 
       document.getElementById('tdCity').style.display= 'none';
       document.getElementById("txtZipCode").value  = "";
       var ddl = document.getElementById('drpShipment');
       ddl.options[0] = null;
       ddl.options[1] = null;
       ddl.style.display = "none";
       document.getElementById('tdFinalTotal').innerHTML = "";
       document.getElementById('tdShipmentCharg').innerHTML = "";
       document.getElementById('btnCheckOut').style.display = "none";
    }
    else
    {
            var ary = new Array(null);
            var i=0;
            
            var weight = ShowCart.CalculateShipmentCharge(ZipCode);
            var ddl = document.getElementById('drpShipment');
             
            var theOption = new Option;
            var x;
            var i;
            
               if(weight.value == null )
               {
                   
                    ddl.options[0] = null;
                    ddl.options[1] = null;
                    ddl.style.display = "none";
                    var lblShipmentCharge = document.getElementById("tdShipmentCharg").innerHTML
                    var ShipmentCharge = lblShipmentCharge.substring(1,lblShipmentCharge.length);
                    var lblFinal = document.getElementById("tdFinalTotal").innerHTML
                    var FinalPrice = lblFinal.substring(1,lblFinal.length);
                    document.getElementById("tdFinalTotal").innerHTML = "$" + round_decimals((FinalPrice-ShipmentCharge),2);
                    document.getElementById("tdShipmentCharg").innerHTML = "";
                    
                    if(document.getElementById("txtZipCode").value == "")
                    {
                        document.getElementById('tdState').style.display = 'none'; 
                        document.getElementById('tdCity').style.display= 'none'
                        document.getElementById('btnCheckOut').style.display = 'none';
                        alert('Please Enter ZipCode');
                    }
                    else
                    {
                        document.getElementById('tdState').style.display = 'none'; 
                        document.getElementById('tdCity').style.display= 'none'
                        document.getElementById('btnCheckOut').style.display = 'none';
                        var temp=document.getElementById('txtZipCode').value;
			if (temp!='ENTER ZIP CODE'&& temp!='ENTER')
			{
				alert('ZipCode Not Found.');
			 }
		   }
                }
               else
               {
               
                      if(weight.value.length == 1)
                      {   
                            ddl.options[0] = null;
                            ddl.options[1] = null;
                            
                            var theOption = new Option;
                           
                            theOption.text = "Expedited ($" + round_decimals(weight.value,2) + ")";
                            theOption.value = round_decimals(weight.value,2);
                            ddl.options[0] = theOption;
                            ddl.style.display = "block";
                            document.getElementById('btnCheckOut').style.display = 'block';
                            shipment();
                
                     }
                    else if(weight.value.length == 2)
                    {
                      
                        ddl.options[0] = null;
                        ddl.options[1] = null;
                        var ArrayWeight = weight.value.toString().split(",");    
                        
                        var theOption = new Option;
                        theOption.text = "Standard ($" + round_decimals(ArrayWeight[0],2) + ")";
                        theOption.value = round_decimals(ArrayWeight[0],2);
                        ddl.options[0] = theOption;
                       
                        var theOption = new Option;
                        theOption.text = "Expedited ($" + round_decimals(ArrayWeight[1],2) + ")";
                        theOption.value = round_decimals(ArrayWeight[1],2);
                        ddl.options[1] = theOption;
                        ddl.style.display = "block";
                        document.getElementById('btnCheckOut').style.display = 'block';
                        shipment();
                   }
                   
                     var Zipcodedata = ShowCart.ZipcodeDetails(ZipCode);
                     if(Zipcodedata.value != "")
                     {
                        var details = Zipcodedata.value.toString().split(",");
                        document.getElementById('txtCity').value = details[0]; 
                        document.getElementById('txtState').value = details[1]; 
                        document.getElementById('tdState').style.display = 'block'; 
                        document.getElementById('tdCity').style.display= 'block';
				//document.getElementById('tdMsgZip').style.display= 'none';
                     }
                     else
                     {
                      document.getElementById('tdState').style.display = 'none'; 
                       document.getElementById('tdCity').style.display= 'none';
				//document.getElementById('tdMsgZip').style.display= 'block';
                       }
                   
               }
               
   
 }
} 

function shipment()
{
    var lblShipmentCharge = document.getElementById('drpShipment').value;
    document.getElementById("tdShipmentCharg").innerHTML = "$" + round_decimals(lblShipmentCharge,2);
    var lblSubTotal =  document.getElementById("tdSubTotal").innerHTML;
    var SubTotal = lblSubTotal.substring(1,lblSubTotal.length);
    var Total  = ((SubTotal*1) + (lblShipmentCharge *1) ) 
    document.getElementById("tdFinalTotal").innerHTML = "$" + round_decimals(Total,2);
}


// Ronded value with specified decimal Point

function round_decimals(original_number, decimals)
 {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function RedictPage()
{
    document.getElementById('hidZipCode').value = document.getElementById('txtZipCode').value;
    var lblShip =  document.getElementById('tdShipmentCharg').innerHTML; 
    var ship = lblShip.substring(1,lblShip.length);
    var OrderID = ShowCart.SetOrderID(document.getElementById('hidZipCode').value,ship)
    
    var w = document.Form1.drpShipment.selectedIndex;
    var selected_text = document.Form1.drpShipment.options[w].text;
    var where_is_r=selected_text.indexOf('(')
    document.getElementById("hidShipmentMode").value  = selected_text.substring(0,where_is_r); 
    //alert(document.getElementById("hidShipmentMode").value)
    
    window.location = "frmCheckOut.aspx?OrderID=" + OrderID.value + "&Smode=" + document.getElementById("hidShipmentMode").value;
}

// Remove selected Part 

function RemovePart(ShipmentID)
{
        if(document.getElementById('txtZipCode').value != "") 
        { 
             document.getElementById('hidZipCode').value = document.getElementById('txtZipCode').value;
             document.getElementById('hidPrevious').value = document.getElementById('drpShipment').selectedIndex;
        }
    ShowCart.DeletePart(ShipmentID);
    ShowItems();
}

function checkNum(e)
{
if (!e) var e = window.event;
 if (e.keyCode) code = e.keyCode; 
else if (e.which) code = e.which; 
var carCode = code;
//var carCode = event.keyCode;

if (((carCode < 48) || (carCode > 57)) && (carCode != 13 && carCode != 46 && carCode != 8 && carCode != 37 && carCode != 39))
{
   alert('Please enter only numbers.');	
e.preventDefault? e.preventDefault() : e.returnValue = false; 
//    e.cancelBubble = true;	
//    e.returnValue = false;	
}
if (carCode ==13)
{
	document.getElementById('txtZipCode').focus();
}

}


// Calculate Total Price for Perticular Part

function CalculatePRate(ShipmentID)
{
           
                    var Quantity = document.getElementById("txt_" + ShipmentID).value
                    if(Quantity == "")
                    {
                        Quantity = 0;
                        document.getElementById("txt_" + ShipmentID).value = 0;
                        
                    }
                        
                        lblPreviousPrice = document.getElementById("tdTotalPrice_" + ShipmentID).innerHTML;
                        lblPreviousPrice1 = lblPreviousPrice.substring(1,lblPreviousPrice.length);

                                                                        
                        var lblPrice = document.getElementById("tdPrice_" + ShipmentID).innerHTML
                        var lblCorePrice = document.getElementById("tdCorePrice_" + ShipmentID).innerHTML
                        ShowCart.UpdateQuantity(ShipmentID,Quantity);
                        var Price = lblPrice.substring(1,lblPrice.length);
                        var CorePrice = lblCorePrice.substring(1,lblCorePrice.length);
                        
				var CalculatePrice = Quantity*((Price*1)+(CorePrice*1)) ;
                        
                        var lblFinal = document.getElementById("tdTotalPrice_" + ShipmentID).innerHTML = "$" + round_decimals(CalculatePrice,2);
                        
                        var lblSubTotal =  document.getElementById("tdSubTotal").innerHTML;
                    
                        var SubTotal = lblSubTotal.substring(1,lblSubTotal.length);
                       
                        SubTotalPrice = ((CalculatePrice*1)+(SubTotal*1) - lblPreviousPrice1);
                        document.getElementById("tdSubTotal").innerHTML = "$"  + round_decimals(SubTotalPrice,2);
                        document.getElementById("tdFinalTotal").innerHTML = "$"  + round_decimals(SubTotalPrice,2);
                        
                         if(document.getElementById("tdShipmentCharg").innerHTML != "") 
                          {
                             document.getElementById('hidPrevious').value = document.getElementById('drpShipment').selectedIndex;
                             CalculateShipCharg();
                             
                             document.getElementById('drpShipment').selectedIndex = document.getElementById('hidPrevious').value; 
                             shipment();
                         }
       
           
}

function Continue()
{
        //alert('Niral');
        var CountItems = ShowCart.TotalItems();
        if(Number(CountItems.value) >= 1)
        {
            if(document.getElementById('txtZipCode').value != "") 
             { 
             document.getElementById('hidZipCode').value = document.getElementById('txtZipCode').value;
              document.getElementById('hidPrevious').value = document.getElementById('drpShipment').selectedIndex;
            }
    }
     document.getElementById('panel2').style.display = 'none';
     document.getElementById('panel1').style.display = 'block';     
     document.getElementById('tblcookies1').style.display = 'block';    
     document.getElementById('tdsearch').style.display = 'block';    
}

function Check()
{
    var temp = "Shopping Cart:";
    var CountItems = ShowCart.TotalItems();
    alert(CountItems);
    if(CountItems.value > 0)
    {
        document.getElementById('hrShopcart').innerHTML = "<b>" + temp.fontcolor("#FFFFFF") + "</b><br><a href='#' onclick='VisibleCart()'>" + CountItems.value + " items</a>";
    }
    else
    {
        document.getElementById('hrShopcart').innerHTML = "<b>" + temp.fontcolor("#FFFFFF") + "</b><br><a href='#' onclick='VisibleCart()'>0 items</a>";
    }
}

function MakeNext(res)
{
    location.href = "ShowYear.aspx?Make=" + res;
}

function MakeYear(res1)
{
    var res = res1.toString().split("@");
    location.href = "ShowModel.aspx?Make=" + res[0] + "&YearID=" + res[1];
}

function YearNext(res1)
{
    var res = res1.toString().split("@");
    location.href = "ShowProductGroup.aspx?Make=" + res[0] + "&YearID=" + res[1] + "&Model=" + res[2];
}

function GroupNext(res1)
{
    var res = res1.toString().split("@");
    location.href = "ShowProductName.aspx?Make=" + res[0] + "&YearID=" + res[1] + "&Model=" + res[2]+ "&categoryname="+ res[3]+ ","+res[4];
}

function ProductNameNext(res1)
{
    var res = res1.toString().split("@");
    location.href = "ShowParts.aspx?Make=" + res[0] + "&YearID=" + res[1] + "&Model=" + res[2]+ "&categoryname="+ res[3]+ ","+res[4]+ "&Partterminologyname="+ res[5]+ ","+res[6];    
}
function ProductNameNextm(res1)
{
    var res = res1.toString().split("@");
    location.href = "ShowParts.aspx?Make=" + res[0] + "&YearID=" + res[1] + "&Model=" + res[2]+ "&categoryname="+ res[3]+ ","+res[4]+ "&Partterminologyname="+ res[5]+ ","+res[6];    
}

function findbyZipCode(name,value)
{
    if(value.length == 5)
    {
        //alert(document.getElementById('txtZipCode').value);
        CalculateShipCharg();
    }
}

function clearserach()
{
    var searchval=document.getElementById('txtsearch').value;
    if ((searchval == "Enter Part#") || (searchval == "Part Not Found"))
    {
        document.getElementById('txtsearch').value='';
    }
}

