/* 
crann.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/

function setup_page(){
    //alert("settings")
    if ($('#map').length){
        eval('load_map();');
    }
    
    $('ul#speciesgallery').innerfade({
    		speed: 2000,
    		timeout: 10000,
    		type: 'sequence',
    		containerheight: '400px'
    	});
    	$('li','#speciesgallery').removeClass('no_show');    
    if ($('#form_join_us').length){
        $('#membership').bind("change", function(){
            $('#amount').val($('#m' + this.options[this.selectedIndex].value).val());
        }); 
        $('#form_join_us').submit(function(){
            return eval($('#signup_validation').val());
        }); 
    }
    if ($('#form_donate').length){
        $('#form_donate').submit(function(){
            return eval($('#donate_validation').val());
        }); 
    
    }
    $('#subscription_form').submit(function(){
        return eval($('#subscription_validation').val());
        
    });  
    
    $("a", "#home_main").hover(function(){  
    
        var css_prop=$(this).css("background");
        css_prop=css_prop.replace('off','on');
        $(this).css('background',css_prop);
        }
        ,function(){
        var css_prop=$(this).css("background");
        css_prop=css_prop.replace('on','off');
        $(this).css('background',css_prop);
        
        }
     );
     //bind to all external links in page
     $('a').bind('click',function(){
        var href=$(this).attr('href');
        var b_ref=$('#u_base').val();
        if ((href.indexOf(b_ref)==-1)&&(href.indexOf('#')==-1)&&(href.indexOf('javascript')==-1)){
            var n_ref='javascript: open_link(\'' + href + '\');';
            $(this).attr('href',n_ref);
        }
     });
    //preload home page images
    preload_images()
    
    
    fix_height();
}

function preload_images(){
    var b=$('#u_base').val();
    var url=b + 'images/1_on.jpg'
    var img=new Image();
    img.src=url;
    var url=b + 'images/6_on.jpg'
    var img=new Image();
    img.src=url;
    var url=b + 'images/7_on.jpg'
    var img=new Image();
    img.src=url;
    
}
//check_subscription: check that name and email address have been entered
function check_subscription(){
    //alert("check_subscription");
    
    if ($('#your_name').val().length==0){
        alert("Please enter your full name!");
        $('#your_name').focus();
        return false;
    }
    
    //email address
    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;
    
    }
    $('#subscription_details').val('');
    return true;
}


//validate_donate: validate the donation form fields
function validate_donate(){
    if (parseInt($('#amount').val())==0){
        alert("Please select the amount you wish to donate!");
        $('#amount').focus();
        return false;
    }
    //billing_name
    if ($('#billing_name').val().length==0){
        alert("Please enter your full name!");
        $('#billing_name').focus();
        return false;
    }
    //billing_email
    if ($('#billing_email').val().length==0){
        alert("Please enter your email address!");
        $('#billing_email').focus();
        return false;
    }
    var e=$('#billing_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#billing_email').focus();
        return false;
    
    }
    //billing_address1
    if ($('#billing_address1').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address1').focus();
        return false;
    }
    
    //billing_address2
    if ($('#billing_address2').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address2').focus();
        return false;
    }
    //billing_address3
    if ($('#billing_address3').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address3').focus();
        return false;
    }
    //billing_address4
    //billing_telephone
    if ($('#billing_telephone').val().length==0){
        alert("Please enter your contact telephone number!");
        $('#billing_telephone').focus();
        return false;
    }
     $('#donate_details').val('');
    return true;
   

}

//validate_signup: validate the sign up form 
function validate_signup(){
    if (parseInt($('#membership').val())==0){
        alert("Please select the membership level required!");
        $('#membership').focus();
        return false;
    }
    //ensure that amount matches the member level
    $('#amount').val($('#m' + $('#membership').val()).val());
    
    //billing_name
    if ($('#billing_name').val().length==0){
        alert("Please enter your full name!");
        $('#billing_name').focus();
        return false;
    }
    //billing_email
    if ($('#billing_email').val().length==0){
        alert("Please enter your email address!");
        $('#billing_email').focus();
        return false;
    }
    var e=$('#billing_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#billing_email').focus();
        return false;
    
    }
    //billing_address1
    if ($('#billing_address1').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address1').focus();
        return false;
    }
    
    //billing_address2
    if ($('#billing_address2').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address2').focus();
        return false;
    }
    //billing_address3
    if ($('#billing_address3').val().length==0){
        alert("Please enter your full address!");
        $('#billing_address3').focus();
        return false;
    }
    //billing_address4
    //billing_telephone
    if ($('#billing_telephone').val().length==0){
        alert("Please enter your contact telephone number!");
        $('#billing_telephone').focus();
        return false;
    }
    
    //delivery option
    if (parseInt($('#delivery').val())==0){ 
        alert("Please select the delivery address you are using!");
        $('#delivery').focus();
        return false;
       
    }
    
    if (parseInt($('#delivery').val())==1){ // indicates that delivery address is as above
        $('#delivery_name').val($('#billing_name').val());
        $('#delivery_address1').val($('#billing_address1').val());
        $('#delivery_address2').val($('#billing_address2').val());
        $('#delivery_address3').val($('#billing_address3').val());
        $('#delivery_address4').val($('#billing_address4').val());
    }
    else{
        //implies that user has specified a delivery address which needs to be validated
    //delivery_name
        if ($('#delivery_name').val().length==0){
            alert("Please enter the full name of the person receiving this membership subscription!");
            $('#delivery_name').focus();
            return false;
        }
    //delivery_address1
        if ($('#delivery_address1').val().length==0){
            alert("Please enter the full address of the person receiving this membership subscription!");
            $('#delivery_address1').focus();
            return false;
        }
    //delivery_address2
        if ($('#delivery_address2').val().length==0){
            alert("Please enter the full address of the person receiving this membership subscription!");
            $('#delivery_address2').focus();
            return false;
        }
    //delivery_address3
        if ($('#delivery_address3').val().length==0){
            alert("Please enter the full address of the person receiving this membership subscription!");
            $('#delivery_address3').focus();
            return false;
        }
    //delivery_address4
    
    }
    $('#signup_details').val('');
    return true;
}

function fix_height(){
    var h=$(window).height()-272
    if (($('#main_panel').height()) < h){
       $('#main_panel').height(h);
    }
    else{
       $('#main_panel').height('auto');    
    }
}

//submit_form: utility that will perform client side validation of the passed form id and allow validator to post it 
function submit_form(param_form){
    var el=get_el("cs_validation");
    //alert("here");
    if (el){
        var f=$(param_form)
        var cs_validation=f['cs_validation'];
         return eval($(cs_validation).getValue());
    }
    else{
        $(param_form).submit();
    }
}

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,'popup',settings);    
}

//validate_search: before posting, make sure that something has been entered
function validate_search(){
    if ($F('txt_keywords').blank()){
        alert("please enter one or more keywords to perform a search!");
        $('txt_keywords').focus();
        return void[0];
    }
    $('search_details').value="";
    $('search_form').submit();
}

//validate_form('form_contact_us')
function validate_form(param_form){
    switch (param_form){
        case 'form_contact_us':
            if ($('txt_context').val()=='advertising_rates'){
                //company name
                if ($('#txt_company').val().length==0){
                    alert("Please enter your company or organisation name!");
                    $('#txt_company').focus();
                    return false;
                }
                
            }
            //name
            if ($('#txt_name').val().length==0){
                alert("Please enter your full name!");
                $('#txt_name').focus();
                return false;
            }
            
            //email address
            if ($('#txt_email').val().length==0){
                alert("Please enter your email address!");
                $('#txt_email').focus();
                return false;
            }

            var e=$('#txt_email').val();
            if (!check_email(e,true)){
                alert("Please check that you have entered a valid  email address!");
                $('#txt_email').focus();
                return false;
            }
            //telephone
            if ($('#txt_telephone').val().length==0){
                alert("Please enter a contact telephone number!");
                $('#txt_telephone').focus();
                return false;
            }
            //message
            if ($('#txt_comment').val().length==0){
                alert($('#txt_validator').val());
                $('#txt_comment').focus();
                return false;
            }
            
            break;
        default:
    
    }
    $('#your_details').val('');
return true;
}


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;
}



