// define global variables
var salesPersonName;

// this function will determine which contact page to display
function chooseContactPage() {
	// determine if salesperson cookie exists
	if (GetCookie('salesPerson')) {
		// salesperson cookie does exist so read name of salesperson
		salesPersonName = (GetCookie('salesPerson'));
//		alert('salesperson cookie does exist and is ' + salesPersonName);

	} else {
//			alert('salesperson cookie does not exist and is set to generic');
		// salesperson cookie does not exist so store generic to salesperson
		salesPersonName = 'Generic';
	}

//	alert('salesPersonName is ' + salesPersonName);

	// send user to salesperson's contact page
	switch (salesPersonName) { 
    case "BeverlyLarsen" : 
		window.location.href="http://beverlylarsen.olotr.com/index.html";
    break; 

    case "DianaPier" : 
		window.location.href="http://dianapier.olotr.com/index.html";
    break; 

    case "BeaWills" : 
		window.location.href="http://beawills.olotr.com/index.html";
    break; 

    case "AlmaPuente" : 
		window.location.href="http://almapuente.olotr.com/index.html";
    break; 

    case "Generic" : 
		// no salesperson so send to generic contact page
		window.location.href="http://www.olotr.com/contact/index.html";
    break; 
	}
}

// this function will write the salesperson cookie.  cookie can be read from any directory
function writeSalesPersonCookie(name) {
	alert('write called');
	var expdate = new Date ();
	// cookie expires 1 day from now
    expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 1)); 
	SetCookie('salesPerson', name, null, "/");
}

// Cookie Functions -- "Toss Your Cookies" Version (22-Mar-96) 
//
// Written by: Bill Dortch, hIdaho Design
// The following functions are released to the public domain.
//
// The fix code for DeleteCookie recalibrates the date prior to 
// deleting the cookie.
//
// ***** HERE IS THE FIX FUNCTION. DO NOT DELETE IT!!! ***** 

function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); 
// dawn of (Unix) time - should be 0 if (skew > 0) 
// Except on the Mac - ahead of its time 
	date.setTime (date.getTime() - skew);
}

// ***** END OF FIX FUNCTION *****

// The first two parameters are required. The others, if supplied, must 
// be passed in the order listed above. To omit an unused optional field, 
// use null as a place holder. For example, to call SetCookie using name, 
// value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null; var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null; var secure = (argc > 5) ? argv[5] : false;
    if      (expires!=null) FixCookieDate(expires);
    document.cookie = name + "=" + escape (value) + 
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : (";  path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

// "Internal" function to return the decoded value of a cookie 

function getCookieVal (offset) {
    var endstr = document.cookie.indexOf (";", offset); if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

// Function to return the value of the cookie specified by "name". 
// name - String object containing the cookie name. 
// returns - String object containing the cookie value, or null if 
//      the cookie does not exist.

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;
}

// this is a function used to display the salesperson cookie if it exists
// this function is only used for testing
function showCookie() {
	if (GetCookie('salesPerson')) {
		// salesperson cookie does exist so read name of salesperson
		salesPersonName = (GetCookie('salesPerson'));
		alert('salesperson cookie does exist and is ' + salesPersonName);
	} else {
		alert('salesperson cookie does not exist');
	}

}

// this function will write the salesperson cookie.  cookie can be read from any directory
function writeSawAd() {
	var expdate = new Date ();
	// cookie expires 1 day from now
    expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 1)); 
	SetCookie('sawAd', 'sawAd', null, "/");
}

