//      
//--------- Code for host page --------- 
var locallink=false;
var refresh_count=0;

function nopopup(){
	locallink = true;
}
function setupLinks(){
refresh_count=1;
	for (var i=0;i<document.links.length;i++){
		document.links[i].onclick = function(){ locallink = true };
	}
	//-------- Set cookie or variable for Google or other restricted sites.
}
function checkExit(){
  var ref=document.referrer;
  var refg=document.URL;
	//--------- Check for cookie if traffic is not from google or other restricted sites.
	if(!locallink){		      
		hdpopup();
	}
	else{}
}
function hdpopup()
{
	    var popunder="/Shop?dsp=100924";
	    var winWidth=1;
			var winHeight=1;
			var screenWidth=screen.width/2
			var screenHeight=screen.height/2
			var xPos=1
			var yPos=800
			var option = "width="+winWidth+",height="+winHeight+",scrollbar s=no,fullScreen=no,resizable=0,top="+yPos+",left=" +xPos+"";
	    var winfeatures="width=400,height=310,scrollbars=0,resizable=1,toolbar=0,location=1,menubar=0,status=0,directories=0";
	    if (GetCookie('hdabpopunder')==null){
		    win2=window.open(popunder,"HelloDirect",winfeatures);
		    if(win2!=null){
		    	win2.blur(); 
		    }
		  }
}
//--------- Code for pop-up--------- 
var yesContinue;
yesContinue=true;

function handleError() {
  yesContinue=false;
  return true;
}
window.onerror = null;

function direct() {
	if (window.opener && !window.opener.closed){
		yesContinue=true;
	}
	else{yesContinue=false;}

  if (yesContinue) { 
  	self.close(); 
  }
  else{
  	window.focus();
  }
}

function closewithstatus(name) {
		setCookie('hdabpopunder','yes','/fetch/');
  	if(name==null){
  		self.close(); 
  	}
  	else{
  		openMain(name);
  	}
}

function submitwithstatus(name) {
		setCookie('hdabpopunder','yes','/fetch/');

}
function loadwithstatus(name) {
		setCookie(name,'yes','/fetch/');
}
//--------- Code for cookies--------- 
function getExpDate(days, hours, minutes){
  var expDate = new Date();
  if (typeof days == "number" && typeof hours == "number" && 
  		typeof minutes == "number"){
  		expDate.setDate(expDate.getDate() + parseInt(days));
  		expDate.setHours(expDate.getHours() + parseInt(hours));
  		expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
  		return expDate.toGMTString();
  		}
}

function resetcookie(){
var expireDate = new Date()
expireDate.setMinutes(expireDate.getMinutes()-10)
document.cookie = "hdabpopunder=;path=/;expires=" + expireDate.toGMTString()
}

function setCookie(NameOfCookie, value,path) 
{
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (0.02 * 24 * 3600 * 1000));
	//document.cookie = NameOfCookie + "=" + escape(value) + "; expires=" + ExpireDate.toGMTString()
	document.cookie = NameOfCookie + "=" + escape(value) + "; expires=" + ExpireDate.toGMTString()+
	((path == null) ? "" : ("; path=" + path));
}

function setCookie(name, value, expires, path){
	document.cookie = name + "=" + encodeURI(value) + 
	((expires) ? "; expires=" +  expires : "") +
	((path) ? "; path=" + path : "");
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1){
    endstr = document.cookie.length;
   }
  return unescape(document.cookie.substring(offset, endstr));
}

//--------- Code for email address check--------- 
function CheckEmail(email) {
			AtPos = email.indexOf("@");
			StopPos = email.lastIndexOf(".");
			Message = "";
			
			if (email == "") {
			Message = "Please enter a valid Email address";
			}
			
			if (AtPos == -1 || StopPos == -1) {
			Message = "Please enter a valid Email address";
			}
			
			if (StopPos < AtPos) {
			Message = "Please enter a valid Email address";
			}
			
			if (StopPos - AtPos == 1) {
			Message = "Please enter a valid Email address";
			} 
			if(Message != ""){
				alert(Message);
				return false;
			}
			else{
				return true;
			}
		}

//------------Code to check Phone number field------------
function CheckPhoneNumber(TheNumber) {
	var valid = 1
	var GoodChars = "0123456789()-+ "
	var i = 0
	if (TheNumber=="") {
		valid = 0
	}
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
			valid = 0
		} // End if statement
	} // End for loop
	if(valid==0){
		alert('Please enter a valid Phone Number');
		return false;
	}
	else{
		return true;
	}
}


//code to open URL in main window and close popup.
function openMain(url) { 
	  if (window.opener && !window.opener.closed) { 
	   window.close();
	   window.opener.location.href = url; 
	   window.opener.focus(); 
	  } else {
	   window.open(url);	
	  }
	  return false; 
	}

var exitblockDomain=new Array(5);//list of all domains being blocked
exitblockDomain[0]="www.google.com";
exitblockDomain[1]="www.shop.com";
exitblockDomain[2]="www.search.yahoo.com";
exitblockDomain[3]="www.search.live.com";
exitblockDomain[4]="www.search.msn.com";



function hdunpopup()
{
	var showpopup=true;
	var refurl=document.referrer;
	var parsestr=refurl.split("/");
	var i=exitblockDomain.length;
	for(var j=0;j<i;j++){
		if(exitblockDomain[j]==parsestr[2]){
			showpopup=false;
		}
	}
	//do not show popup if Googlebot
	if((navigator.userAgent.toUpperCase().indexOf("GOOGLE")!=-1) ||
	   (navigator.userAgent.toUpperCase().indexOf("MSNBOT")!=-1) ||
	   (navigator.userAgent.toUpperCase().indexOf("SLURP")!=-1) ||
	   (navigator.userAgent.toUpperCase().indexOf("JEEVES")!=-1) ||
	   (navigator.userAgent.toUpperCase().indexOf("INKTOMI")!=-1) ||
	   (navigator.userAgent.toUpperCase().indexOf("YAHOO")!=-1) )  {
		showpopup=false;
	}
	
	
	if(showpopup==true){
		 if (GetCookie('hdabpopunder')==null && GetCookie('hdunpopup')==null){
			setTimeout("hdunpopupdelay()",5000);
		}
	}
}

function hdunpopupdelay()
{
	    var popunder="/Shop?";
	    var winWidth=1;
			var winHeight=1;
			var screenWidth=screen.width
			var screenHeight=screen.Height
			var xPos=1
			var yPos=800
			var option = "width="+winWidth+",height="+winHeight+",scrollbars=no,fullScreen=no,location=0,resizable=0,status=0,directories=0,top="+yPos+",left=" +xPos+"";
	    var winfeatures="width=1,height=1,scrollbars=0,resizable=0,toolbar=0,location=0,menubar=0,status=0,directories=0";
		  win3=window.open(popunder,"null",option);
		  setCookie('hdunpopup','yes','/fetch/');
		  if(win3!=null){
		  	win3.blur();
		  }
}

function getQueryStringParameter (parameterName) {
	var queryString = window.top.location.search.substring(1);
	// Add "=" to the parameter name (i.e. parameterName=value)
	var parameterName = parameterName + "=";
	if ( queryString.length > 0 ) {
		// Find the beginning of the string
		begin = queryString.indexOf ( parameterName );
		// If the parameter name is not found, skip it, otherwise return the value
		if ( begin != -1 ) {
			// Add the length (integer) to the beginning
			begin += parameterName.length;
			// Multiple parameters are separated by the "&" sign
			end = queryString.indexOf ( "&" , begin );
			if ( end == -1 ) {
				end = queryString.length
			}
			// Return the string
			return unescape ( queryString.substring ( begin, end ) );
		}	
		// Return "null" if no parameter has been found
		return "null";
	}
} 
