// put all functions at the top
// Create Key/Val pair object
function httpVals(key,val) {
  this.key = key;
  this.val = val;
}

function ReadHTTPParamURL() {
	httpParameterURL = "";
	// Create variable is_input to see if there is a ? in the url
	var is_input = document.URL.indexOf('?');

	// Check the position of the ? in the url
	if (is_input != -1)
	{ 
		// Create variable from ? in the url to the end of the string
		httpParameterURL = document.URL.substring(is_input+1, document.URL.length);
	}
	return httpParameterURL;
}

function ReadHTTPParams() {
	httpParameters = new Array();

	// Create variable is_input to see if there is a ? in the url
	var is_input = document.URL.indexOf('?');

	// Check the position of the ? in the url
	if (is_input != -1)
	{ 
		// Create variable from ? in the url to the end of the string
		addr_str = document.URL.substring(is_input+1, document.URL.length);
	
		start_http_index = 0;
		end_http_index = addr_str.indexOf("&");
		do_loop = 1;	
		count = 0;

		while (do_loop == 1) 
		{	
			if (end_http_index == -1) 
			{
				do_loop = 0;
				http_var_str = addr_str.substring(start_http_index);
				httpParameters[count] = new httpVals(http_var_str.substring(start_http_index,http_var_str.indexOf("=")),http_var_str.substring(http_var_str.indexOf("=")+1));
			} else {
				http_var_str = addr_str.substring(start_http_index,end_http_index);
				httpParameters[count] = new httpVals(http_var_str.substring(start_http_index,http_var_str.indexOf("=")),http_var_str.substring(http_var_str.indexOf("=")+1));
				addr_str = addr_str.substring(end_http_index+1);			
			}
	
			start_http_index = 0;
			end_http_index = addr_str.indexOf("&");
	
			count++;
					
		}
	}
	return 	httpParameters;
}



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 correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
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);
}


//
//  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;
}


//
//  Function to return the key/value pairss of the cookie specified by "zmail".
//    returns - String object containing the cookies value, or null if
//      the cookie does not exist.
//
function GetZMailCookies () {
  var retVal = "";
  var retKeys = "zmailkeys=";
  var arg = "zmail";
  var alen = 5;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) 
    {
	var k = document.cookie.indexOf("=",i);
	retVal = retVal + document.cookie.substring(i, k)+getCookieVal (k)+"&";
	retKeys = retKeys + document.cookie.substring(i, k)+":";
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  if (retVal.length > 0) {
	return "&"+retVal.substring(0,retVal.length) + retKeys.substring(0,retKeys.length-1);
  } else {
 	return retVal;
  }

  

}

//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  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, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


// functions transferred from code
function autoTab(input,len) {
	if (input.value.length >= len) {
		input.value = input.value.slice(0,len);
		var next = input.form[(getIndex(input)+1) % input.form.length];
		next.focus();
	}
	return true;								
}
function getIndex(input){
	var index = 1;
	var form = input.form;
	for (var i=0; i <form.length; i++) {
		if (form.elements[i]==input){
			index = i;
			break;
		}
	}
	return index;
}
	
function selectAll(thearray) {
	for (var i=2; i < thearray.length;i++) {
		n=thearray[0].elements.length;
		var j=0;
		for(j=0;j<n;j++){
			if(thearray[0].elements[j].name==thearray[i]){
				thearray[0].elements[j].checked=thearray[1].checked;
			}
		}
	}
}



// Start Processing Here
if (zmailNextPage.indexOf("http") == 0) 
{

	var zmailURL = "";
	
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now

	var mylang = "en";
        if (window.lang === undefined)  {
		//do nothing
	} else {
		if (lang != "") {
			mylang = lang;
		}
	}
	httpParameters = ReadHTTPParams();
	if (httpParameters.length > 0) {
		<!---zmailURL = "http://zmaildirect.com/app/"+zmailApp+"?nextPage="+zmailNextPage+"&thisPage="+document.URL.substring(0, document.URL.indexOf('?'))+"&js=1"+"&lang="+mylang+"&"+ReadHTTPParamURL ()+GetZMailCookies();--->
		zmailURL = "http://www.mbgolf.com/javascripts/zmail-form.js"
	
		for (var i = 0; i < httpParameters.length; i++) {
			if (httpParameters[i].key.indexOf("write") > -1) {
				SetCookie ("zmail"+httpParameters[i].key.substring(5),httpParameters[i].val, expdate);
			}
		}	
		//document.write ("<h2>"+zmailURL+"</h2><br>");
	} else {
		zmailURL = "http://www.mbgolf.com/javascripts/zmail-form.js"
		//document.write ("<h2>"+zmailURL+"</h2><br>");
	}
	
	
	document.write ("<script language=\"JavaScript\" src=\""+zmailURL+"\"></script>");
} else {

	document.write ("Please specify this the page you would like to go to next starting with \"http\"");	
}

var Br_netscape = (document.layers) ? 1:0; 
var Br_ie = (document.all) ? 1:0; 
var Br_netscape6 = (document.getElementById) ? 1:0; 
if (Br_ie) {alertObj = eval('document.all.alertBox')}
else if (Br_netscape) {alertObj = eval('document.ids.alertBox')}
else if (Br_netscape6) {alertObj = document.getElementById('alertBox')}
if (alertObj != null) {alert(alertObj.value.replace(/<br>/g,"\n"));}


