﻿$(document).ready(function(){

    // Add the rollovers to the quantity buttons //
    
    $('.Up').hover(function() {
        $(this).attr("src", "i/UpOver.jpg");
    }, function() {
        $(this).attr("src", "i/Up.jpg");
    });
    
    $('.Down').hover(function() {
        $(this).attr("src", "i/DownOver.jpg");
    }, function() {
        $(this).attr("src", "i/Down.jpg");
    });
    
    // Add the rollover to the place order button //
    
    $('#PlaceOrder').hover(function() {
        $(this).attr("src", "i/PlaceOrderHover.jpg");
    }, function() {
        $(this).attr("src", "i/PlaceOrder.jpg");
    });
         
    // Add click functions to quantity buttons //
    
    $('.Up').click(function() {
        
        var txtBox = $(this).parent().parent().find('.QuantityNumber');
        var val = txtBox.val()
        
        if(val == '' || !IsNumeric(txtBox.attr('value'),false)){                    
            txtBox.attr('value','1');                                                     
        }else{        
            var CurrentValue = parseInt(txtBox.attr('value'));        
            CurrentValue++;        
            txtBox.attr('value',CurrentValue.toString());                       
        }               
    });
    
    $('.Down').click(function() {
    
        var txtBox = $(this).parent().parent().find('.QuantityNumber');
        var val = txtBox.val()
    
        if(val == ''){
            txtBox.attr('value','0');     
        }else{
            var CurrentValue = parseInt(txtBox.attr('value'));        
            CurrentValue--;
            if (CurrentValue < 0) {
                CurrentValue=0;
            };
            txtBox.attr('value',CurrentValue.toString());         
        }
    });
    
    //Add validation the quantity boxes
    $('.QuantityNumber').blur(function() {
    
        var val = $(this).val();
    
        if(val == ''){        
            $(this).attr('value','0');        
        }else{
            if(!IsNumeric(val,false)){        
                $(this).attr('value','0');        
            }          
        }

    })
            
    ////////// Add form validation //////////
    
    function IsNumeric(strString,AllowSpecialChars)
    //  check for valid numeric strings	
    {
        var strValidChars = "";
        if(AllowSpecialChars){
            strValidChars = "0123456789.-+() ";
        }
        else {
            strValidChars = "0123456789";
        }        
        var strChar;
        var blnResult = true;

        if (strString.length == 0) return false;

        //  test strString consists of valid characters listed above
        for (i = 0; i < strString.length && blnResult == true; i++)
        {
            strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1)
            {
            blnResult = false;
            }
        }
        return blnResult;
    }

    // Company Name //
    
    $('#Validation1').click(function() {    
        $(this).parent().find('#Answer1').focus();            
    });    
        
    $("input[id$='Answer1']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation1").removeClass();
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation1").addClass('Validation');
            $("#Validation1").html('<span>Required</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation1").addClass('ValidationOK');
            $("#Validation1").html('');
        }
        
    });

    // Customers Name //
    
    $('#Validation2').click(function() {    
        $(this).parent().find('#Answer2').focus();            
    });    
        
    $("input[id$='Answer2']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation2").removeClass();
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation2").addClass('Validation');
            $("#Validation2").html('<span>Required</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation2").addClass('ValidationOK');
            $("#Validation2").html('');
        }
        
    });
    
    
    // Phone Number //
    
    $('#Validation3').click(function() {    
        $(this).parent().find('#Answer3').focus();            
    });    
    
    $("input[id$='Answer3']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation3").removeClass();
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation3").addClass('Validation');
            $("#Validation3").html('<span>Required</span>');
        }
        else if (!(IsNumeric(this.value,true))){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation3").addClass('Validation');
            $("#Validation3").html('<span>Numbers only please</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation3").addClass('ValidationOK');
            $("#Validation3").html('');
        }
        
    });
    
    // Email Address //
    
    $('#Validation4').click(function() {    
        $(this).parent().find('#Answer4').focus();            
    });    
    
    $("input[id$='Answer4']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation4").removeClass();
        
        var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation4").addClass('Validation');
            $("#Validation4").html('<span>Required</span>');
        }
        else if (!(filter.test(this.value))){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation4").addClass('Validation');
            $("#Validation4").html('<span>Email address not valid</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation4").addClass('ValidationOK');
            $("#Validation4").html('');
        }
        
    });
    
    // Number of weeks //
    
    $('#Validation5').click(function() {    
        $(this).parent().find('#Answer5').focus();            
    });    
    
    $("input[id$='Answer5']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation5").removeClass();
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation5").addClass('Validation');
            $("#Validation5").html('<span>Required</span>');
        }
        else if (!(IsNumeric(this.value))){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation5").addClass('Validation');
            $("#Validation5").html('<span>Numbers only please</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation5").addClass('ValidationOK');
            $("#Validation5").html('');
        }
        
    });    

    // Start Date //
    
    $('#Validation6').click(function() {    
        $(this).parent().find('#Answer6').focus();            
    });    
        
    $("input[id$='Answer6']").blur(function() {
        
        $(this).parent().parent().removeClass();
        $("#Validation6").removeClass();
        
        if (this.value == ""){
            $(this).parent().parent().addClass('AnswerError');
            $("#Validation6").addClass('Validation');
            $("#Validation6").html('<span>Required</span>');
        }
        else {
            $(this).parent().parent().addClass('Answer');
            $("#Validation6").addClass('ValidationOK');
            $("#Validation6").html('');
        }
        
    });

    $('.sticky').cluetip({
        activation: 'click',
        sticky: true, 
        closePosition: 'title',
        mouseOutClose: true,
        height: '400px',
        width: 650,
        fx:{open: 'fadeIn'},
        positionBy: 'bottomTop',
        closeText: '<img src="i/cross.png" alt="close" />'       
    });    
        
    //Validate everything when order placed
    //$("#PlaceOrder").click(function(){
    //    $('#Answer1').trigger('blur');
    //    $('#Answer2').trigger('blur');
    //    $('#Answer3').trigger('blur');
    //   $('#Answer4').trigger('blur');        
    //});
        
});

function callModal(){

    $.blockUI(); 

}





//Mark's additional stuff 19/08/2010

var telephonePackage = false;
var telephonePackagePrice = 0;
var telephonePackageRemote = 0;
var telephonePackageOnSite = 0;

var pageLoad = function(){

    $(function(){

        $('.radio INPUT').click(calculate);

        $('.QuantityNumber').keyup(calculate);

        $('.QuantityButtons IMG').click(calculate);

        $('SELECT').change(calculate);

        $('#AnswerBox5 INPUT').keyup(calculate);

        calculate();

    });

};

var calculate = function(){
    
    var subtotal = 0;
    var html = '';

    var desks = $('#ddl_room').val();
    var weeks = $('#AnswerBox5 INPUT:first').val();

    $('.Product').each(function(){

        var productID = $(this).prev().val();

        var quantity = 0;
        var price = 0;

        if ($(this).find('.QuantityNumber').length > 0){
            
            quantity = $(this).find('.QuantityNumber').val();

            if (quantity > 0){
            
                price = $(this).find('.price:first').val();
                var pricePerWeek = $(this).find('.pricePerWeek:first').val();                
                
                if (pricePerWeek === 'true' && weeks > 0){

                    html += '<b>' + quantity + 'x ' + $(this).find('.ProductName').text() + '</b><br />&pound;' + price + ' each per week<br /><br />';
                
                    subtotal += (parseFloat(price) * quantity) * weeks;

                }
                else{

                    html += '<b>' + quantity + 'x ' + $(this).find('.ProductName').text() + '</b><br />&pound;' + price + ' each<br /><br />';
                
                    subtotal += parseFloat(price) * quantity;
                
                }
                            
            }
            
        }
        else{

            if (productID == 2){                
                                
                if ($(this).find('INPUT:checked:first').val() === 'Renewal'){
                    
                    quantity = 1;

                    price = $(this).find('.price:first').val();

                    html += '<b>' + $(this).find('.ProductName').text() + ' (Renewal)</b><br />&pound;' + price + ' per desk per week<br /><br />';

                    if (desks > 0 && weeks > 0){
    
                        subtotal += (parseFloat(price) * desks) * weeks;
    
                    }
                    
                }
                                
                if ($(this).find('INPUT:checked:first').val() === 'Initial Installation'){
                    
                    quantity = 1;

                    price = $(this).find('.price:first').val();
                        
                    html += '<b>' + $(this).find('.ProductName').text() + ' (Initial Installation)</b><br />&pound;' + price + ' per desk per week<br /><br />';
                    
                    if (desks > 0 && weeks > 0){
    
                        subtotal += (parseFloat(price) * desks) * weeks;
    
                    }
                                        
                    var installType = $(this).find('INPUT:checked:eq(1)').val();

                    if (installType === 'Remote Activation'){
                    
                        price = $(this).find('.remotePrice:first').val();
                    
                    }
                    else{
                    
                        price = $(this).find('.onsitePrice:first').val();
                    
                    }                    

                    html += '<b>' + installType + '</b><br />&pound;' + price + '<br /><br />';

                    subtotal += parseFloat(price);
                    
                }
                
            }

//            if (productID == 13){
//                                
//                if ($(this).find('INPUT:checked').val() === 'Yes'){
//                    
//                    quantity = 1;

//                    price = $(this).find('.price:first').val();

//                    html += '<b>' + $(this).find('.ProductName').text() + '</b><br />&pound;' + price + '<br /><br />';

//                    subtotal += parseFloat(price);
//                    
//                }
//                
//            }

        }            
                
    });


    //site name
    var siteName = $('#ddl_site').find('OPTION:selected').text();

    if (siteName === 'Please choose a site...'){
    
        siteName = '';
    
    }

    $('#orderSite').text(siteName);


    //room name
    var siteRoom = $('#ddl_room').find('OPTION:selected').text();

    if (siteRoom === 'Please choose a room...'){
    
        siteRoom = '';
    
    }

    $('#orderRoom').text(siteRoom);


    //desks
    var desks = $('#ddl_room').val();

    $('#orderDesks').text(desks);


    //weeks
    var weeks = $('#AnswerBox5 INPUT:first').val();

    if (weeks > 0){
    
        $('#orderWeeks').text(weeks);
    
    }


    $('#orderProducts').html(html);


    subtotal = Math.round(subtotal * Math.pow(10, 2)) / Math.pow(10, 2);

    var vat = subtotal * (vatRate / 100); //vatRate is defined on index.aspx

    vat = Math.round(vat * Math.pow(10, 2)) / Math.pow(10, 2);

    var total = subtotal + vat;

    total = Math.round(total * Math.pow(10, 2)) / Math.pow(10, 2);


    
    subtotal = subtotal.toString();

    if (subtotal.indexOf('.') > -1 && subtotal.indexOf('.') === subtotal.length - 2) { 
                
        subtotal = subtotal + '0'; 
                    
    }

    $('#orderSubtotal').html('&pound;' + subtotal);


    vat = vat.toString();

    if (vat.indexOf('.') > -1 && vat.indexOf('.') === vat.length - 2) { 
                
        vat = vat + '0'; 
                    
    }

    $('#orderVat').html('&pound;' + vat);


    total = total.toString();

    if (total.indexOf('.') > -1 && total.indexOf('.') === total.length - 2) { 
                
        total = total + '0'; 
                    
    }

    $('#orderTotal').html('&pound;' + total);



        

//        $('#ddl_site').change(function(){
//                    
//            $('#orderSite').text($(this).find('OPTION:selected').text());
//            
//        }); 

//        $('#ddl_room').change(function(){
//                    
//            $('#orderRoom').text($(this).find('OPTION:selected').text());
//            $('#orderDesks').text($(this).val());
//            
//        });
    
};