// <![CDATA[
$(function() {
	// Slider
	$('#coin-slider').coinslider({width:960,height:260,opacity:1});
	
	// radius Box
	$('.topnav ul li a.top_level, .topnav ul li ul, .topnav ul li ul li a, .graybox').css({"border-radius":"6px", "-moz-border-radius":"6px", "-webkit-border-radius":"6px"});
	
});

// Cufon
Cufon.replace('h1, h2', { hover: true });


//Clickability to top logo
jQuery(document).ready(function(){
    jQuery("#header .logo").click(function(){
        window.location = "/";
    });
});

//Must checked terms at the login page
jQuery(document).ready(function(){
    if ( $(".loginbox_salesforce #terms_conditions").length ){
       $("#LoginWithAjax_SubmitButton input:submit").click(function(){
            if ( $("#terms_conditions input").is(":checked") ){
                if (typeof(redirect_url) != 'undefined'){
                    //Only terms page, splash-terms-salesforce.php template
                    location.href = redirect_url;
                    return false;
                } else {
                    //ordinary login, splash-salesforce.php
                    return true;
                }
            }else{
                $("#terms_conditions span").css({'color' : 'red', 'font-weight' : 'bold'});
                return false;
            }
       });
    }
    if ( $(".loginbox_salesforce").length ){
        if ($.browser.msie && $.browser.version.substr(0,1)=="7"){
            $(".graybox").css("height","auto");
        }
    }    
});


//************************************Place And Order logic start here********************************

// products selecting page
jQuery(document).ready(function($){
    if( $("#cforms5form").length ){
        //Attach to the "interface"\table cells, early prices
        $("#wp-table-reloaded-id-3-no-1 .row-2 .column-3").html("&pound;"+telephone_early_price);
        $("#wp-table-reloaded-id-3-no-1 .row-3 .column-3").html("&pound;"+isdn_early_price);
        $("#wp-table-reloaded-id-3-no-1 .row-4 .column-3").html("&pound;"+broadband_early_price);
        $("#wp-table-reloaded-id-3-no-1 .row-5 .column-3").html("&pound;"+wifi_early_price);
        $("#wp-table-reloaded-id-3-no-1 .row-6 .column-3").html("&pound;"+voip_early_price);
        //Attach to the "interface"\table cells, standard prices
        $("#wp-table-reloaded-id-3-no-1 .row-2 .column-4").html("&pound;"+telephone_standard_price);
        $("#wp-table-reloaded-id-3-no-1 .row-3 .column-4").html("&pound;"+isdn_standard_price);
        $("#wp-table-reloaded-id-3-no-1 .row-4 .column-4").html("&pound;"+broadband_standard_price);
        $("#wp-table-reloaded-id-3-no-1 .row-5 .column-4").html("&pound;"+wifi_standard_price);
        $("#wp-table-reloaded-id-3-no-1 .row-6 .column-4").html("&pound;"+voip_standard_price);
        //Attach to the "interface"\table cells, late prices
        $("#wp-table-reloaded-id-3-no-1 .row-2 .column-5").html("&pound;"+telephone_late_price);
        $("#wp-table-reloaded-id-3-no-1 .row-3 .column-5").html("&pound;"+isdn_late_price);
        $("#wp-table-reloaded-id-3-no-1 .row-4 .column-5").html("&pound;"+broadband_late_price);
        $("#wp-table-reloaded-id-3-no-1 .row-5 .column-5").html("&pound;"+wifi_late_price);
        $("#wp-table-reloaded-id-3-no-1 .row-6 .column-5").html("&pound;"+voip_late_price);        

        //Gray-out all not active price periods
        switch (current_price_period) {
            case "early":
                $("#wp-table-reloaded-id-3-no-1 .column-4, #wp-table-reloaded-id-3-no-1 .column-5").css('color','lightgrey');
                break;
            case "standard":
                $("#wp-table-reloaded-id-3-no-1 .column-3, #wp-table-reloaded-id-3-no-1 .column-5").css('color','lightgrey');
                break;
            case "late":
                $("#wp-table-reloaded-id-3-no-1 .column-3, #wp-table-reloaded-id-3-no-1 .column-4").css('color','lightgrey');
                break;                
        }

        //******************** Totals calculating***********************

        jQuery("#telephone").keyup(function() {

            if ( !isNumeric(this) ){
                $(".row-2 .column-7").html("&pound;0");
                organize_grand_price();
                return;
            }

            var total_price = $(this).val()*get_price("telephone");
            $(".row-2 .column-7").html("&pound;"+total_price);

            organize_grand_price();

        });

        jQuery("#isdn").keyup(function() {

            if ( !isNumeric(this) ){
                $(".row-3 .column-7").html("&pound;0");
                organize_grand_price();
                return;
            }

            var total_price = $(this).val()*get_price("isdn");
            $(".row-3 .column-7").html("&pound;"+total_price);

            organize_grand_price();

        });

        jQuery("#broadband").keyup(function() {

            if ( !isNumeric(this) ){
                $(".row-4 .column-7").html("&pound;0");
                organize_grand_price();
                return;
            }

            var total_price = $(this).val()*get_price("broadband");
            $(".row-4 .column-7").html("&pound;"+total_price);

            organize_grand_price();

        });

        jQuery("#wifi").keyup(function() {

            if ( !isNumeric(this) ){
                $(".row-5 .column-7").html("&pound;0");
                organize_grand_price();
                return;
            }

            var total_price = $(this).val()*get_price("wifi");
            $(".row-5 .column-7").html("&pound;"+total_price);

            organize_grand_price();

        });

        jQuery("#voip").keyup(function() {

            if ( !isNumeric(this) ){
                $(".row-6 .column-7").html("&pound;0");
                organize_grand_price();
                return;
            }

            var total_price = $(this).val()*get_price("voip");
            $(".row-6 .column-7").html("&pound;"+total_price);

            organize_grand_price();

        });        


        //******************** Adding products to the cart ***********************

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

            if (products_added)
                return true;

            if ( $(".row-7 .column-7").html() != "&pound;0" && $(".row-7 .column-7").html() != "" ){
                $(this).hide();
                $(this).before('<img src="'+bloginfo_url+'/wp-content/themes/blueline/images/ajax-loader.gif" />');
                add2cart();
                return false;
            }else{
                $("#li-5-1").css('cssText','background-color:#F0B0AB !important');
                $('html, body').animate({scrollTop:0}, 'fast');
                return false;
            }

        });


    }
});

//checkout page
jQuery(document).ready(function($){
    //confirmation
    if ( $(".eshopcheckoutconf").length ){
       $(".eshop.cart").show();
       $(".confirmation").show();
    }
})

//last cfroms page, redirecting to the PayPal end of sequence or to the Invoice
//and submit cform with ajax
jQuery(document).ready(function($){
    if( $("#cforms8form").length ){
        $("#sendbutton8").click(function(){
            var invoice_selected = $("#cf8_field_1-1").is(":checked");
            var paypal_selected  = $("#cf8_field_1-2").is(":checked");
            if ( invoice_selected || paypal_selected ){
                $(this).hide();
                $(this).before('<img src="'+bloginfo_url+'/wp-content/themes/blueline/images/ajax-loader.gif" />');
                $.post( bloginfo_url+'/contact-us/place-an-order', $("#cforms8form").serialize()+"&sendbutton8=Continue", function(){
                    var invoice_selected = $("#cf8_field_1-1").is(":checked");
                    var paypal_selected  = $("#cf8_field_1-2").is(":checked");                
                    if (invoice_selected){
                        window.location = bloginfo_url+'/contact-us/place-an-order/shopping-cart/invoice';
                    } else if (paypal_selected) {
                        window.location = bloginfo_url+'/contact-us/place-an-order/shopping-cart/checkout';
                    }
                });
            } else {
                $("#li-8-1").css('cssText','background:none repeat scroll 0 0 #F0B0AB !important;padding:5px !important;');
            }
            return false;
        });
    }
});

/*
 * If the element's string matches the regular expression it is all numbers
 */
function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
        $(elem).val('');
		return false;
	}
}

/* 
 * Get the price for specific product and current active price period
 * as argument accept part of the product's price variable name
 * like "wifi"
 */
function get_price(product_tag){
    return parseInt(window[product_tag+"_"+current_price_period+"_price"]);
}

/*
 * Calculate and set the grand price
 */
function organize_grand_price(){

    var telephone_total_price = 0;
    var telephone_total_price_current_raw = parseInt(String($(".row-2 .column-7").html()).slice(1));
    if (!isNaN(telephone_total_price_current_raw))
        telephone_total_price = telephone_total_price_current_raw;

    var isdn_total_price = 0;
    var isdn_total_price_current_raw = parseInt(String($(".row-3 .column-7").html()).slice(1));
    if (!isNaN(isdn_total_price_current_raw))
        isdn_total_price = isdn_total_price_current_raw;   

    var broadband_total_price = 0;
    var broadband_total_price_current_raw = parseInt(String($(".row-4 .column-7").html()).slice(1));
    if (!isNaN(broadband_total_price_current_raw))
        broadband_total_price = broadband_total_price_current_raw;

    var wifi_total_price = 0;
    var wifi_total_price_current_raw = parseInt(String($(".row-5 .column-7").html()).slice(1));
    if (!isNaN(wifi_total_price_current_raw))
        wifi_total_price = wifi_total_price_current_raw;

    var voip_total_price = 0;
    var voip_total_price_current_raw = parseInt(String($(".row-6 .column-7").html()).slice(1));
    if (!isNaN(voip_total_price_current_raw))
        voip_total_price = voip_total_price_current_raw;    


    var grand_price =   telephone_total_price + 
                        isdn_total_price + 
                        broadband_total_price + 
                        wifi_total_price + 
                        voip_total_price;

    $(".row-7 .column-7").html("&pound;"+grand_price);
}

/*
 * Adding products to the cart, depending on the
 * selecting in the table quantity values.
 */
var products_added = false; //global variable in order to connect add2cart and click event
function add2cart(){

    var needed_requests = 0;
    $("#wp-table-reloaded-id-3-no-1 input").each(function(){
        if( $(this).val() != "" ){
            needed_requests++;
        }
    });    

    //Clear the eshop cart, going through "interface" products if qty is set, submit adding to the cart request
    //after all click on the cform button in order to submit ordinary cforms's form
    $.post( bloginfo_url+'/contact-us/place-an-order/shopping-cart', $(".eshop.eshopcart").serialize()+"&unset=Empty%20Cart", function(){
        $("#wp-table-reloaded-id-3-no-1 input").each(function(){
            if( $(this).val() != "" ){
                var input_id = $(this).attr('id');
                $(".eshop.hilite").each(function(index){
                    if ( String($("input[name=pid]",this).val()).search(input_id) != -1 ){
                        $("input[name=qty]",this).val( $("#"+input_id).val() );
                        $.post(bloginfo_url+'/contact-us/place-an-order/shopping-cart', $("form",this).serialize(), function(data){
                            needed_requests--;
                            if ( needed_requests == 0){
                                products_added = true;
                                $("#sendbutton5").click();
                            }
                        });
                    }
                });
            }
        });
    });

}


// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};


function urldecode (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      bugfixed by: Rob
    // +      input by: e-mike
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // %        note 2: Please be aware that this function expects to decode from UTF-8 encoded strings, as found on
    // %        note 2: pages served as UTF-8
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    return decodeURIComponent((str+'').replace(/\+/g, '%20'));
}



//************************************Place And Order logic end here**********************************

// ]]>

