/* 
power.js: library of javascript for Power Insurances web site
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
  validate_search_form: test that user has entered valid keywords  
*/

var pausecontent=false; // global variable used for scrolling news area
var icon=false; // used for icon representing position on map


function setup_page(){
    // fix the height of page in case we're using a different browser than ie 7
    
    //Event.observe('Window','resize',fix_height,false);

  //  var el=get_el("scrolling_news");

    if ($("#scrolling_news").length){
        eval('start_scroller();');
    }

    if ($('#map').length){
        eval('load_map();');
    }
    if ($('#info_box').length){
        eval('$(\'#info_box\').hide();');
    }
    if ($('#renewal_form').length){
    $('img','#renew').removeClass('no_show');
    $('img','#renew').bind('mouseover',function(){
        $('#' + this.id + '_information').show('fast');
    });
    $('img','#renew').bind('mouseout',function(){
        $('.help').hide();
        //$('#' + this.id + '_information').show('fast');
    });
    
    $('img','#renew').hide();
           $('input,select','#renewal_form').bind('focus',function(){
            $('img','#renew').hide();
            $('#' + this.id + '_help').show('slow');
           });
           $('#renewal_form').submit(function(){
                    return eval($('#renewal_validation').val());
        
             }); 
           
    }
    
    //offer_form
    if ($('#offer_form').length){
        $('#offer_form').submit(function(){
                    return eval($('#offer_validation').val());
        
             }); 
    }
    //payment_form
    if ($('#payment_form').length){
     $(":checkbox", "#payment_form").bind("click", function(){ 
        var on=this.checked;
        var id=this.id // the option that has been ticked
        
        var price=$('#price_' + id.replace('opt_','')).text(); // price for this option
        
        var total=$('#total').text() // total currently displayed to the customer
        
        var n_hash=$('#hash' + id.replace('opt_','')).val(); // hash value for base + this option only
        var n_amount=$('#amount' + id.replace('opt_','')).val(); // formatted amount for base + this option only
        var o_hash=$('#o_hash').val(); //hash for base price only
        var o_amount=$('#o_amount').val(); // formatted amount of base price only
        
        if (on==true){
            var n_total=parseFloat(total) + parseFloat(price);
            //$('#AMOUNT').val(n_amount);
//$('#SHA1HASH').val(n_hash);
            var url="../asp/ajax.asp?action=_add&id=" + id.replace('opt_','') + '&recordid=' + $('#recordid').val();
            $.getJSON(url,function(data){
                $('#AMOUNT').val(data.total);
                $('#SHA1HASH').val(data.hash);
                $('#TIMESTAMP').val(data.timestamp);
            },"json");
        }
        else{
            var n_total=parseFloat(total) - parseFloat(price);
            $('#AMOUNT').val(o_amount);
            $('#SHA1HASH').val(o_hash);
            var url="../asp/ajax.asp?action=_remove&id=" + id.replace('opt_','') + '&recordid=' + $('#recordid').val();
            $.getJSON(url,function(data){
                $('#AMOUNT').val(data.total);
                $('#SHA1HASH').val(data.hash);
                $('#TIMESTAMP').val(data.timestamp);
            },"json");
        }
        $('#total').text(n_total.toFixed(2));
        
        
       });
    }
    if ($('#test_harness').length){
        $('#but_cancel').bind('click',function(){$('#RESULT').val('-1');$('#test_harness').submit();});
        $('#but_success').bind('click',function(){$('#RESULT').val('00');$('#test_harness').submit();});
        $('#but_fail').bind('click',function(){$('#RESULT').val('01');$('#test_harness').submit();});
        
    }
    
    fix_height();
}


function fix_height(){
    var h=$(window).height()-282
    if (($('#main_panel').height()) < h){
       $('#main_panel').height(h);
    }
    else{
       $('#main_panel').height('auto');    
    }
}

function load_map() {

	if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl())
//        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(52.64721663731051, -8.639373779296875), 12);

        var point = new GPoint(parseFloat(-8.639373779296875), parseFloat(52.64721663731051));
        var marker = new GMarker(point);
        
        //Longitude , Latitude 
//(-7.411994934082031, 52.34844860104819
        var icon = new GIcon();
        icon.image = "../images/pointer.gif";
        icon.iconSize = new GSize(32,32);
        icon.iconAnchor = new GPoint(0,32);
        icon.infoWindowAnchor = new GPoint(10, 20);
        
        var marker = new GMarker(point,icon);
        marker.title="Power Insurances"
        GEvent.addListener(marker, "click", function() {
//        showMapBlowup(point, opts?)
        marker.showMapBlowup({maxWidth:80});
        });
        
        map.addOverlay(marker);
        return marker

    }
 }
 

function clear_text(){
    var el=get_el("txt_username");
    if (el){
        el.value='';
    }
}

//validate_search_form: test that user has entered valid keywords 
function validate_search_form(){
    var el=get_el("txt_keywords");
    if (el){
        
    }
    return true;
}




function open_link(url){
 var settings='Height=' + screen.height + ',Width=' + screen.width + ',Top=0,left=0,scrollbars=yes,resizable=1, location=1,status=1'
 var w=window.open(url,'power',settings);    
}

/*validate_contact_form: validate the contact us/ enquiry form
 */
 function validate_contact_form(param_type){
 
 
 
 
    // txt_company
//        var el=get_el("txt_company");
//        if (el){
//            if (el.value.length==0){
//                alert("Please enter the Company Name.");
//                el.focus();
//                return false;
//            }
//        }
    switch(param_type){
        case 2: //application form
            var el=get_el("txt_address1");
            if (el){
                if (el.value.length==0){
                    alert("Please enter at least a two line address!");
                    el.focus();
                    return false;
                }
            }
            var el=get_el("txt_address2");
            if (el){
                if (el.value.length==0){
                    alert("Please enter at least a two line address!");
                    el.focus();
                    return false;
                }
            }
                    
            var el=get_el("txt_address4");
            if (el){
                if (el.options.selectedIndex==0){
                    alert("Please select the county from the list provided!");
                    el.focus();
                    return false;
                }
            }
            
            var el=get_el("reference");
            if (el){
                if (el.options.selectedIndex==0){
                    alert("Please select the number of employees from the list provided!");
                    el.focus();
                    return false;
                }
            }
                    
        }       //end switch statement 
        
    // txt_name
        var el=get_el("txt_name");
        if (el){
            if (el.value.length==0){
                alert("Please enter a contact name.");
                el.focus();
                return false;
            }
        }
    // txt_email
        var el=get_el("txt_email");
        if (el){
            if (el.value.length==0){
                alert("Please enter a contact email address.");
                el.focus();
                return false;
            }
        }
        var s=el.value;
        if (!fixstring(s,true)){
            alert("The email address you have entered is not supported on this system, please check your entry!");
            el.focus();
            return false;
        
        }
        
        
    // txt_telephone
//        var el=get_el("txt_telephone");
//        if (el){
//            if (el.value.length==0){
//                alert("to help us assist you better, please enter your message.");
//                el.focus();
//                return false;
//            }
//        }
    switch (param_type){    
        case 0: // ordinary 'contact us' form
        // txt_comment
            var el=get_el("txt_comment");
            if (el){
                if (el.value.length==0){
                    alert("to help us assist you better, please enter your message.");
                    el.focus();
                    return false;
                }
            }
            break;
        
    }    
    return true;
 }
 
 //validate_renewal: check that all fields in renewal form have been entered
 function validate_renewal(){
    //check that valid policy type selected
        var t=$('#list_types').val();
        if(t==0){
            alert("Please select the policy type!");
            $('#list_types').focus();
            return false;
        }
        
        
    //validate the email address format
        if ($('#email').val().length==0){
            alert("Please enter your email address!");
            $('#email').focus();
            return false;
        }

        var e=$('#email').val();
        if (!check_email(e,true)){
            alert("Please check that you have entered a valid  email address!");
            $('#email').focus();
            return false;
        }
    
    //check that policy holder name has been entered
        if ($('#policy_holder').val().length==0){
            alert("Please enter the policy holder\'s name!");
            $('#policy_holder').focus();
            return false;
        }
    
    //check that policy number has been entered
        if ($('#policy_number').val().length==0){
            alert("Please enter the policy number!");
            $('#policy_number').focus();
            return false;
        }
    //check that a numeric value has been entered for the price
        if ($('#amount').val().length==0){
            alert("Please enter the price as shown on your renewal letter!");
            $('#amount').focus();
            return false;
        }
        var p=$('#amount').val();
        p=p.replace('€','');
        p=p.replace('&euro;','');
        //alert(p);
        if (isNaN(p)){
            alert("Please enter only numeric values (e.g. 129.99) for the price, as indicated on your renewal letter!");
            $('#amount').val('');
            $('#amount').focus();
            return false;
        }
           $('#amount').val(p);
           $('#renewal_details').val('');
    //   
        return true;
 }
 
 //validate_offer: ensure we have name, email and telephone number
 function validate_offer(){
    //validate the email address format
        if ($('#email').val().length==0){
            alert("Please enter your email address!");
            $('#email').focus();
            return false;
        }

        var e=$('#email').val();
        if (!check_email(e,true)){
            alert("Please check that you have entered a valid  email address!");
            $('#email').focus();
            return false;
        }
    
    //check that policy holder name has been entered
        if ($('#txt_name').val().length==0){
            alert("Please enter your name!");
            $('#txt_name').focus();
            return false;
        }
    
    //check that policy number has been entered
        if ($('#txt_telephone').val().length==0){
            alert("Please enter your telephone number!");
            $('#txt_telephone').focus();
            return false;
        }
            $('#offer_details').val('');
    //   
        return true;

 }
 
 
 
 
 
 
 
 
 
 
 
 
 //check_email: check email address format
 function check_email(svalue, bmail){
    var str=svalue;
    var sinvalid="!,£,\$,%,',\&";
    
    if (bmail==true){
    	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
    }
    else{
    	var reg1 = /(\.\.)|(^\.)|(')|(%)|(")/; // not valid
    	var reg2 = /([a-zA-Z0-9])|([a-zA-Z])|([0-9])/; // valid
    
    }
    if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
    	return true;
    	}
    return false;
}


 //validate_application_form: builds upon the contact us form and validates the address etc.
 function validate_application_form(){
        
        return validate_contact_form(2)
 }
 
 function display_narrative(param_el){
    $('#info_box').html($('#' + param_el).html())
    $('#info_box').toggle();
    return void[0];
 }