// functions used by contents area on the popup windows.
//overriding function, if in case target is not presented on the content.
function GotoLink(mlink)
{
	Link(mlink,'');
}

// mlink - hyperlink
// target - target window
function Link(mlink,target)
{
	if (target == "self")
	{
		self.focus();
		self.location.href = mlink;
	}
	else
	{
		// If parent is not closed, display the doc on the parent
		// If parent is closed, open the doc on a new window.
		if (opener == null)
			window.open(mlink);
		else
		{
			if (!opener.closed)
			{
				//opener.P_showPop1 = false; //Disable exit popup
				opener.focus();
				opener.location.href = mlink;
			}
			else
			{
				window.open(mlink);
			}
		}
	}
}


function getNewUrl(newurl,infld,invalue)
{
	
	var newlocation = "";
	var updated = false;
	var qstr = newurl.indexOf("?");
	
	if (qstr < 0)
		return newurl;
	
		newlocation = newurl.substring(0,qstr + 1);
		qstr = newurl.substring(qstr + 1);
	
		var pairs=qstr.split("&");
		var mamp = "&";
	
		for (var i=0; i<pairs.length; i++)
		{
			
			if (i == (pairs.length-1))
				mamp = '';
				
			var pos = pairs[i].indexOf("=");
			if (pos == -1) continue;
			
			var argname = pairs[i].substring(0,pos);
			var argval = pairs[i].substring(pos+1);
			
			
			if (argname.toLowerCase() == infld)
			{ 
				updated=true;
				if (invalue.indexOf(argval) < 0)
				{ // append new origin
					newlocation = newlocation + argname + "=" + argval + "-" + invalue + mamp;
				}
				else
				{ // as is incoming
					newlocation = newlocation + argname + "=" + invalue + mamp;	
				}
			}	
			else
				newlocation = newlocation + argname + "=" + argval + mamp;
		}
	
	
	return (newlocation);

}

function passOrigin(url)
{
	var val = document.o_form.o_origin.value;
		
	if	(!navigator.javaEnabled() 
		|| val == "" 
		|| val.charAt(0) != "!"
		)
		location.href = url;
	else
		location.href = getNewUrl(url,"o",val);
	
}


function getQValue(newurl,infld)
{	
	
	var qstr = newurl.indexOf("?");
	var returnstr = "";
	
	if (qstr < 0)
		return returnstr;
	
	
	qstr = newurl.substring(qstr + 1);
	var pairs=qstr.split("&");
	var mamp = "&";
	
	for (var i=0; i<pairs.length; i++)
	{
			
		if (i == (pairs.length-1))
			mamp = '';
				
		var pos = pairs[i].indexOf("=");
		if (pos == -1) continue;
			
		var argname = pairs[i].substring(0,pos);
		var argval = pairs[i].substring(pos+1);
					
		if (argname.toLowerCase() == infld)
		{ 
				returnstr = argval;
				break;
		}
	}
	
	return returnstr;
}


function CheckCookie(sCookieName)
{
	var sClientCookie = unescape(document.cookie);
	if(sClientCookie != null && sClientCookie.length>0) 
		return sClientCookie.indexOf(sCookieName + "=");
	else
		return -1;
}

// Function that gets a cookie's value by searching for the name of the cookie
function readCookieValue(name)
{     
   // Declare variable to set the "name=" value
   var start = document.cookie.indexOf(name + "=");
   
   if (start < 0) return "";
   
   // Get the first character of the cookie
   start = document.cookie.indexOf("=", start) + 1;
     
   var end;   
   
   // If there are more cookies appended
   if(document.cookie.indexOf("&", start) > -1)
   {
      end = document.cookie.indexOf("&", start);
   }
   else
   {
      end = document.cookie.indexOf(";", start);
   }
   
   //Cooke is at end of cookies string
   if(end == -1)
   {
      end = document.cookie.length;
   }

   // Get the cookie value, reversing the escaped format by using the unescape method 
   var value = unescape(document.cookie.substring(start, end));
         
   if(value == null)
   {
      return value;
   }
   else
   {
      //Return existing cookie value
      return value;
   }   
}

function StoreOrigin()
{
	var o_val =  getQValue(document.location.href,"o");
	
	if (o_val.length > 0)
	{	
		o_val = unescape(o_val); // in case escaped, unescape and reescape
      document.cookie = fAddToCollection('sitesession','o',o_val);		
	}
}

function OverrideOrigin(val)
{	
	if (val != null && val.length > 0)
	{	
		var o_val = unescape(val); // in case escaped, unescape and reescape
      document.cookie = fAddToCollection('sitesession','o',o_val);
	}
}

//Add to cookie collection with an existing key value - works with 2 keys only 
function fAddToCollection(sCookieCollection, sCookieName, sCookieValue)
{ 

   var sCookiePathDomain = ";path=" + scPath;
   if (scDomain !="") sCookiePathDomain += (";domain=" + scDomain + ";");
   
   //Get existing cookie collection values
   sValue = fReadCookieValue(sCookieCollection);
   
   if (sValue != "")
   {
      var sStartDelimeter;		   
      
      //If there is only one Key start at 0 -- For now there will only be one key ie. lb_popup_shown or ts_popup_shown
      if(sValue.indexOf("&") < 0)
      {
         sStartDelimeter = 0;
      }
      else // reserved for future use if more keys are added to this collection
      {
         //sStartDelimeter = sValue.indexOf("&") + 1;  
	 sStartDelimeter = sValue.indexOf(sCookieName+"=");
      }
   		   
      var sEndDelimeter;
      sEndDelimeter = sValue.indexOf("=",sStartDelimeter);
   		   
      //Seperate the existing cookie key from the collection
      var sKey;
      sKey = sValue.substring(sStartDelimeter, sEndDelimeter);
   }   
	       
   if(sKey == sCookieName)
   {
      //Overwrite existing cookie key value to collection
	  // first, get the total existing cookie value
	  if (sValue.indexOf("&",sEndDelimeter) > -1)
	      {
		sEndDelimeter = sValue.indexOf("&",sEndDelimeter);
	      }
	  else if (sValue.indexOf(";",sEndDelimeter) > -1)
	      {
		sEndDelimeter = sValue.indexOf(";",sEndDelimeter);
	      }
	  else 
	      {
		sEndDelimeter = sValue.Length;
	      }

	  sValue = sCookieCollection + "=" + sValue.replace(sValue.substring(sStartDelimeter, sEndDelimeter), sCookieName + "=" + sCookieValue) + sCookiePathDomain;
   }
   else
   {
     if (sValue != "")
     {
      //Add new cookie value to collection
	  sValue = sCookieCollection + "=" + sValue + "&" + sCookieName + "=" + sCookieValue + sCookiePathDomain;
	  }
	  else
	  {
	  sValue = sCookieCollection + "=" + sCookieName + "=" + sCookieValue + sCookiePathDomain;
	  }
   }

   return sValue;
}

// Function that gets a cookie's value by searching for the name of the cookie
function fReadCookieValue(name)
{     
   // Declare variable to set the "name=" value
   var start = document.cookie.indexOf(name + "=");
   
   if (start < 0) return "";
   
   // Get the first character of the cookie
   start = document.cookie.indexOf("=", start) + 1;
     
   var end = document.cookie.indexOf(";", start);
   
   //Cooke is at end of cookies string
   if(end == -1)
   {
      end = document.cookie.length;
   }

   // Get the cookie value, reversing the escaped format by using the unescape method 
   var value = unescape(document.cookie.substring(start, end));
         
   if(value == null)
   {
      return value;
   }
   else
   {
      //Return existing cookie value
      return value;
   }   
}

function fGetCookie(sCookie, sCookieName)
{
	 //Get existing cookie collection values
   var sValue = fReadCookieValue(sCookie);
   
   var sKeyValue = "";
   if (sValue != "")
   {
      var sStartDelimeter;		   
      
      //If there is only one Key start at 0 -- For now there will only be one key ie. lb_popup_shown or ts_popup_shown
      if(sValue.indexOf("&") < 0)
      {
         sStartDelimeter = 0;
      }
      else // reserved for future use if more keys are added to this collection
      {
         //sStartDelimeter = sValue.indexOf("&") + 1;  
		sStartDelimeter = sValue.indexOf(sCookieName+"=");
		sStartDelimeter = sStartDelimeter + sCookieName.length + 1;
      }
   		   
      var sEndDelimeter;
      if (sValue.indexOf("&",sStartDelimeter) > -1)
		sEndDelimeter = sValue.indexOf("&",sStartDelimeter);
   	  else
   		sEndDelimeter = sValue.length;
   			   
      //Seperate the existing cookie key from the collection
      sKeyValue = sValue.substring(sStartDelimeter, sEndDelimeter);
   }   
	     
   return sKeyValue;
}

function openValidateZip(sHomePath)
{
	var url = sHomePath+"/cnt/prod/wine/ValidateZip.aspx";
	window.open(url, "validateZip", "menubar=no,status=no,location=no,width=420,height=200,toolbar=no,scrollbars=no,resizable=yes,left=100,top=100");
}

//StoreOrigin();

//this function allows to add a new function to the
//windows.onload event without overwritting what it is there.
function HsnAddLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      if (oldonload) 
      {
        oldonload();
      }
      func();
    }
  }
}

//this function allows to add a new function to the
//windows.onresize event without overwritting what it is there.
function HsnAddResizeEvent(func) 
{
  var oldonresize = window.onresize;
  if (typeof window.onresize != 'function') 
  {
    window.onresize = func;
  } 
  else 
  {
    window.onresize = function() 
    {
      if (oldonresize) 
      {
        oldonresize();
      }
      func();
    }
  }
}

function ShowProcessingImage(overrideflag)
{
  
   if(overrideflag || (ProcAuth=='True')) 
   {
        document.getElementById('Processings').style.visibility="hidden";
        document.getElementById('ProcessingImage').innerHTML="<p/><DIV style='font-size:20;margin-left:20px;margin-top:70px' class='hdr8' align=left>Order Status</DIV><br/><img width='350px' height='80px' id='ProcessingImage' src='" + Procimg.src + "' />";
        window.scrollTo(0,0);
   }
   return true;
}

