﻿var m_oValidators = {
    "alpha": /^[a-zA-Z\s]*$/,
    "phone": /^(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4}([\s]?(x{1}|ext[.]?)[\s]?\d{2,6})?$/,
    "alpha num plus (max 50)": /^["'\-,\.#\/\\\w\s\d\$]*$/,
    "zip": /^\d{5}([- ]?\d{4})?$/,
    "email": /^[a-zA-Z0-9\-][a-zA-Z0-9\._%\-\+]*@[\-a-zA-Z0-9]+(\.[\-a-zA-Z0-9]+)*\.([cC][oO][mM]|[eE][dD][uU]|[iI][nN][fF][oO]|[gG][oO][vV]|[iI][nN][tT]|[mM][iI][lL]|[nN][eE][tT]|[oO][rR][gG]|[bB][iI][zZ]|[nN][aA][mM][eE]|[mM][uU][sS][eE][uU][mM]|[cC][oO]{2}[pP]|[aA][eE][rR][oO]|[pP][rR][oO]|[a-zA-Z]{2})$/,
    "date": /^(10|11|12|[0]?\d)\/(30|31|(0|1|2)?\d)\/(\d{2}|\d{4})$/,
    "comments": /^[^\^]*$/
},
m_oOptions = {
    "CStatesAbbrev": [{ key: 0, value: "set state" }, { key: 1, value: "AL" }, { key: 2, value: "AK" }, { key: 3, value: "AZ" }, { key: 4, value: "AR" }, { key: 5, value: "CA" }, { key: 6, value: "CO" }, { key: 7, value: "CT" }, { key: 8, value: "DE" }, { key: 9, value: "DC" }, { key: 10, value: "FL" }, { key: 11, value: "GA" }, { key: 12, value: "HI" }, { key: 13, value: "ID" }, { key: 14, value: "IL" }, { key: 15, value: "IN" }, { key: 16, value: "IA" }, { key: 17, value: "KS" }, { key: 18, value: "KY" }, { key: 19, value: "LA" }, { key: 20, value: "ME" }, { key: 21, value: "MD" }, { key: 22, value: "MA" }, { key: 23, value: "MI" }, { key: 24, value: "MN" }, { key: 25, value: "MS" }, { key: 26, value: "MO" }, { key: 27, value: "MT" }, { key: 28, value: "NE" }, { key: 29, value: "NV" }, { key: 30, value: "NH" }, { key: 31, value: "NJ" }, { key: 32, value: "NM" }, { key: 33, value: "NY" }, { key: 34, value: "NC" }, { key: 35, value: "ND" }, { key: 36, value: "OH" }, { key: 37, value: "OK" }, { key: 38, value: "OR" }, { key: 39, value: "PA" }, { key: 40, value: "PR" }, { key: 41, value: "RI" }, { key: 42, value: "SC" }, { key: 43, value: "SD" }, { key: 44, value: "TN" }, { key: 45, value: "TX" }, { key: 46, value: "UT" }, { key: 47, value: "VT" }, { key: 48, value: "VI" }, { key: 49, value: "VA" }, { key: 50, value: "WA" }, { key: 51, value: "WV" }, { key: 52, value: "WI" }, { key: 53, value: "WY"}],
    "CFrequency": [{ key: 0, value: "Never" }, { key: 1, value: "Daily" }, { key: 2, value: "Weekly" }, { key: 3, value: "Bi-Weekly" }, { key: 4, value: "Monthly"}]
};

/* Client-side access to querystring name=value pairs
Version 1.3
28 May 2008
	
License (Simplified BSD):
http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;

    // Turn <plus> back to <space>
    // See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&'); // parse out name/value pairs separated via &

    // split out each name=value pair
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]).toLowerCase();

        var value = (pair.length == 2) ? decodeURIComponent(pair[1]) : name;

        this.params[name] = value;
    }
}

Querystring.prototype.get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
    var value = this.params[key];
    return (value != null);
}

//http:www.jibbering.com/faq/faq_notes/type_convert.html
// <0 - this first
// =0 - same
// >0 - s first

/* Global Variables */
var EQUALTO = 0, GT = 1, LT = 2;

function CSStrCompareTo(s) {
    var sThis = this.toLowerCase();
    s = s.toString().toLowerCase();
    if (sThis === s)
        return EQUALTO;
    else if (sThis < s)
        return LT;

    return GT;
}

function CSNumCompareTo(s) {
    s = s - 0;
    if (this == s)
        return EQUALTO;
    else if (this < s)
        return LT;

    return GT;
}

function CSDateCompareTo(s) {
    var dtThis = Date.parse(this);
    s = Date.parse(s)
    if (dtThis == s)
        return EQUALTO;
    else if (dtThis < s)
        return LT;

    return GT;
}

/*
false = 0
true = 1
so, 
false < true
true > false
*/
function CSBooleanCompareTo(s) {
    s = Boolean(s);
    if (this == s)
        return EQUALTO;
    else if (this < s)
        return LT;

    return GT;
}

/*
* All types share same interface and interface is assigned to different function.
*/
String.prototype.CSCompareTo = CSStrCompareTo;
Number.prototype.CSCompareTo = CSNumCompareTo;
Date.prototype.CSCompareTo = CSDateCompareTo;
Boolean.prototype.CSCompareTo = CSBooleanCompareTo;

/* Trim by myself!?! - I hate .js */
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

/*
* This utility function is used to create a consistent Year Make Model SubModel text.
* 
* Parameters:
*   year            - Item is used to create YMMMS String 
*    mk              - Item is used to create YMMMS String 
*   mdl             - Item is used to create YMMMS String 
*   dflt            - Item is used to indicate if it has YMMS created or not 
*/
function YMMSText(year, mk, mdl, smdl, dflt) {
    var sYMMS = "";
    if (year && year > 0) sYMMS = " " + year.toString();
    if (mk && mk.length > 0) sYMMS += " " + mk;
    if (mdl && mdl.length > 0) sYMMS += " " + mdl;
    if (smdl && smdl.length > 0) sYMMS += " " + smdl;
    if (sYMMS.length == 0) return dflt;
    return sYMMS.trim();
}

/*
* This utility function is used to create a consistent Year Make Model SubModel text
*   with a maximum character count.
* 
* Parameters:
*   year            - Item is used to create YMMMS String 
*    mk             - Item is used to create YMMMS String 
*   mdl             - Item is used to create YMMMS String 
*   dflt            - Item is used to indicate if it has YMMS created or not
*   maxChars        - Max length of the YMMS text
*/
function YMMSTextMax(year, mk, mdl, smdl, dflt, maxChars) {

    //Year Make Model SubModel
    var sYMMS = YMMSText(year, mk, mdl, smdl, dflt);
    if (sYMMS.length <= maxChars) return sYMMS;

    //Year Make Model
    sYMMS = YMMSText(year, mk, mdl, null, dflt);
    if (sYMMS.length <= maxChars) return sYMMS;

    //Make Model
    sYMMS = YMMSText(null, mk, mdl, null, dflt);
    if (sYMMS.length <= maxChars) return sYMMS;

    //Year Model
    sYMMS = YMMSText(year, null, mdl, null, dflt);
    if (sYMMS.length <= maxChars) return sYMMS;

    //Model
    if (mdl && mdl.length <= maxChars) return mdl;

    return dflt;
}

/*
*This utility function is used to sort and merge Arrary. 
* Parameters:
* CarList         - The List need to be sorted 
* sSortItem       - sorting will be based on this item 
* sortFlags       - All the items can be used as sSortItem 
*/
function MergeSort(CarList, sSortItem, sortFlags) {
    var left = new Array();
    var right = new Array();

    var len = CarList.length;

    if (len <= 1) return CarList;

    var middle = Math.floor(len / 2);
    for (var i = 0; i < middle; i++) left.push(CarList[i]);
    for (var i = middle; i < len; i++) right.push(CarList[i]);

    left = MergeSort(left, sSortItem, sortFlags);
    right = MergeSort(right, sSortItem, sortFlags);

    return Merge(left, right, sSortItem, sortFlags);
}

/*
*This utility function is used to combine two arrays. 
* Parameters:
* left            - left half CarList 
* right           - right half CarList 
* sSortItem       - sorting will be based on this item 
* sortFlags       - All the items can be used as sSortItem 
*/
function Merge(left, right, sSortItem, sortFlags) {
    var retval = new Array();
    while (left.length > 0 && right.length > 0) {
        for (var j = 0; j < sortFlags.length; j++) {
            if (sortFlags[j].key == sSortItem) {
                if (!sortFlags[j].value) {
                    if (left[0][sSortItem].CSCompareTo(right[0][sSortItem]) == 0 || left[0][sSortItem].CSCompareTo(right[0][sSortItem]) == 2) retval.push(left.shift());
                    else retval.push(right.shift());
                }
                else {
                    if (left[0][sSortItem].CSCompareTo(right[0][sSortItem]) == 0 || left[0][sSortItem].CSCompareTo(right[0][sSortItem]) == 1) retval.push(left.shift());
                    else retval.push(right.shift());
                }
            }
        }
    }
    if (left.length > 0) return retval.concat(left);
    if (right.length > 0) return retval.concat(right);
}

/*
*This utility function is used to make money more readable. 
* Parameters:
* sValueToBeautify     - Make Price more readable with $ sign 
*/
function Convert2PrettyString(sValueToBeautify) {

    var oRegExp = /(^[-]?\d+$)|(^[-]?\d*[\.]\d{1,2}$)/; //find if it is any integer or float(2 precision) like 0.12, .12, or 12.23
    if (!oRegExp.test(sValueToBeautify)) return "";
    sValueToBeautify = sValueToBeautify.split(".")[0];
    oRegExp = /(-?[0-9]+)([0-9]{3})/;
    while (oRegExp.test(sValueToBeautify)) {
        //replace original string with first group match,a comma, then second group match
        sValueToBeautify = sValueToBeautify.replace(oRegExp, '$1,$2');
    }
    return '$' + sValueToBeautify;
}

/*
*This utility function is used to parse moneny into normal number. 
* Parameters:
* sDollarNumb     - it probably contain $ or , 
*/
function Currency2Numb(sDollarNumb) {
    if (!sDollarNumb || sDollarNumb == "") return 0;

    var iNumber = sDollarNumb.replace(/(\$|,)/gi, '');
    //check if iNumber is integer or float
    if (!(/(^[-]?\d+$)|(^[-]?\d*[\.]\d{1,2}$)/).test(iNumber))
        iNumber = 0;
    return iNumber;
}

/*
*   This utility function creates key value pairs suitable for a querystring GET request
*
*   Parameters:
*       qs              - if set, we will use this QueryString object
*       paramName        - As a new key for new querystring
*       paramKey         - key in querystring
*       isSimple         - decide whether or not to call GetDecodedParamValue
*       preventQuotes   - Set to have the value returned with quotes around it
*/
function GetParam(paramName, paramKey, isSimple, preventQuotes) {
    return GetParam(new Querystring(), paramName, paramKey, isSimple, preventQuotes);
}

/*
*   This utility function creates key value pairs suitable for a querystring GET request
*
*   Parameters:
*       qs              - if set, we will use this QueryString object
*       paramName        - As a new key for new querystring
*       paramKey         - key in querystring
*       isSimple         - decide whether or not to call GetDecodedParamValue
*       preventQuotes   - Set to have the value returned with quotes around it
*/
function GetParam(qs, paramName, paramKey, isSimple, preventQuotes) {
    if (!qs) qs = new Querystring();
    return paramName + "=" + QSVal(qs, paramKey, isSimple, preventQuotes) + "&";
}

/*
*   This utility function is used to get the value of a QS parameter
* 
*   Parameters:
*       qs              - if set, we will use this QueryString object
*       paramKey        - key in querystring
*       isSimple        - decide whether or not to call GetDecodedParamValue
*       preventQuotes   - Set to have the value returned with quotes around it
*/
function QSVal(qs, paramKey, isSimple, preventQuotes) {
    if (!qs) qs = new Querystring();
    var sParamValue = qs.get(paramKey.toLowerCase(), null);

    if (!isSimple) {
        sParamValue = GetDecodedParamValue(sParamValue);
    }

    if (!YAHOO.lang.isNull(sParamValue) && !preventQuotes)
        sParamValue = "\"" + escape(sParamValue) + "\"";

    return sParamValue;
}

/*
*This utility function is used to decode paramValue. 
* Parameters:
* paramValue        - Value has encoded value and junk sign '+'
*/
function GetDecodedParamValue(paramValue) {
    if (YAHOO.lang.isNull(paramValue)) return paramValue;
    return unescape(paramValue.replace(/[+]/g, " "));
}

/*
*This utility function is used to encode query string.
*escape function does not properly encode the '+' and '/' character 
*these characters need to be converted manually
http://www.permadi.com/tutorial/urlEncoding/
*/
EncodeString = function(sEncoding) {

    var sEncoded = escape(sEncoding);
    if (sEncoded.indexOf('+') > -1)
        sEncoded = sEncoded.replace(/[+]/, "%2B");

    if (sEncoded.indexOf('/') > -1)
        sEncoded = sEncoded.replace(/[\/]/g, "%2F");
    return sEncoded;
}

/* 
* Decodes the URL string 
*/
DecodeURL = function(sURL) {
    return unescape(sURL).replace(/[+]/g, " ");
}

/*
* Description: Adds a last child to the element referenced by elem2OpenWthn.
*   Adds any type of html element that has a separate closing tag.
*   <See AddSameCloseElem for elements like img or br>
* 
* Returns: Reference to the new element that is good until any siblings
*   of the newly created element (i.e. more children of elem2OpenWthn) are
*   added.  TODO: Fix that 
* 
* Parameters:
*   elem2OpenWthn       - Element that we are adding a last child to - not an ID
*   eId                 - ID of the ew element, will be generated if empty or null
*   eClass              - Class to add to the new element
*   eStyle              - Style(s) to add to the new element
*/
function AddClassStyle(elem2OpenWthn, eId, eClass, eStyle) {
    var oElem = YAHOO.util.Selector.query("#" + eId, elem2OpenWthn, true);
    if (eClass && eClass.length > 0) YAHOO.util.Dom.addClass(oElem, eClass);
    if (eStyle && eStyle.length > 0) oElem.style = eStyle;
    return oElem;
}

/*
* Description: Adds a last child to the element referenced by elem2OpenWthn.
*   Adds any type of html element that has a separate closing tag.
*   <See AddSameCloseElem for elements like img or br>
* 
* Returns: Reference to the new element that is good until any siblings
*   of the newly created element (i.e. more children of elem2OpenWthn) are
*   added.  TODO: Fix that 
* 
* Parameters:
*   elem2OpenWthn       - Element that we are adding a last child to - not an ID
*   eTag                - Type of ew element we are creating, e.g. div, p, a
*   eId                 - ID of the ew element, will be generated if empty or null
*   eClass              - Class to add to the new element
*   eStyle              - Style(s) to add to the new element
*/
function AddSepCloseElem(elem2OpenWthn, eTag, eId, eClass, eStyle) {
    if (!eId || eId.length == 0) eId = YAHOO.util.Dom.generateId(null, eTag);
    elem2OpenWthn.innerHTML += "<" + eTag + " id=\"" + eId + "\"></" + eTag + ">";
    return AddClassStyle(elem2OpenWthn, eId, eClass, eStyle);
}

/*
* Description: Adds a last child to the element referenced by elem2OpenWthn.
*   Adds any type of html element that has a self-closing tag.
*   <See AddSepCloseElem for elements like div or table>
* 
* Returns: Reference to the new element that is good until any siblings
*   of the newly created element (i.e. more children of elem2OpenWthn) are
*   added.  TODO: Fix that 
* 
* Parameters:
*   elem2OpenWthn       - Element that we are adding a last child to - not an ID
*   eTag                - Type of ew element we are creating, e.g. div, p, a
*   eId                 - ID of the ew element, will be generated if empty or null
*   eClass              - Class to add to the new element
*   eStyle              - Style(s) to add to the new element
*/
function AddSameCloseElem(elem2OpenWthn, eTag, eId, eClass, eStyle) {
    if (!eId || eId.length == 0) eId = YAHOO.util.Dom.generateId(null, eTag);
    elem2OpenWthn.innerHTML += "<" + eTag + " id=\"" + eId + "\" />";
    //elem2OpenWthn.innerHTML = elem2OpenWthn.innerHTML.concat("<", eTag, " id=\"", eId, "\" />");
    return AddClassStyle(elem2OpenWthn, eId, eClass, eStyle);
}

/*
* Description: Adds a last child to the element referenced by elem2OpenWthn.
*   Adds any type of html element that has a self-closing tag.
*   <See AddSepCloseElem for elements like div or table>
* 
* Returns: Reference to the new image that is good until any siblings
*   of the newly created element (i.e. more children of elem2OpenWthn) are
*   added.  TODO: Fix that 
* 
* Parameters:
*   elem2OpenWthn       - Element that we are adding a last child to - not an ID
*   eTag                - Type of ew element we are creating, e.g. div, p, a
*   eId                 - ID of the ew element, will be generated if empty or null
*   eClass              - Class to add to the new element
*   eStyle              - Style(s) to add to the new element
*   eSrc                - URL of the image
*   eAlt                - alt text of the image
*/
function AddImg(elem2OpenWthn, eTag, eId, eClass, eStyle, eSrc, eAlt) {
    var oElem = AddSameCloseElem(elem2OpenWthn, eTag, eId, eClass, eStyle)
    oElem.src = eSrc;
    if (eAlt && eAlt.length > 0) oElem.alt = eAlt;
    return oElem;
}

/*
* Description: Assumes that in the current operating context, 
*   the keyword this is a reference to a yi-menu object.  This function
*   preprends our CSHd and appends our CSFt divs as CSS hooks
*   *if* this menu is not scrolled.  If it is, we do not.
*
*   In the case where we are scrolled, we replace the reserved CSS hook
*   classes with "XXX".
* 
* Returns: 
*   n/a
* 
* Parameters:
*   eventType   - Name of the event that triggered this callback
*   eventArgs   - Event arguments (empty in at least beforeShow and show
*/
var MenuCSHdrFtr = function(eventType, eventArgs) {
    var menuID = this.id, menuBD = null;
    try { menuBD = YAHOO.util.Dom.getElementsByClassName("bd", "div", menuID)[0]; } catch (err) { }
    if (!menuBD) return;


    //Remove CS curvy corners holder if this is scrolled
    if (YAHOO.util.Dom.hasClass(menuBD, "yui-menu-body-scrolled")) {

        try { YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.getElementsByClassName("CShd", "div", menuID)[0], "CShd", "XXX"); }
        catch (err) { alert("CShd " + err); }
        try { YAHOO.util.Dom.replaceClass(YAHOO.util.Dom.getElementsByClassName("CSft", "div", menuID)[0], "CSft", "XXX"); }
        catch (err) { alert("CSft " + err); }
        return;
    }

    //Create/insert CShd
    var oCssHook = YAHOO.util.Dom.getElementsByClassName("CShd", "div", menuID)[0];
    if (!oCssHook) oCssHook = YAHOO.util.Dom.getElementsByClassName("XXX", "div", menuID)[0];
    if (!oCssHook) {
        oDiv = document.createElement("div");
        sDivID = YAHOO.util.Dom.generateId(oDiv, "Hdr");
        oDiv.id = sDivID;
        oDiv.className = "CShd";
        YAHOO.util.Dom.insertBefore(oDiv, menuBD);
    }
    else { YAHOO.util.Dom.replaceClass(oCssHook, "XXX", "CShd"); }

    //Create/insert CSft
    oCssHook = YAHOO.util.Dom.getElementsByClassName("CSft", "div", menuID)[0];
    if (!oCssHook) oCssHook = YAHOO.util.Dom.getElementsByClassName("XXX", "div", menuID)[0];
    if (!oCssHook) {
        oDiv = document.createElement("div");
        sDivID = YAHOO.util.Dom.generateId(oDiv, "Ftr");
        oDiv.id = sDivID;
        oDiv.className = "CSft";
        YAHOO.util.Dom.insertAfter(oDiv, menuBD);
    }
    else { YAHOO.util.Dom.replaceClass(oCssHook, "XXX", "CSft"); }
}

/*
* Description: Creates a menu and subscribes to its events.
* 
* Returns: 
*   n/a
* 
* Parameters:
*   menuID   - ID of the menu that needs to be rendered
*/
function CSCreateMainNavMenu(menuID) {
    var oMenu = new YAHOO.widget.MenuBar(menuID, { autosubmenudisplay: true, keepopen: true });

    //Stay visible after clicking on the menu bar root element
    oMenu.subscribe("click", function(eventType, eventArgs) {
        var oSubMenu = eventArgs[1].cfg.getProperty("submenu");
        if (oSubMenu && !oSubMenu.cfg.getProperty("visible"))
            oSubMenu.show();
    });

    //CSS Hooks
    oMenu.subscribe("beforeShow", MenuCSHdrFtr);
    oMenu.render();
}

function CleanResponse(response) {
    response = response.replace('<?xml version="1.0" encoding="utf-8"?>', "").replace('</string>', "").replace('<string xmlns="http://www.dfadmin.com/">', "");
    if (!YAHOO.lang.JSON.isSafe(response)) return response;

    var oJSResp = YAHOO.lang.JSON.parse(response);
    if (oJSResp.d) {
        if (YAHOO.lang.JSON.isSafe(oJSResp.d))
            oJSResp = YAHOO.lang.JSON.parse(oJSResp.d);
        else
            oJSResp = oJSResp.d;
    }
    return oJSResp;
}

/* 
*  Called by the function CreateElement. Adds the options to the dropdown
* 
*  @param {object} element  - The dropdown element
*  @param {object} options  - Dropdown options
*  @param {string} dispText - Text to be selected on dropdown
*/
function AddOptions(element, options, dispText) {
    var oOption = null;
    for (iOptionIdx in options) {
        oOption = document.createElement("option");
        oOption.value = options[iOptionIdx].key;
        oOption.text = options[iOptionIdx].value;
        element.options.add(oOption);
        if (options[iOptionIdx].value == dispText) element.selectedIndex = options[iOptionIdx].key + "";
    }
}

/* 
*  Creates element with passed attributes  
* 
*  @param {string} eTag            - tag type
*  @param {string} eId             - Id for the new element
*  @param {string} dispText        - display text
*  @param {string} eValue          - value attribute (ignored if options is null)
*  @param {string} eHref           - href attribute
*  @param {string} eClass          - class name
*  @param {string} style           - style attribute
*  @param {object} options         - Item Collection for dropdown
*  @param {string} eventType       - event type
*  @param {function} hndlrFunction - Click event handler function
*  @param {object} scope           - Scope for Listener 
*  @param {string} src             - src attribute 
*  @param {string} alt             - alt attribute   
*/
function CreateElement(eTag, eId, dispText, eValue, eHref, eClass, style, options, eventType, hndlrFunction, scope, src, alt) {
    var oEle = document.createElement(eTag);
    if (eId) {
        YAHOO.util.Dom.setAttribute(oEle, "id", eId);
        YAHOO.util.Dom.setAttribute(oEle, "name", eId); //Support for IE6
    }
    if (dispText) oEle.innerHTML = dispText;
    if (eValue) YAHOO.util.Dom.setAttribute(oEle, "value", eValue);
    if (eHref) YAHOO.util.Dom.setAttribute(oEle, "href", eHref);
    if (eClass) YAHOO.util.Dom.setAttribute(oEle, "class", eClass);
    if (style) YAHOO.util.Dom.setAttribute(oEle, "style", style);
    if (options) {
        eValue = eValue == "" ? "0" : eValue;
        AddOptions(oEle, options, options[eValue].value);
    }
    if (hndlrFunction && eventType) YAHOO.util.Event.addListener(oEle, eventType, hndlrFunction, scope == null ? oEle : scope, true);
    if (src) YAHOO.util.Dom.setAttribute(oEle, "src", src);
    if (alt) YAHOO.util.Dom.setAttribute(oEle, "alt", alt);
    return oEle;
}

/* 
*  It validates the input of the field with the validation expression
*     and assigns corresponding classes.
* 
*  @param {object} inputElement - The input element 
*  @param {string} regExp - regular expression for validation 
*/
function ValidateField(inputElement, validate) {
    var m_oYDOM = YAHOO.util.Dom;

    m_oYDOM.removeClass(inputElement, "Success");
    m_oYDOM.removeClass(inputElement, "Error");

    var oInputValue = inputElement.value;

    //If the value is empty return
    if (oInputValue == null || oInputValue.length == 0) return;

    if (oInputValue.match(m_oValidators[validate]) == null) {
        //Validation Failed
        m_oYDOM.addClass(inputElement, "Error");
    }
    else {
        //Validation Success
        m_oYDOM.addClass(inputElement, "Success");
    }
}

/* 
*  Validates all the input fields and builds output object 
* 
*  @param {object} fields - Json object with all fields
*  @param {string} fldPreFix - prefix to be added to the field name to get the input element's ID 
*/
function ValidateAllFields(fields, fldPreFix) {
    var IsRequired = false, IsSuccess = true, IsEmptySelect = false, m_oOut = { fields: {} }, m_oYDOM = YAHOO.util.Dom;

    for (field in fields) {
        IsRequired = fields[field].required;
        oElement = m_oYDOM.get(fldPreFix + field);
        IsEmptySelect = fields[field].type == "select" && oElement.value == "0";

        //Check Validation (which would have occurred on blur
        if (oElement.className.indexOf("Error") > -1)
            IsSuccess = false;

        //Check Required fields
        else if (IsRequired && (oElement.value == "" || IsEmptySelect)) {
            m_oYDOM.addClass(oElement, "Error");
            IsSuccess = false;
        }

        //Build ouput object if successfully validated
        if (IsSuccess && !IsEmptySelect) {
            m_oOut.fields[field] = { value: oElement.value, dbCol: fields[field].dbCol };
        }
    }
    if (IsSuccess) return m_oOut.fields;
    return null;
}