safedelim="[*[";

function explodeArray(item,delimiter) {
  tempArray=new Array(1);
  var Count=0;
  var tempString=new String(item);

  while (tempString.indexOf(delimiter)>0) {
    tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
    tempString=tempString.substr(tempString.indexOf(delimiter)+delimiter.length,tempString.length-tempString.indexOf(delimiter)+delimiter.length); 
    Count=Count+1
  }

  tempArray[Count]=tempString;
  return tempArray;
}

function writeCookie(myName, myVal) {

   //var myPw = "";
   var expire = new Date ();
   expire.setTime (expire.getTime() + (1 * 168 * 3600000)); //seven days from now!
//                                     (dd) (hr) (ms in hr)
  document.cookie = myName+"="+myVal+"; expires=" + expire.toGMTString();
}

function writeAllTextBoxCookies() {
	for (i=0;i<document.forms.length;i++) {
		for (j=0;j<document.forms[i].elements.length; j++) {
			if (document.forms[i].elements[j].type == "text") {
				//alert("name=" + document.forms[i].elements[j].name+", type=" + document.forms[i].elements[j].type+", value="+document.forms[i].elements[j].value);
				writeCookie(document.forms[i].elements[j].name, document.forms[i].elements[j].value) ;
			}
		}
	}
	//alert(document.cookie);
}

function updateForm() {

	if (validateUser()) {
		for (i=0;i<document.forms.length;i++) {
			for (j=0;j<document.forms[i].elements.length; j++) {
				if (document.forms[i].elements[j].type == "text") {
					myval = GetCookie(document.forms[i].elements[j].name);
					if(myval != null) {
						document.forms[i].elements[j].value = myval;
					}
				}
			}
		}
	}
	else {
		alert("You are not authorized to access this page");
		document.location.href = "login.html";
	}
}

//Set the Cookie with expire date in the past - 2 days to get it for sure.
function DelEatCookie (name) {
  var expire = new Date();
  expire.setTime (expire.getTime() - 2 * 86400001);  //-2 days ago. Stale Cookie
  document.cookie = name + "=*; expires=" + expire.toGMTString();
}

function GetCookie(name) {
	//alert(name);
	//alert(document.cookie);
	cookie_name = name + "=";
	cookie_pos = instr(document.cookie,cookie_name,0);
	if(cookie_pos > -1) {
		start_pos = cookie_pos+len(name)+1
		end_pos = instr(document.cookie,";",start_pos);
		cookie_val = document.cookie.substring(start_pos, end_pos);
		return cookie_val;
	}
	else {
		return null;
	}
}

function writeCookie(myName, myVal) {

   //var myPw = "";
   var expire = new Date ();
   expire.setTime (expire.getTime() + (1 * 168 * 3600000)); //seven days from now!
//                                     (dd) (hr) (ms in hr)
  document.cookie = myName+"="+myVal+"; expires=" + expire.toGMTString();
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function checkLogin() {
	cookie_name = "tie";
	usr = login.usr.value
	pwd = login.pwd.value
	assigned = "";
	page = "";
	//alert(usr)
	switch (usr) {
		case "fred":
			assigned = "sunita";
			page = "management_report.php"
		break;
		case "glenn":
			assigned = "rachel";
			page = "management_report.php"
		break;
		case "samer":
			assigned = "alternative";
			page = "atg_management_report.php"
		break;
			case "rotem":
			assigned = "alternative";
			page = "rotem_management_report.php"
		break;
		default:
			alert("That user name is invalid");
			DelEatCookie (cookie_name);
	}
	if(assigned > ""){
		if(pwd == assigned) {
			//write name and page to cookie
			cookie_val = usr + "&" + page
			writeCookie(cookie_name, cookie_val)
			//move to that page
			document.location.href = page
		}
		else {
			DelEatCookie (cookie_name);
			alert("That password is invalid");
		}
	}
}

function right(str, num) {
	return str.substring(str.length -num, str.length);
}

function left(str, num) {
	return str.substring(0, num);
}

function len(str) {
	return str.length;
}

function instr(strSearch, strSearchFor, startPos)
{
	SearchForLen = strSearchFor.length
	for (a=startPos; a < (strSearch.length - SearchForLen)+1; a++)
	{
	    if (strSearchFor == strSearch.substring(a, a + SearchForLen))
	    {
			return a;
	    }
	}
	return -1;
}

function validateUser() {
	//alert("validateUser");
	var myVal = GetCookie("tie");
	//alert(myVal);
	if (myVal > "") {
		var pos = instr(myVal,"&",0) + 1
		//alert(pos);
		var usr = left(myVal,pos-1)
		//alert(usr);
		var page = right(myVal, len(myVal) - pos)
		//alert(page);
		var sPath = window.location.pathname;
		//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		//alert(sPage);		
		if (sPage==page) return true;
	}
	return false;
}