// JavaScript Document



$(document).ready(function()
{

    
	$('.product_listing .item').hover(
		function () {
			$(this).addClass("hover");
		},
		function () {
			$(this).removeClass("hover");
		}
	);
	
	//$(document).ready(function()
    //{
	//$(".email_link").colorbox({width:"480px", inline:true, href:"#email_layer"});
    //});
	$("#basketpopip").dialog({ title: "Basket message", width: 500, modal: true });
    $(".email_link").click(function(){
        $("#email_layer").dialog({ title: "Tell a friend", width: 500, modal: true });
    });
    $(".login_link").click(function(){
        $("#login_layer").dialog({ title: "Login", width: 450, modal: true });
    });

    $(".tandclink").click(function () {
        $("#termsandcondition").dialog({ title: "Terms And Conditions", width: 500, resizable: false, modal: true });
    });
	
	//$("checkbox").checked(false);
	
	$("#primarynavigation > li").mouseover(function(){$(this).addClass("active")});
	

	$("#primarynavigation > li").mouseout(function(){$(this).removeClass("active")});
    
});

function toInteger(number){ 
  return Math.round(  // round to nearest integer
    Number(number)    // type cast your input
  ); 
};
function swatchproductImage (objid,src)
{       
    if (src !="0")
    {
        $('#'+objid).attr("src",'/Images/Products/Default/Small/' + src)
    }
    else
    {
        $('#'+objid).attr("src",'/SiteImages/productcategory_noimage.jpg')
    }
}

function defaultproductImage(objid,src)
{       
    
      $('#'+objid).attr("src", src)
    
}

function show(obj)
{
	$('#'+obj).removeClass('hide').addClass('show')
	}
function hide(obj)
{
	$('#'+obj).removeClass('show').addClass('hide')
	}
	
function select_tab(selected, count)
{
	for (i=1; i<=count; i++ )
	{
		if (i!=selected)
		{
			$('#tab_header_'+i).removeClass('active')
			$('#tab_content_'+i).removeClass('active')
		}
		else
		{
			$('#tab_header_'+i).addClass('active')
			$('#tab_content_'+i).addClass('active')
			}
	}
}

function checkout()
        {
            if (document.getElementById("terms_conditions").checked == true)
            {
                window.location='/Checkout.aspx?reset=1';          
            }
            else
            {
				document.confirm_form.redirectto.value = '/Checkout.aspx?reset=1';
                $("#confirm_layer").dialog({ title: "You accept our terms and conditions", width: 400, inline: true }); 
                
            }
            
        }
        
        
function usebilling()
{
    document.Address.shipping_title.value=document.Address.billing_title.value;
    document.Address.shipping_firstname.value=document.Address.billing_firstname.value;
    document.Address.shipping_lastname.value=document.Address.billing_lastname.value;
    document.Address.shipping_postcode.value=document.Address.billing_postcode.value;
    document.Address.shipping_address1.value=document.Address.billing_address1.value;
    document.Address.shipping_address2.value=document.Address.billing_address2.value;
    document.Address.shipping_address3.value=document.Address.billing_address3.value;
    //document.Address.shipping_address4.value=document.Address.billing_address4.value;
    document.Address.shipping_address5.value=document.Address.billing_address5.value;
    document.Address.shipping_phone.value=document.Address.billing_phone.value;
    document.Address.shipping_country.value=document.Address.billing_country.value;
    
}

function update_unit(unit)
{
    $('.measurement_unit').html(unit);
	if (unit== 'cm')
	{
		$('.drop_cm').css('display','block');
		$('.drop_in').css('display','none');
	}
	else
	{
		$('.drop_in').css('display','block');
		$('.drop_cm').css('display','none');
	}
    document.getElementById('selectedUnit').value = unit;
}


function validateWindowWidth(ProductID){
    var windowWidth;
    var gather;
    gather = document.getElementById('gather');
    windowWidth = document.getElementById('width_' + ProductID);
    
    if (isNaN(windowWidth.value)) {
        //alert("Please Enter Only Numerics");
		$("#measure_message .addToBasketMessage").html("<p>Please Enter Only Numerics</p>")
		$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
        return false;
    }else{
        document.getElementById('totalwidth_' + ProductID).value = windowWidth.value * gather.value;
		CalculatePrice(ProductID)
    }
    
}

function updateTotalCurtainWidth(){
    var variationCount;
    var ProductID;
    variationCount = document.getElementById('variationCount');
    if (variationCount.value > 0){
        for (i = 1; i <= variationCount.value; i++){
            ProductID = document.getElementById('variation' + i).value;
            if (document.getElementById('width_' + ProductID).value != '' ){
                validateWindowWidth(ProductID);
            }
        }
    }
}

function validateQuantity(value,ProductID){
    if (isNaN(value)) {
        //alert("Please Enter Only Numerics");
		$("#measure_message .addToBasketMessage").html("<p>Please Enter Only Numerics</p>")
		$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
        return false;
	}
	else
	{
	CalculatePrice(ProductID)	
    }
}

function validateShortening(value,ProductID,drop_cm,drop_in){
    if (isNaN(value)) {
        $("#measure_message .addToBasketMessage").html("<p>Please Enter Only Numerics</p>")
		$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
        return false;
	}
	else
	{
		//alert($('#unit_cm_option').is(':checked'))
		//alert($('#unit_inches_option').is(':checked'))
		//alert(value)
		//alert(drop_cm)
		//alert(drop_in)
		if (($('#unit_inches_option').is(':checked') == true) && (value> drop_in))
		{
			//alert("The maximum 'Shortening to' value for this drop is " + drop_in +"inch");
			$("#measure_message .addToBasketMessage").html("<p>The maximum 'Shortening to' value for this drop is " + drop_in +"inch</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
			return false;
		}
		if (($('#unit_cm_option').is(':checked')== true) && (value> drop_cm) )
		{
			//alert("The maximum 'Shortening to' value for this drop is " + drop_cm + "cm");
			$("#measure_message .addToBasketMessage").html("<p>The maximum 'Shortening to' value for this drop is " + drop_cm + "cm</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
			return false;
		}
		CalculatePrice(ProductID)	
    }
}

function showhideShortening(ProductID){
    var shorteningCheckbox;
    shorteningCheckbox = document.getElementById('showShortening_' + ProductID)
    
    if(shorteningCheckbox.checked){
        //document.getElementById('shortening_' + ProductID).disabled = '';
        document.getElementById('shortening_' + ProductID).style.visibility = 'visible';
    }else{
        document.getElementById('shortening_' + ProductID).value = '';
        //document.getElementById('shortening_' + ProductID).disabled = 'disabled';
        document.getElementById('shortening_' + ProductID).style.visibility = 'hidden';
    }
}

function getCheckedRadio(name) {
    var radioButtons = document.getElementsByName(name);
    for (var x = 0; x < radioButtons.length; x ++) {
        if (radioButtons[x].checked) {
            return radioButtons[x].value;
            //alert("You checked " + radioButtons[x].id);
        }
    }
}

function CalculatePrice(ProductID){
    var productprice;
    var total;
    var hemingprice;
    var shorteningprice;
    var totalwidth;
    var unitRate;
    
    hemingprice = 0;
    shorteningprice = 0
    total = 0;
    totalwidth = 0;
    productprice = 0;
    unitRate = 1; // default is cm. 1 inch = 2.54cm
    
    //alert(getCheckedRadio('units'));
   
    if (getCheckedRadio('units') == "inch"){
        unitRate = 2.54 // convert from inches to cm
    }
    
    if (document.getElementById('width_' + ProductID).value != '' && document.getElementById('basket_' + ProductID + '_quantity').value != '') {
    
        if(isNaN(document.getElementById('width_' + ProductID).value)){
            //alert("Invalid Window Width. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Window Width. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else if(isNaN(document.getElementById('basket_' + ProductID  + '_quantity').value)){
            //alert("Invalid Number of Windows. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Number of Windows. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else if(isNaN(document.getElementById('shortening_' + ProductID).value)){
            //alert("Invalid Shortening to. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Shortening to. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else {
            productprice = document.getElementById('price_' + ProductID).value
        
            // calculate total widht in metres
            totalwidth = toInteger((parseFloat(document.getElementById('totalwidth_' + ProductID).value) * unitRate)/100 + 0.49);
			//totalwidth = (parseFloat(document.getElementById('totalwidth_' + ProductID).value) * unitRate)/100 + 0.49;
            
            //alert(totalwidth);
			//alert(productprice);
            
            total = productprice *  totalwidth ;
            
            if (document.getElementById('showShortening_' + ProductID).checked){
                if (document.getElementById('shortening_' + ProductID).value == ''){
                    alert("Please enter value for Shortening to");
                }
                
                shorteningprice = parseFloat(document.getElementById('Shortening_Price').value) * totalwidth;      
                
            }
            
            if (document.getElementById('sidehemming_' + ProductID).checked){  
                hemingprice = document.getElementById('Side_Hemming_Price').value;       
            }
            
            total = (parseFloat(total) + parseFloat(shorteningprice) + parseFloat(hemingprice)) * document.getElementById('basket_' + ProductID  + '_quantity').value ;
            
            document.getElementById('total_' + ProductID).innerHTML = parseFloat(total).toFixed(2);
            document.getElementById('linetotal_' + ProductID).value = parseFloat(total).toFixed(2);
        }
    } 
	CalculatePriceTotal()
	//else {
    //    alert("Please enter required information before calculating the  price.");
    //}
}


function CalculatePriceWithoutCheckingShorteningValue(ProductID){
    var productprice;
    var total;
    var hemingprice;
    var shorteningprice;
    var totalwidth;
    var unitRate;
    
    hemingprice = 0;
    shorteningprice = 0
    total = 0;
    totalwidth = 0;
    productprice = 0;
    unitRate = 1; // default is cm. 1 inch = 2.54cm
    
    //alert(getCheckedRadio('units'));
   
    if (getCheckedRadio('units') == "inch"){
        unitRate = 2.54 // convert from inches to cm
    }
    
    if (document.getElementById('width_' + ProductID).value != '' && document.getElementById('basket_' + ProductID + '_quantity').value != '') {
    
        if(isNaN(document.getElementById('width_' + ProductID).value)){
            //alert("Invalid Window Width. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Window Width. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else if(isNaN(document.getElementById('basket_' + ProductID  + '_quantity').value)){
            //alert("Invalid Number of Windows. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Number of Windows. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else if(isNaN(document.getElementById('shortening_' + ProductID).value)){
            //alert("Invalid Shortening to. Please Enter Only Numerics");
			$("#measure_message .addToBasketMessage").html("<p>Invalid Shortening to. Please Enter Only Numerics</p>")
			$("#measure_message").dialog({ title: "Warning", width: 450, modal: true });
            return false;
        }else {
            productprice = document.getElementById('price_' + ProductID).value
        
            // calculate total widht in metres
            totalwidth = toInteger((parseFloat(document.getElementById('totalwidth_' + ProductID).value) * unitRate)/100 + 0.49);
			//totalwidth = (parseFloat(document.getElementById('totalwidth_' + ProductID).value) * unitRate)/100 + 0.49;
            
            //alert(totalwidth);
			//alert(productprice);
            //alert(productprice);
			//alert(totalwidth);
            total = productprice *  totalwidth ;
            //alert(total);
            if (document.getElementById('showShortening_' + ProductID).checked){
                /*if (document.getElementById('shortening_' + ProductID).value == ''){
                    alert("Please enter value for Shortening to");
                }*/
                
                shorteningprice = parseFloat(document.getElementById('Shortening_Price').value) * totalwidth;      
                
            }
            
            if (document.getElementById('sidehemming_' + ProductID).checked){  
                hemingprice = document.getElementById('Side_Hemming_Price').value;       
            }
            
            total = (parseFloat(total) + parseFloat(shorteningprice) + parseFloat(hemingprice)) * document.getElementById('basket_' + ProductID  + '_quantity').value ;
            //alert(total)
            document.getElementById('total_' + ProductID).innerHTML = parseFloat(total).toFixed(2);
            document.getElementById('linetotal_' + ProductID).value = parseFloat(total).toFixed(2);
        }
    } 
	CalculatePriceTotal()
	//else {
    //    alert("Please enter required information before calculating the  price.");
    //}
}

function CalculatePriceTotal(){
    var varitationCount;
    var totalPrice;
    var ProductID;
    
    totalPrice = 0;
    
    varitationCount = document.getElementById('variationCount').value;
    
    for (i = 1; i <= varitationCount; i++){
        ProductID = document.getElementById('variation' + i).value;
        totalPrice = totalPrice + parseFloat(document.getElementById('linetotal_' + ProductID).value);
    }
    
    document.getElementById('totalprice').innerHTML = totalPrice.toFixed(2);
}
