﻿function addEvent(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	}
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}
	else
	{ 
		return false; 
	} 
}


function searchProducts(appPath, fieldName, keyfilter)
{			
	if(keyfilter == true)
    {
         if (event.keyCode != 13 && event.which != 13)
            return;
        event.returnValue=false;
    }
	if (getElementSafe(fieldName) != null && getElementSafe(fieldName).value != "" && getElementSafe(fieldName).value != "enter keyword or item #")
	{
	
		var SearchTerm = getElementSafe(fieldName).value;
		
		// NOT NEEDED FOR NOW, BUT USEFUL WHEN WE WANT TO LINK DIRECTLT TO ITEM DETAIL PAGE																				
//		if(parseInt(SearchTerm)>0 && parseInt(SearchTerm)<99999)
//		{
//			if (document.URL.indexOf("https:") == -1)
//				document.location = "/shop/search?shortsku=" + getElementSafe(fieldName).value;	
//			else
//				document.location = appPath + "/shop/search.aspx?shortsku=" + document.forms[0].elements[fieldName].value;		
//		}
//		else
//		{
			if (document.URL.indexOf("https:") == -1)
			{
				document.location = appPath + "/shop/search.aspx?PK=" + SearchTerm;	
			}
			else
			{
				document.location = "https://www.westportbigandtall.com/shop/search.aspx?PK=" + SearchTerm;							
			}				
//		}
	}

}	



 function getElementSafe(id)
    {
        var retval;
        if(document.getElementById){
            retval = document.getElementById(id);
        }
        else if(document.all){
            retval = document.all[id];
        }
        else if(document.layers)
        {
            retval = document.layers[id];
        }
        return retval;
    }


function popUp(url, width, height, menubar, name, top, left) 
{
	var strName
	if(!top)
	{
		if(height)
			top = (screen.height / 2 - height / 2);
		else
			top = 50;
	}
	if(!left)
	{
		if(width)
			left = (screen.width / 2 - width / 2);
		else
			left = 100;
	}
	if(name) strName = name; else strName = "popup"
	if(menubar)
		window.open(url,strName,'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
	else
	{
		window.open(url,strName,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,left=' + left + ',top=' + top + ',width=' + width + ',height=' + height);
	}
}

function popUpDrilldown(url,name, width, height)
{
	if (width == null)
		width = 800;
	if (height == null)
		height = 600;
	popUp(url, width, height, false, name);
}

function subscribeEmail(email) 
{
	var bImage=new Image();
	bImage.src="";		
}

function openLogin()
{
    var popup = $find('loginPopupExtenderBehavior');
    popup.show();
}

function closeModalPopup(extendername)
{
    var popup = $find(extendername);
    
    popup.hide();
}

function validateLogin(url)
{
    WP.Web.Services.ServiceMethods.ValidateLogin(url, validateSucceededCallback);
}

function validateSucceededCallback(result)
{
    if (result == "false") // user not logged in
        openLogin();
    else
        window.location = result;
}