var qStr = document.location.search;

function init() 
{
	// this all has to initialize here to assure that it works with NS3.x
	
	// If qStr is not null and contains a "gealertk=",
	// then set form.k to that and add to cookie
	if( (qStr) && (qStr.indexOf('gealertk=') != -1) ){
		var qSubStr = qStr.substring( (qStr.indexOf('gealertk=') + 9), qStr.length);
		document.ContactForm.k.value = qSubStr;
		document.cookie = "gealertk=" + qSubStr;
	}
	else {
		// set form.k to cookie value. Could by a number if found or empty string if not.
		document.ContactForm.k.value = read_cookie('gealertk', 0);
	}
}


function read_cookie(key, skips)
{
        // Set skips to 0 if parameter was omitted:
        if (skips == null)
                skips = 0;

        // Get cookie string and separate into individual cookie phrases:
        var cookie_string = "" + document.cookie;
        var cookie_array = cookie_string.split ("; ");

        // Scan for desired cookie:
        for (var i = 0; i < cookie_array.length; ++i)
        {
                var single_cookie = cookie_array[i].split("=");
                if (single_cookie.length != 2)
                        continue;
                var name  = unescape (single_cookie[0]);
                var value = unescape (single_cookie[1]);

                // Return cookie if found:
                if (key == name && skips -- == 0)
                        return value;
        }

        // Cookie was not found:
        return "";
}


function valFrmFlds() 
{
	var incomplete = '';
	
	with (document.ContactForm) {
		if(  ( (!qStr) || (qStr.indexOf('gealertk=') == -1) )  &&  (!read_cookie('gealertk', 0))  ) {
			// check that a field is not empty after first trimming user input
			if (!trimStr(fname.value).length) incomplete = incomplete+"\n"+"- First Name";
			// check that a field is not empty after first trimming user input
			if (!trimStr(lname.value).length) incomplete = incomplete+"\n"+"- Last Name";
			// check that a field is not empty after first trimming user input
			if (!trimStr(emailAdd.value).length) incomplete = incomplete+"\n"+"- E-Mail Address";
			// check that a field is not empty after first trimming user input
			if (!trimStr(zip.value).length) incomplete = incomplete+"\n"+"- Postal Code";
		}
		
		if(document.location.pathname.indexOf('action.htm') > 0) {
			if (howHearAboutUs.selectedIndex == 0) incomplete = incomplete+"\n"+"- Please select an option for how you heard about us";
		}
		
		if(document.location.pathname.indexOf('invite.htm') > 0) {
			if (!trimStr(invitationTxt.value).length) incomplete = incomplete+"\n"+"- The Invitation Text";
			if (!trimStr(inviteFname.value).length) incomplete = incomplete+"\n"+"- Friend's First Name";
			if (!trimStr(inviteLname.value).length) incomplete = incomplete+"\n"+"- Friend's First Name";
			if (!trimStr(inviteEmailAdd.value).length) incomplete = incomplete+"\n"+"- Friend's E-mail Address";
		}			

	}
	
	if (!incomplete.length)
	{
		return true
	}
	else
	{
		alert('You must complete data for the following:\n' + incomplete) 
		return false 
	}
}

// this is the trim function.
function trimStr(userInput) {
     var cChar = '';
     var iEnd = (userInput.length - 1);
     var iStart = 0;

     cChar = userInput.charAt(iStart);
     while ((iStart < iEnd) && ((cChar == "\n") || (cChar == "\r") ||
                               (cChar == "\t") || (cChar == " "))){
        iStart ++;
        cChar = userInput.charAt(iStart);
     }

     cChar = userInput.charAt(iEnd);
     while ((iEnd >= 0) && ((cChar == "\n") || (cChar == "\r") ||
                           (cChar == "\t") || (cChar == " "))){
        iEnd --;
        cChar = userInput.charAt(iEnd);
     }

     if (iStart <= iEnd){
        return userInput.substring(iStart, iEnd + 1);
     } else {
        return "";
     }
}