/* 
    commonScripts.js
        validation and other javascript routines common to both CWS & WOTG websites
        
        check_search: check search dialogue before submitting
        
*/

var twitterWidget=false; //global object

//windowDependentRoutines: these are javascript functions that work best when the window (rather than dom) has completed loading
function windowDependentRoutines(){
    if ($('.wrapInnerTwitter').length){
        eval('activateTwitterFeed();');
    }
    //enablePanels: enable sliding panels
    eval('enablePanels();');
    //profile page map
    if ($('#detailMap').length){
        eval('recommendedMap(\'detailMap\');');
    }    
    //trap change event on search bar listboxes
    eval('enableSearchBar();');
    eval('enableCurrencyController();');
    eval('enableAdditionalNarratives();');
    //eval('checkInstalled();');
 }


//check_search: check search dialogue before submitting
function check_search(){
    var b_return=false;
    if (parseInt($('#priceRange').val()) > 0){b_return=true}
    if (parseInt($('#brand').val()) > 0){b_return=true}
    //if (parseInt($('#search_distilleries').val()) > 0){b_return=true}
    //if (parseInt($('#search_brands').val()) > 0){b_return=true}
    if ($('#quick_kw').val()=='By Keyword'){
        $('#quick_kw').val('')
    }
    if ($('#quick_kw').val().length!=0){b_return=true}
    return b_return;
}

function activateTwitterFeed(){
    var twitterUserAccount=$('#twitterName').text();
   //alert(twitterUserAccount);
    twitterWidget.render().setUser(twitterUserAccount).start()
}

//enablePanels: enable sliding panels
function enablePanels(){
    //Full Caption Sliding (Hidden to Visible)
    $('.panel').hover(function(){
    	$(".panelNote", this).stop().animate({top:'80px'},{queue:false,duration:1000});
    }, function() {
    	$(".panelNote", this).stop().animate({top:'172px'},{queue:false,duration:160});
    });

}

//enableSearchBar: trap the onchange event for select boxes in search bar area
function enableSearchBar(){
    if ($('#brand').length){
        $('#brand').bind('change',function(){
            if (parseInt($('#brand').val()) > 0){
                var url=$('#u_base').val() + '-brand-' + $('#brand').val() + '-z-brand.htm'
                window.location.href=url;
            }
        });
    }
    if ($('#priceRange').length){
        $('#priceRange').bind('change',function(){
            if (parseInt($('#priceRange').val()) > 0){
                var url=$('#u_base').val() + '-price-' + $('#priceRange').val() + '-results-1-z-search_results.htm'
                window.location.href=url;
            }
        });
    }



}

//enableCurrencyController: attach change event to the currency selection list
function enableCurrencyController(){
//alert("currency")
    if ($('#list_currencies').length){
        $('#list_currencies').bind('change',function(){
            if (parseInt($('#list_currencies').val()) > 0){
                var url=$('#cURL').val() + '-currency-' +$('#list_currencies').val() + '.htm'  ;
               // alert(url)
                window.location.href=url;
            }
        });
    }

}

//enableAdditionalNarratives(): slide down additional narrative if it exists
function enableAdditionalNarratives(){
    if ($('div.additionalNarrative').length){
        $('a.moreText').bind('click',function(e){
                e.preventDefault();
                
                
                $('div.defaultNarrative').toggle('fast');
                $('div.additionalNarrative').toggle('fast');
        });
    
    }

}

//recommendedMap: display profile detail map
function recommendedMap(paramName){
    var lat=parseFloat($('#lattitude').val());
    var lon=parseFloat($('#longtitude').val());
    var myLatlng = new google.maps.LatLng(lat,lon);
    var myOptions = {
        zoom: 8,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById(paramName), myOptions);
                //create marker
                    var marker = new google.maps.Marker({
                          position: new google.maps.LatLng(parseFloat($('#lattitude').val()),parseFloat($('#longtitude').val())), 
                          map: map,
                          icon: "http://google-maps-icons.googlecode.com/files/info.png",
                          title: ''
                           });
                                     
                 //bind info window
                        //google.maps.event.addListener(marker, 'click', function() {
                        //alert("pop")
                        //  info.open(map,marker);
                       // });                               


return void[0];    
}


//displayForm: popup an overlay with contact form for making contact with a profile
function displayForm(){
    var url=$('#u_base').val() + 'contactProfile-item-' + $('#profileID').val() + '-z-_contact.htm';
    //open_link(url);
    //return void[0];
    $("#smallInteractive").load(url,'',function(){
                        $("#smallInteractive").overlay({
                                    effect: 'apple',
                                    speed: 'fast',
                                	expose: {
                                		color: '#000',
                                		loadSpeed: 200,
                                		opacity: 0.5
                                	},
                                    onLoad: function(){ 
                                         $('#butClose').bind('click',function(){
                                            $('#smallInteractive').overlay().close();
                                         }); 
                                         
                                            $('#profileContact :input').each(function( e, item ){
                                                        $(this).bind('focus',function(){
                                                            $(this).closest('tr').toggleClass('focused');
                                                        });
                                                        $(this).bind('blur',function(){
                                                            $(this).closest('tr').removeClass('focused');
                                                        });
                                            });                                                                            
                                    },
                                    api: true
                                	
                                }).load(); 
                                
//        alert("");
        
        });
    
}

//validateProfileEnquiry: test the form before posting
function validateProfileEnquiry(){

    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;
    }
    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 enter the message you would like to send!");
        $('#txt_comment').focus();
        return false;
    }
    
    if ($('#newsletteroption').attr('checked')==false){
        $('#newsLetter').val('0');
    }
    //$('#your_details').val('');
    //
    
    return true;
 }
 

//validate_subscribe: validate the 'subscribe box' 
function validate_subscribe(){
$('#subscribe_email').val(jQuery.trim($('#subscribe_email').val()));
    if ($('#subscribe_email').val()=='enter email address'){
        alert("please enter your email address!");
        return false;
    }
    
    var e=$('#subscribe_email').val()
    if (e.length==0){
        alert("please enter your email address!");
        return false;
    }
    //check that this is in supported format
    if (!check_email(e,true)){
        show_error_message("please check your email address as it does not seem to be valid",'error_message_panel');
        $('#user_name').focus();
        return false;
    }
    window.location.href=$('#u_base').val() + 'subscribe-z-subscribe-e-' + e + '.htm'
    return false;
}


function validateEnquiry(){
//    alert('here');
    
    //txt_name
    if ($('#your_name').val().length==0){
        alert("Please enter a contact name!");
        $('#your_name').focus();
        return false;
    }
    //email address
    if ($('#your_email').val().length==0){
        alert("Please enter a contact email address!");
        $('#your_email').focus();
        return false;
    }
    var e=$('#your_email').val();
    if (!check_email(e,true)){
        alert("Please check that you have entered a valid  email address!");
        $('#your_email').focus();
        return false;
    }
    
    //telephone
    var telephone=fix_telephone($('#your_telephone').val());
    $('#your_telephone').val(telephone);
    
    if (telephone.length==0){
        alert("Please enter a contact telephone number!");
        $('#your_telephone').focus();
        return false;
    }

    //message
    if ($('#your_message').val().length==0){
        alert("Please tell us how we can assist!");
        $('#your_message').focus();
        return false;
    }
    
    if ($('#newsletteroption').attr('checked')==false){
        $('#reference').val('Do not add this customer to the mailing list!');
    }
    $('#your_details').val('');
    //
    
    return true;
 }
 


function applyCSS(){

    //user form elements: add onfocus/onblur behaviour
        $(':input').each(function( e, item ){
           var elementClassName=$(this).attr('class');
           switch(elementClassName){
                case '': // no class specified so ignore
                    break;
                default: //follow focus / blur events
                    $(this).focus(function(){
                        $(this).closest('tr').toggleClass('focused');
                    });
                    $(this).blur(function(){
                        $(this).closest('tr').removeClass('focused');
                    });
           }
        });

}
