//////////////////////////////////////////////////
// Copyright (c) 2004 by America Online, Inc.
// All Rights Reserved
//
// File:        constants.js
//
// Facility:    Parkview
//
// Abstract:    Javascript file containing common variables.
//
// Environment: Linux
//
// Author:      Patrick Flannery
//
// History:     $Log: constants.js,v $
// History:     Revision 1.4  2008/11/21 21:07:39  flannery
// History:     Changed cookies for coupon add to be session-based.
// History:
// History:     Revision 1.3  2008/10/17 20:33:11  flannery
// History:     Fixed an HTTP/HTTPS issue when adding a coupon after logging in,
// History:     and added the new "grayed" Please Wait message.
// History:
// History:     Revision 1.2  2008/10/16 21:48:44  flannery
// History:     Added changes to allow for a client coupon add after authentication and
// History:     to try and prevent multiple clicks and AJAX requests.
// History:
// History:     Revision 1.1  2007/10/29 17:41:52  sadasiva
// History:     added validation for free text search
// History:
// History:     Revision 1.4  2007/07/13 21:09:06  sadasiva
// History:     shopping list fixes
// History:
// History:     Revision 1.3  2007/05/14 18:42:53  sadasiva
// History:     fixes for shopping list
// History:
// History:     Revision 1.2  2007/03/09 20:57:22  flannery
// History:     Added method for setting focus on an HTML element.
// History:
// History:     Revision 1.2  2006/11/02 19:12:48  flannery
// History:     Revisions for shopping list to complete iteration 16.
// History:
// History:     Revision 1.1  2006/09/06 15:12:20  flannery
// History:     DB Refactor changes
// History:
// History:     Revision 1.4  2006/06/09 19:21:56  flannery
// History:     Added regular expression for date.
// History:
// History:     Revision 1.3  2006/05/18 18:44:28  flannery
// History:     Final changes for iteration 5.
// History:
// History:     Revision 1.2  2006/05/09 16:43:14  flannery
// History:     Iteration 4 updates
// History:
// History:     Revision 1.1  2006/04/19 14:29:44  xli
// History:
// History:     Initial version
// History:
//
////////////////////////////////////////////

    // Global AJAX obj used
    var myAjax;

    // Character Sets
    var CS_NUMERIC                  = "0123456789";
    var CS_ALPHABETIC               = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var CS_ALPHANUMERIC             = CS_NUMERIC + CS_ALPHABETIC;
    var CS_TEXTSEARCH               = CS_ALPHANUMERIC + " $./-,'";
    
    var CS_INTERNET                 = CS_ALPHANUMERIC + ".,@_-";
    var CS_USERNAME                 = CS_ALPHANUMERIC + "_-";
    var CS_NAME                     = CS_ALPHANUMERIC + "-' ,.";
    var CS_DATE                     = CS_ALPHANUMERIC + "/-., ";
    var CS_PHONE                    = CS_NUMERIC + "()- ";
    var CS_PASSWORD                 = CS_ALPHANUMERIC + "_-.~`!@#$%^&*()_+";
    var CS_NUMERIC_ACCT             = CS_NUMERIC + " -";
    var CS_NUMBERTRIM               = " -./";
    var CS_NS_EMAIL                 = CS_ALPHANUMERIC + "._-";

    // Regular Expression
    var REGEXP_DATE = /^\d{1,2}\/\d{1,2}\/\d{4}$/;

    // Client errors
    var MIN_LEN_ERR         = "min_length";
    var MAX_LEN_ERR         = "max_length";
    var EXACT_LEN_ERR       = "exact_length";
    var STRING_FORMAT_ERR   = "string_format";
    var MOD10_ERR           = "mod10_failed";
    var STRING_MATCH_ERR    = "string_mismatch";
    var EMPTY_STRING_ERR    = "empty_string";
    var INVALID_CHAR_ERR    = "invalid_character";
    var SIMPLE_PSW_ERR      = "password_too_easy";
    var EMPTY_STRING_ERR    = "empty_string";
    var INVALID_NUM         = "invalid_number";
    var CC_EXPIRED			= "creditcard_expired";
    var MIN_ZIPCODE_ERR		= "invalid_zipcode";

    var CLIENT_ERR_DELIM = ";";
    var client_error;

    //////////////////////////////////////////////////
    // handle_error()
    //
    //      - Display client error to user, re-enables to
    //        'Continue' button, and puts focus on the field
    //        that had the error
    //
    //      Input: form field, error_text
    /////////////////////////////////////////////////
    function handle_error(form_field, error_text, error_type)
    {
    		if (error_text == null)
    			error_text = "An error occurred while processing the form on this page.";
            alert(error_text);
            allowPop();
            enableLink();
            form_field.focus();
            if (form_field.type == "password" ||
                    form_field.type == "text"         ||
                    form_field.type == "textarea")
            {
                    form_field.select();
            }
            client_error += form_field.name + "=" + error_type + CLIENT_ERR_DELIM;
            return;
    }

    //***************** VALIDATION FUNCTIONS *****************//

	//////////////////////////////////////////////////
	// trim()
	//
	//	- Trims leading and trailing whitespace.
	//
	//	Input: string to trim
	//	Output: string minus leading/trailing whitespace
	/////////////////////////////////////////////////
	function trim(var_string)
	{
		var_string=var_string.replace(/^\s+/, "");  // takes care of leading spaces
        var_string=var_string.replace(/\s+$/, ""); // removes trailing spaces

		return var_string;
	}

	//////////////////////////////////////////////////
	// check_len()
	//
	//	- Checks that variable length exceeded min limit.
	//
	//	Input: string to check, length minimum
	//	Output: True/False
	/////////////////////////////////////////////////
	function check_len(var_string, minimum)
	{
		return (var_string.length > minimum)
	}

	//////////////////////////////////////////////////
	// check_exact_len()
	//
	//	- Checks if variable is exactly the size of
	//	  the limit.
	//
	//	Input: string to check, length limit
	//	Output: True/False
	/////////////////////////////////////////////////
	function check_exact_len(var_string, limit)
	{
		return (var_string.length == limit)
	}

	//////////////////////////////////////////////////
	// string_format()
	//
	//	- Checks if string matches regular expression
	//
	//	Input: string to check, regular expression
	//	Output: True/False
	/////////////////////////////////////////////////
	function string_format(var_string, reg_exp)
	{
		return reg_exp.test(var_string);
	}

    //////////////////////////////////////////////////
	// trim_body()
	//
	//	- Removes any character existing in the passed
	//	  character set
	//
	//	Input: var_string, charset
	//	Output: string with characters removed
	/////////////////////////////////////////////////
	function trim_body(var_string, charset)
	{
	    var new_string = "";
		for(i=0; i < var_string.length; i++)
		{
			var var_char = var_string.charAt(i);
			if (charset.indexOf(var_char) == -1)
			{
    			new_string += var_char;
    		}
		}
		return new_string;
	}

	//////////////////////////////////////////////////
	// string_match()
	//
	//	- Checks two strings match (case sensitive)
	//
	//	Input: string1, string2
	//	Output: True/False
	/////////////////////////////////////////////////
	function string_match(string1, string2)
	{
		return (string1 == string2);
	}

    //////////////////////////////////////////////////
	// getCookieVal()
	//
	//	- Retrieves the value of a cookie, given its name
	//
	//	Input: name of the desired cookie
	//	Output: string value of the cookie or undefined
	/////////////////////////////////////////////////
        function getCookieVal(cname) {
            var cval;
            var allCookies = document.cookie;
            var pos = allCookies.indexOf(cname);
            if (pos != -1) {
                if ((pos + cname.length == allCookies.length) || (allCookies.charAt(pos + cname.length) == ';')) {
                    return true;
                } else {  // this cookie has an "=value"
                    var start = pos + cname.length + 1;
                    var end = allCookies.indexOf(";", start);
                    if (end == -1) {end = allCookies.length;}
                    cval = allCookies.substring(start, end);
                }
            }
            return cval;
        }
        
    //////////////////////////////////////////////////
	// setCookieVal()
	//
	//	- Sets a cookie with the name-value pair provided
	//
	//	Input: name of the desired cookie, value to store, expiration
	/////////////////////////////////////////////////
    function setCookieVal(name, value, expireDays) 
    {
        var expDate=new Date();
        expDate.setDate(expDate.getDate() + expireDays);
        document.cookie = name + "=" + escape(value)+ "; expires=" + expDate.toGMTString() + "; path=/";
    }
    
            
    //////////////////////////////////////////////////
	// setSessionCookie()
	//
	//	- Sets a cookie with the name-value pair provided
	//
	//	Input: name of the desired cookie, value to store, expiration
	/////////////////////////////////////////////////
    function setSessionCookie(name, value) 
    {
        document.cookie = name + "=" + escape(value)+ "; path=/";
    }
    
    //////////////////////////////////////////////////
	// removeCookieVal()
	//
	//	- Sets a cookie expiration date to yesterday
	//
	//	Input: Name of cookie to remove
	/////////////////////////////////////////////////
    function removeCookieVal(name) 
    {
        var expDate=new Date();
        setCookieVal(name, "", -1);
    }    

    //////////////////////////////////////////////////
	// filterString()
	// - HTML encodes potantially malicious characters
	//
	//	Input: input string from user
	//	Output: string value with characters HTML-encoded
	/////////////////////////////////////////////////
    function filterString(input)
    {   
        var strInput = new String(input);
        strInput = replaceAll(strInput, "&", "&amp;");
        strInput = replaceAll(strInput,"<", "&lt;");
        strInput = replaceAll(strInput,">", "&gt;");
        strInput = replaceAll(strInput,"'", "&#39;");
        strInput = replaceAll(strInput,"\"", "&quot;");
        strInput = replaceAll(strInput,"%", "&#137;");
        return strInput;
    }
    
//    function replaceAll(stringToEdit, oldStr, newStr)
//    {
//        while(stringToEdit.indexOf(oldStr) != -1)
//        {
//            var prefix = stringToEdit.substring(0, stringToEdit.indexOf(oldStr));
//            var suffix = stringToEdit.substring((stringToEdit.indexOf(oldStr) + oldStr.length), stringToEdit.length);
//            stringToEdit = prefix + newStr + suffix;
//        }
//        return stringToEdit;
//    }

	//The replace method is rewritten since the earlier method gets into
	//infinite loop in replacing '&' there by hanging the browser
	function replaceAll(stringToEdit, oldStr, newStr)
	{
		var newstr1 = "";
		var suffix = "";
		while(stringToEdit.indexOf(oldStr) != -1)
		{
			var prefix = stringToEdit.substring(0, stringToEdit.indexOf(oldStr));
			suffix = stringToEdit.substring((stringToEdit.indexOf(oldStr) + oldStr.length), stringToEdit.length);
			stringToEdit = suffix;
			newstr1=  newstr1 + prefix + newStr;
		}
		newstr1 = newstr1 + suffix;
		if(newstr1 == "")
		{
			newstr1 = stringToEdit;
		}
		return newstr1;
	}

    
    //////////////////////////////////////////////////
	// setFocus()
	// - Takes the object passed in and sets focus on it.
	//   This is mostly used for screenreaders to indicate
	//   content has updated.
	//
	//  NOTE: Focus can only be set on a, area, button, 
	//  input, object, select, and textarea elements. If
	//  it is not one of these, we can set the tab index
	//  to -1, and it will be able to receive focus in
	//  Internet Explorer and Mozilla.  This may not work
	//  in Safari.
	//
	//	Input: object upon which to set focus
	/////////////////////////////////////////////////
    function setFocus(obj)
    {
        //Determine tag type
        var tagType = obj.nodeName;
        
        //if not any of the "focusable" tags, set tabIndex
        if (tagType != "A" &&
            tagType != "AREA" &&
            tagType != "BUTTON" &&
            tagType != "INPUT" &&
            tagType != "OBJECT" &&
            tagType != "SELECT" &&
            tagType != "TEXTAREA")
        {
            obj.tabIndex = -1;
        }
        
        obj.focus();        
    }