﻿var searchMenu;
var winelistMenu;
var ourfavouritesMenu;
var finewinesMenu;

window.onload = function() {
    searchMenu = new SDMenu("search_menu");
	searchMenu.init();

	winelistMenu = new SDMenu("winelist_menu");
	winelistMenu.init();
	
	ourfavouritesMenu = new SDMenu("ourfavourites_menu");
	ourfavouritesMenu.init();
	
	finewinesMenu = new SDMenu("finewines_menu");
	finewinesMenu.init();
    
    var param = GetParameter("browseby");
    if ((param != "all") && (param != "type") && (param != "country") && (param != "region") && (param != "grape") && (param != "favourites") && (param != "finewine"))
        CollapseAllMenu();
	
    var ele = document.getElementById("toggleText");
	var text = document.getElementById("ctl00_middle_content_displayText");
	if (ele != null && text != null) {
	    if (GetCookie('ShowAdvancedSearch') == "collapse") {
            //ele.style.display = "none";
            ele.style.visibility = "hidden";
            ele.style.overflow = "hidden";
            ele.style.height = "0px";
		    text.innerHTML = "expand";
  	    }
	    else {
		    //ele.style.display = "block";
		    ele.style.visibility = "visible";
		    ele.style.overflow = "visible";
		    ele.style.height = "245px";
		    text.innerHTML = "collapse";
	    }
	}
	
	InitSearchTextBox();
	InitSubscribeTextBox();
	
	Shadowbox.init({ // initialise 
        language:   "en", 
        players:    ["html"] 
    });
	
	ExternalLinks();
	
	//doShowPngPopUp('divpopup');
}

function ExternalLinks() { 
	if (!document.getElementsByTagName) return; 
 	var anchors = document.getElementsByTagName("a"); 
 	for (var i=0; i<anchors.length; i++) { 
   		var anchor = anchors[i]; 
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
     			anchor.target = "_blank"; 
	 } 
}

function doShowPngPopUp(selectedDiv) {
    popdiv=document.getElementById(selectedDiv);

    if (GetCookie('HasSeenPopup') == null || GetCookie('HasSeenPopup') == "") {
	    //popdiv.style.visibility="visible";
	    popdiv.style.display = "";

	    SetCookie('HasSeenPopup', '1');
	}
	else
	    popdiv.style.display = "none";
}

function doHidePngPopUp(selectedDiv) {
	popdiv=document.getElementById(selectedDiv)
	//popdiv.style.visibility="hidden";
	popdiv.style.display = "none";
}

function GetParameter( name ){  
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
    var regexS = "[\\?&]"+name+"=([^&#]*)";  
    var regex = new RegExp( regexS );  
    var results = regex.exec( window.location.href );  
    if( results == null )    
        return "";  
    else    
        return results[1];
}

function CollapseAllMenu()
{
    searchMenu.collapseAll();
    winelistMenu.collapseAll();
    ourfavouritesMenu.collapseAll();
    finewinesMenu.collapseAll();
}

function toggle() {
	var ele = document.getElementById("toggleText");
	var text = document.getElementById("ctl00_middle_content_displayText");
	if (ele != null && text != null)
	{
	    //if (ele.style.display == "block") {
	    if (ele.style.visibility == "visible") {
            //ele.style.display = "none";
            ele.style.visibility = "hidden";
            ele.style.overflow = "hidden";
            ele.style.height = "0px";
		    text.innerHTML = "expand";
		    SetCookie('ShowAdvancedSearch','collapse');
  	    }
	    else {
		    //ele.style.display = "block";
		    ele.style.visibility = "visible";
		    ele.style.overflow = "visible";
		    ele.style.height = "245px";
		    text.innerHTML = "collapse";
		    SetCookie('ShowAdvancedSearch','expand');
	    }
	}
} 

function ShowEmail(user, domain, anchor) {
	var OutText = '';
	OutText += '<a href="mailto:' + user + '@' + domain + '">';
	if (anchor != '') OutText += anchor;
	else OutText += user + '@' + domain;
	OutText  += '</a>';
	document.write(OutText);
}

function IncreaseQty()
{
    var tbQty = document.getElementById("ctl00_middle_content_TxBxQty");
    if (tbQty != null)
        tbQty.value ++;
    
    return tbQty.value;
}

//function DecreaseQty(rowIndex)
function DecreaseQty()
{
    /*var txtQty = document.getElementsByName("TxtQty");
alert(txtQty[rowIndex]);
    if (txtQty != null) {
        if (txtQty[rowIndex].value > 1) 
            txtQty[rowIndex].value =  txtQty[rowIndex].value - 1;
        return txtQty.value;
    }    
    return "";    */

    var tbQty = document.getElementById("ctl00_middle_content_TxBxQty");
    if (tbQty != null) {
        if (tbQty.value > 1)
            tbQty.value --;
            
        return tbQty.value;
    }
    
    return "";
}

function SetCookie(name,value)
{
    cookiestring=name+"="+escape(value)+";";
    window.parent.document.cookie=cookiestring;
    if(!GetCookie(name)) return false;
    return true;
}

function GetCookie(cookiename) {
    var cookiestring=""+window.parent.document.cookie;
    var index1=cookiestring.indexOf(cookiename);
    if (index1==-1 || cookiename=="") return ""; 
    var index2=cookiestring.indexOf(';',index1);
    if (index2==-1) index2=cookiestring.length; 
    return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function DeleteCookie(name){
    var days = 1;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000)); 
        var expires = "; expires="+date.toGMTString();
    }
    else expires = "";
        
    window.parent.document.cookie = name+"="+expires+"; path=/";
}



/************************************ TextBox onfocus/onblur ************************************/
function QuickSearchBtnClick(e, buttonid){ 
    var bt = document.getElementById(buttonid); 
    if (typeof bt == 'object'){ 
        if(navigator.appName.indexOf("Netscape")>(-1)){ 
              if (e.keyCode == 13){ 
                    bt.click(); 
                    return false; 
              } 
        } 
        if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1)){ 
              if (event.keyCode == 13){ 
                    bt.click(); 
                    return false; 
              } 
        } 
    } 
} 

function TextBoxOnFocus(txt) {
    if ( txt.value == 'quick search' || txt.value == 'your email' )
        txt.value = '';
}

function TextBoxOnBlur(txt, obj) {
    //if ( txt.value.Trim() == '' )
    if ( txt.value == '' ) {
        if (obj == 'search')
            txt.value = 'quick search';
        else if (obj == 'subscribe')
            txt.value = 'your email';
    }
}

function InitSearchTextBox() {
    var txt = document.getElementById('ctl00_TxBxQuickSearch');
    txt.onfocus = function() { TextBoxOnFocus(txt); };
    txt.onblur = function() { TextBoxOnBlur(txt, 'search'); };
}

function InitSubscribeTextBox() {
    var txt = document.getElementById('ctl00_TxtBoxEmail');
    txt.onfocus = function() { TextBoxOnFocus(txt); };
    txt.onblur = function() { TextBoxOnBlur(txt, 'subscribe'); };
}
