var myActualCookie = ""

function readCookie(cookieName) {
   //
	 // read in cookie
	 //  
   var ca = document.cookie.split(';');
   var nameEQ = cookieName + "=";
   for(var i=0; i < ca.length; i++) {
    	var c = ca[i];
      while (c.charAt(0)==' ') 
			    c = c.substring(1, c.length); //delete spaces
      if (c.indexOf(nameEQ) == 0) { 
					myActualCookie = c.substring(nameEQ.length, c.length);
					myActualCookie = myActualCookie.replace(/%2A/g, "*");
					myActualCookie = myActualCookie.replace(/%2C/g, ",");
					myActualCookie = myActualCookie.replace(/%28/g, "(");
					myActualCookie = myActualCookie.replace(/%29/g, ")");
			}
   }
}

function readDirectory() {
   //
	 // get directory
	 //  
   var cookie = myActualCookie.split("*")	 
	 if (cookie.length > 0)
   		return cookie[0]
	 else 
	    return null;		
}

function readInternetID() {
   //
	 // get internetid
	 //  
   var cookie = myActualCookie.split("*")	 
	 if (cookie.length > 1)
   		return cookie[1].substring(1,cookie[1].length)
	 else 
	    return "";		
}

function readOldCookie() {
   //
	 // get old cookie format 
	 //  
   var cookie = myActualCookie.split("*")	 
	 if (cookie.length > 1)
   		return cookie[2].substring(1,cookie[2].length)
	 else 
	    return "";		
}


function checkCookieExist() {
   //
	 // check if there's cookie available, if yes, check it out and open appropriate page
	 //  

   var temp = window.location+"";
	 var tempArray = temp.split("/");
	 var host = tempArray[2].split(".");
	 var mytarget = "_blank";

	 if (host[0] == "job")
	     mytarget = "_self";
	 else if (tempArray[3] != "login.html") {
	     window.open("main.html","_self");
	     return
	}		 
			 	 
 	 readCookie("BookingDetails");
	 var MyInternetID = readInternetID()
	 var MyOldCookie = readOldCookie()
	 var MyDirectory = readDirectory()

	 if (MyOldCookie != "") {
	   if (MyDirectory == "") {
		     window.location.href = "../deletecookie/index.php";
		     return
		 }   
	   // old cookie exists
		 window.location.href = "../Validation.php?CookieType=Old&MyInternetID="+MyDirectory
	 } else if (MyDirectory != "") { 	 
	   if (MyInternetID == "") {
		     window.location.href = "../deletecookie/index.php";
		     return
		 }   
		 
		 if (document.getElementById("Login")) {
         document.getElementById("rightsidenav").innerHTML = "We are now creating a new window with your booking information. Please wait."+
				 																									   "<br><br>If the new window could not popup, look at the top of your window, "+
																														 "right click at the yellow bar, and click on 'temporary allow scripting' "

     }
	   // new cookie exists
		 window.open("../Validation.php?CookieType=New&MyDirectory="+MyDirectory+"&MyInternetID="+MyInternetID, mytarget);
	 } 		 	 
	 
}



function checkCookie() {
	readCookie("BookingDetails");
	var MyInternetID = readInternetID()
	var MyOldCookie = readOldCookie()
	var MyDirectory = readDirectory()
	if (MyOldCookie != "") {
	   // old cookie exists
		 window.location.href = "../Validation.php?CookieType=Old&MyInternetID="+MyDirectory
	} else if (MyDirectory != "") { 	 
	   // new cookie exists
		 window.location.href = "../Validation.php?CookieType=New&MyDirectory="+MyDirectory+"&MyInternetID="+MyInternetID
	}				

}