/**
 * Author: C F MEYER
 * Virtuosa
 * FOR: Saidwot.com
 * Script enables redirects depending on cookie and page href
 */

function layOver() {
	//alert(getBaseURL())
	//alert(document.location.pathname)
	var thediv=document.getElementById('displaybox');
	if(thediv.style.display == "none"){
		thediv.style.display = "";
		thediv.innerHTML = "<table width='100%' height='100%'><tr><td align='center' valign='middle' width='100%' height='100%'><div id='solidbox' ><table height='200' class='inside' ><tr><td  align='center' valign='middle'><h1>Welcome to</h1><div class='inside'><img src='http://www.saidwot.com/wp-content/uploads/layOverLogo.png' height='60' width='155' border='0'/><br /><h2>Please select your<br>country of residence.</h2><br /><a href='#' onclick='moveRSA();'><img src='http://www.saidwot.com/wp-content/uploads/sa.png' width='110' height='66' border='0'/></a>&nbsp;&nbsp;&nbsp;<a href='#' class='inside' onClick='moveUSA();'> <img src='http://www.saidwot.com/wp-content/uploads/usa.png' width='110' height='66' border='0'/></a>&nbsp;<br />&nbsp;</td></tr></table></div></td></tr></table>";
	}else{
		
		thediv.style.display = "none";
		thediv.innerHTML = '';
		
	}
	return false;
}	

function moveUSA() {
	
	var country="USA";
	
	setCookie('country','USA',365);
	
	if (document.location.href.indexOf("usa")!=-1) // usa in href
		{
		return layOver();
		}
	
	else 
	{
		if(document.location.href.indexOf("contact-us")!=-1) // no contact us found
			{
			window.location='contact-us-usa';
			
			}
		else
			{
			window.location='saidwot-services-usa';
			}
	}
}	
	
	
function moveRSA() 
{
	var country="RSA";
	
	setCookie('country','RSA',365);
	
	
	//alert(document.location.href.indexOf("usa"))
	if (document.location.href.indexOf("usa")==-1) //no usa in href
		{
	       return layOver();
		}
	
	else 
	{
		
		if(document.location.href.indexOf("contact-us")!=-1) // contact us found
			{
			window.location='../contact-us';
			
			}
		else
			{
			window.location='saidwot-packages';
			}
	}
}	
	

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");

for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    alert(y)
    }
  }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString())+ ";path=/;";
document.cookie=c_name + "=" + c_value; 
}
	

function checkCookie()
{

var country=getCookie("country");
//alert(country);

if (document.location.href.indexOf("monitoring")==-1){
if (document.location.href.indexOf("reporting")==-1){
if (document.location.href.indexOf("saidwot-for-agencies")==-1){
	

if (document.location.href.indexOf("contact-us")!=-1 || document.location.href.indexOf("contact-us-usa")!=-1 || document.location.href.indexOf("saidwot-services-usa")!=-1 || document.location.href.indexOf("saidwot-packages")!=-1)
  {
	
if (country!=null && country!="")
  {
//	alert(country +"; " + document.location.href.indexOf("contact-us")+"; " + document.location.href.indexOf("usa") );
	  if(country == "USA" && document.location.href.indexOf("contact-us")==-1 && document.location.href.indexOf("usa")!=-1) // no contact us found
		{
		   return layOver;
		}
	 else if (country == "USA" && document.location.href.indexOf("contact-us")!=-1 && document.location.href.indexOf("usa")!=-1)
		{
		  return layOver;
		  
		}
	 else if (country == "RSA" && document.location.href.indexOf("contact-us")==-1 && document.location.href.indexOf("usa")==-1)
		{
		  return layOver;
		 
		}
	 else if (country == "RSA" && document.location.href.indexOf("contact-us")!=-1 && document.location.href.indexOf("usa")==-1)
		{
		  return layOver;
		
		}
	  
	 else
		{
		 if (country == "USA" && document.location.href.indexOf("contact-us")!=-1 && document.location.href.indexOf("usa")==-1)//USA cookie - redirect to contact-USA
			 {
			 window.location='contact-us-usa';
			
			 }
		 else if (country == "USA" && document.location.href.indexOf("contact-us")==-1 && document.location.href.indexOf("usa")==-1)
			 {
			 
			 window.location='saidwot-services-usa';
			 }
		 else if (country == "RSA" && document.location.href.indexOf("contact-us")!=-1 && document.location.href.indexOf("usa")!=-1)
			 {
			 
			 window.location='../contact-us';
			 }
		 else if (country == "RSA" && document.location.href.indexOf("contact-us")==-1 && document.location.href.indexOf("usa")!=-1)
			 {
			 
			 window.location='saidwot-packages';
			 }
		}
  }
  else
  {
	 	  
	  return layOver();
  }
}
}
}
}
}

function getBaseURL() {
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}


