/* 
crann.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/
$(document).ready(function(){setupPage();});
$(window).bind('load',function(){eval('windowDependentRoutines();');});

function setupPage(){
    //alert($().jquery);
   eval('activateGallery();');    

    if ($('#subscribe_email').length){
        //alert("here")
        $("#subscribe_email").bind("click", function(event){
           if ($('#subscribe_email').val()=='enter email address'){
            $('#subscribe_email').val('');
           } 
        });
    }    

    if ($('#quick_kw').length){
        //alert("here")
        $("#quick_kw").bind("click", function(event){
           if ($('#quick_kw').val()=='By Keyword'){
            $('#quick_kw').val('');
           } 
        });
    }    
    //activate superfish menu
  // eval('superfishMenu();');
    //return void[0];
    //trap external links
    eval('outbound_links();');
    //validate billing options
    eval('billing();');
    //enable paging
    eval('paging();');
    //fix_height();
    
}

//superfishMenu: lhsMenu Animation
function superfishMenu(){
$("ul.sf-menu").supersubs({ 
            minWidth:    2,   // minimum width of sub-menus in em units 
            maxWidth:    21,   // maximum width of sub-menus in em units 
            extraWidth:  2     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish(); 
}

function manage_flash(e){
return void[0]; 
}  




//outbound_links: ensure that outbound links open in a new window
function outbound_links(){
    $("a", "#rhsContentArea").bind("click", function(event){  
    var b=$('#u_base').val();
    var href=$(this).attr("href").toString();
    
    //alert(b);
    //alert(href);
        if ((href.indexOf(b)==-1)&&(href.indexOf('javascript')==-1)&&(href.indexOf('mailto')==-1)&&(href.indexOf('../')==-1)&&(href.indexOf('https')==-1)){
            var n_ref='javascript: open_link(\'' + href + '\');';
            $(this).attr("href",n_ref);
        }
        else{
            if ((href.indexOf('.pdf')!=-1)&&(href.indexOf('javascript')==-1)){
            var n_ref='javascript: open_link(\'' + href + '\');';
            $(this).attr("href",n_ref);
            
            }
        }
    });     
    
}


function fix_height(){
 
    var h=$(window).height()-199
    var d=$(document).height()-199
    if (($('#main_panel').height()) < h){
       $('#main_panel').height(h);
    }
    else{
       $('#main_panel').height('auto');    
    }
}


// paging: catch paging links and replace with ajax calls
function paging(){
    $("a", "div.paging").bind("click", function(event){ 
        
          event.preventDefault();
          var href=$(this).attr('href');
          var n_ref=href;
          if (href.indexOf('z-catalogue') > -1){
             n_ref=href.replace('z-catalogue','z-_catalogue');
          }
          if (href.indexOf('z-distillery') > -1){
             n_ref=href.replace('z-distillery','z-_distillery');
          }
          if (href.indexOf('z-brand') > -1){
             n_ref=href.replace('z-brand','z-_brand');
          }
          if (href.indexOf('z-promotion') > -1){
             n_ref=href.replace('z-promotion','z-_promotion');
          }
          if (href.indexOf('z-country') > -1){
             n_ref=href.replace('z-country','z-_country');
          }
          if (href.indexOf('z-promotionalgroup') > -1){
             n_ref=href.replace('z-promotionalgroup','z-_promotionalgroup');
          }
          if (href.indexOf('z-search_results') > -1){
             n_ref=href.replace('z-search_results','z-_search_results');
          }
          //alert(n_ref);
                    $("div.paging").html($('#ajax_progress_bar').html()); 
                    page_products(n_ref);
                    //$(this).attr('href','javascript:page_products(\'' + n_ref + '\');');
           });
           
              
      }

function page_products(url){
    //window.location.href=url
    //return void[0];
    //alert(url);
    $('#product_listing').load(url,'',function(){
       // alert(url);
        paging();
    });
}



//open_link: opens link in a new window
function open_link(url){
    var d=Math.random();
    var n='win' + d.toString().replace('.','') //window name
    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,n,settings);    
}







function cws_form(){
//    alert('here');
    //txt_company
    
    //txt_name
    if ($('#txt_name').val().length==0){
        alert("Please enter a contact name!");
        $('#txt_name').focus();
        return false;
    }
    
    //telephone
    var telephone=fix_telephone($('#txt_telephone').val());
    $('#txt_telephone').val(telephone);
    
    if (telephone.length==0){
        alert("Please enter a contact telephone number!");
        $('#txt_telephone').focus();
        return false;
    }

    //email address
    if ($('#txt_email').val().length==0){
        alert("Please enter a contact email address!");
        $('#txt_email').focus();
        return false;
    }
    $('#txt_email').val(jQuery.trim($('#txt_email').val()));
    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;
    
    }
    //message
    if ($('#txt_comment').val().length==0){
        alert("Please tell us how we can assist!");
        $('#txt_comment').focus();
        return false;
    }
    
    $('#your_details').val('');
    //
    
    return true;
 }
 
function fix_telephone(param_value){
    var ret_value='';
    var wip_value=param_value;
    var a_values='1234567890+-'
    //alert(wip_value.length);
    for (var i=0;i<=wip_value.length;i++){
    //alert(wip_value.charAt(i))
        for (var x=0;x<=a_values.length;x++){
            
            if (wip_value.charAt(i)==a_values.charAt(x)){
                ret_value+=wip_value.charAt(i)
            }
        }
    } 
    //alert(ret_value);
    return ret_value;   
}




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;
}





function show_error_message(param_message,param_wrapper, param_div){
alert(param_message);
return void[0];
    if (param_div==false){
        alert(param_message);
    }
    else{
            $('#display_error_message').innerHTML=param_message;
            //$(param_wrapper).absolutize();
            //$(param_wrapper).setStyle({top: 200, left:400, zIndex:100});
            $('#display_error_message').show('slow');
            setTimeout(function(){hide_error_message(param_wrapper)},2000);
/*
            new Effect.Appear(param_wrapper,{
                duration: 0.5, 
                afterFinish: function(){
                setTimeout(function(){hide_error_message(param_wrapper)},2000);
            }});
*/            
    }
}
// hide the error message panel
function hide_error_message(param_wrapper){
        $('#' + param_wrapper).hide('slow');

}

function record_stat(param_context,param_id){
var a_url=$('#u_base').val() + "asp/fanore.asp";
var a_data="action=_stat&context=" + param_context + "&contextid=" + param_id
//alert(a_data)
$.ajax({
   type: "GET",
   url: a_url,
   data: a_data
 });
}
 


//validate_reminder: test the password reminder dialogue before submitting
function validate_reminder(){
    //email address
    if ($('#user_name').val().length==0){
        alert("Please enter your email address!");
        $('#user_name').focus();
        return false;
    }
    $('#user_name').val(jQuery.trim($('#user_name').val()));
    var e=$('#user_name').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#user_name').focus();
        return false;
    
    }
    $('#reminder_details').val('');
    return true;         
    
}

//validate_password_update: validate that password conforms to business rules
function validate_password_update(){
    //user_pass
    var p=$('#user_pass').val();
    //check that password has been entered
    if (p.length==0){
        show_error_message("please enter your current password (this was sent to you when you registered)",'error_message_panel');
        $('#user_pass').focus();
        return false;
    }
    $('#user_pass').val(jQuery.trim($('#user_pass').val()));
    p=$('#user_pass').val();
    //check that password conforms
    if (p.length<6){
        show_error_message("Your password should be at least 6 characters long",'error_message_panel');
        $('#user_pass').focus();
        return false;
    
    }
    if (p.length> 10){
        show_error_message("Your password should be no longer than 10 characters long",'error_message_panel');
        $('#user_pass').focus();
        return false;
    }
    //new_user_pass
    var n=$('#new_user_pass').val();
    //check that password has been entered
    if (n.length==0){
        show_error_message("Please enter your new password",'error_message_panel');
        $('#new_user_pass').focus();
        return false;
    }
    $('#new_user_pass').val(jQuery.trim($('#new_user_pass').val()));
    n=$('#new_user_pass').val();
    //check that password conforms
    if (n.length<6){
        show_error_message("Your new password should be at least 6 characters long",'error_message_panel');
        $('#new_user_pass').focus();
        return false;
    
    }
    if (n.length> 10){
        show_error_message("Your new password should be no longer than 10 characters long",'error_message_panel');
        $('#new_user_pass').focus();
        return false;
    }
    $('#confirm_user_pass').val(jQuery.trim($('#confirm_user_pass').val()));
    //confirm_user_pass
    var c=$('#confirm_user_pass').val();
    if (c.length==0){
        show_error_message("Please re-enter your new password",'error_message_panel');
        $('#confirm_user_pass').focus();
        return false;
    }
    //check that new and confirm match
    if (c.toLowerCase()!=n.toLowerCase()){
        show_error_message("Please check your entry, the new password and confirm password entries do not match!",'error_message_panel');
        $('#new_user_pass').focus();
        return false;
    }
    return true;
    
}


 function gotoAlternate(url){
 
 window.location.href=url;
 }
