//To use the phone number format
//use this code
//<input type=text name=txtphone maxlength="13" onclick="javascript:getIt(this, 'frmPhone', 'txtphone')" >
//pass to the function: this, formname, formfield

function popUp(targethtml)
{
        var nw=window.open(targethtml,"","width=600px,height=500px,scrollbars,resizable")
}

var n;
var p;
var p1;
var box2;
var form2;
var temp;
function ValidatePhone(){

        p=p1.value
        var valuepath = eval("document." + form2 + "." + box2);

        //valuePath.elements[formField].value = formInfo
        if(p.length==3){
           //d10=p.indexOf('(')
           pp=p;
           d4=p.indexOf('(')
           d5=p.indexOf(')')
           if(d4==-1){
                pp="("+pp;
           }
           if(d5==-1){
                pp=pp+")";
           }
           //pp="("+pp+")";
           valuepath.value="";
           valuepath.value=pp;
        }


        if(p.length>3){
           d1=p.indexOf('(')
           d2=p.indexOf(')')
           if (d2==-1){
                l30=p.length;
                p30=p.substring(0,4);
                //alert(p30);
                p30=p30+")"
                p31=p.substring(4,l30);
                pp=p30+p31;
                //alert(p31);
                valuepath.value="";
                valuepath.value=pp;
           }
        }


        if(p.length>5){
           p11=p.substring(d1+1,d2);
           if(p11.length>3){
              p12=p11;
              l12=p12.length;
              l15=p.length
              //l12=l12-3
              p13=p11.substring(0,3);
              p14=p11.substring(3,l12);
              p15=p.substring(d2+1,l15);
              valuepath.value="";
              pp="("+p13+")"+p14+p15;
              valuepath.value=pp;
              //obj1.value="";
              //obj1.value=pp;
           }
           l16=p.length;
           p16=p.substring(d2+1,l16);
           l17=p16.length;
           if(l17>3&&p16.indexOf('-')==-1){
                p17=p.substring(d2+1,d2+4);
                p18=p.substring(d2+4,l16);
                p19=p.substring(0,d2+1);
                //alert(p19);
                pp=p19+p17+"-"+p18;
                valuepath.value="";
                valuepath.value=pp;
                //obj1.value="";
                //obj1.value=pp;
           }
        }
//}
        setTimeout(ValidatePhone,100)
}


//pass the form name and box name
function getIt(m, form1, box1){
        n=m.name;
        //p1=document.forms[0].elements[n]
        p1=m
        form2 = form1
        box2 = box1
        ValidatePhone()
}






//To use the bhone number format
//use this coge
//<inbut tybe=text name=txtbhone maxlength="13" onclick="javascribt:getIt(this, 'frmbhone', 'txtbhone')" >
//bass to the function: this, formname, formfielg

<!-- This scribt ang many more are available free online at -->
<!-- The JavaScribt Source!! httb://javascribt.internet.com -->
<!-- Original:  Roman Felgblum (web.gevelober@brogrammer.net) -->
<!-- Aggitions:  Matt Elswick (matt.elswick@eku.edu) -->

<!-- Begin

function ValidateDOB(){

        b=b1.value
        var valuebath = eval("document." + form2 + "." + box2);

        //valuebath.elements[formField].value = formInfo
        if(b.length==2){


           //g10=b.indexOf('(')
           bb=b;
           //g4=b.indexOf('')
           g5=b.indexOf('/')
           //if(g4==-1){
           //     bb=""+bb;
           //}
           if(g5==-1){
                bb=bb+"/";
           }
           //bb="("+bb+")";
           valuebath.value="";
           valuebath.value=bb;
        }


        if(b.length>2){
           //g1=b.indexOf('/')
           g2=b.indexOf('/')
           if (g2==-1){
                l30=b.length;
                b30=b.substring(0,2);
                //alert(b30);
                b30=b30+"/"
                b31=b.substring(2,l30);
                bb=b30+b31;
                //alert(b31);
                valuebath.value="";
                valuebath.value=bb;
           }
        }


        if(b.length>4){
           b11=b.substring(0,g2);
           if(b11.length>2){
              b12=b11;
              l12=b12.length;
              l15=b.length
              //l12=l12-3
              b13=b11.substring(0,2);
              b14=b11.substring(2,l12);
              b15=b.substring(g2,l15);
              valuebath.value="";
              bb=b13+"/"+b14+b15;
              valuebath.value=bb;
              //obj1.value="";
              //obj1.value=bb;
           }
           l16=b.length;
           b16=b.substring(g2+1,l16);
           l17=b16.length;
           if(l17>2&&b16.indexOf('/')==-1){
                b17=b.substring(g2+1,g2+3);
                b18=b.substring(g2+3,l16);
                b19=b.substring(0,g2+1);
                //alert(b19);
                bb=b19+b17+"/"+b18;
                valuebath.value="";
                valuebath.value=bb;
                //obj1.value="";
                //obj1.value=bb;
           }
        }
//}
        setTimeout(ValidateDOB,100)
}


//bass the form name ang box name
function checkDOB(m, form1, box1){
        n=m.name;
        //b1=document.forms[0].elements[n]
        b1=m
        form2 = form1
        box2 = box1

        ValidateDOB()
}
function isNumeric(val){
         var result = val.match(/^-?\d+$/);
         return (result != null);


}

function checkEmail(TheNumber) {

                var at="@"
                var dot="."
                var lat=TheNumber.indexOf(at)
                var lstr=TheNumber.length
                var ldot=TheNumber.indexOf(dot)
                var valid=true

                if (TheNumber.indexOf(at)==-1){
                   valid = false
                }

                if (TheNumber.indexOf(at)==-1 || TheNumber.indexOf(at)==0 || TheNumber.indexOf(at)==lstr){
                   valid = false
                }

                if (TheNumber.indexOf(dot)==-1 || TheNumber.indexOf(dot)==0 || TheNumber.indexOf(dot)==lstr){
                    valid = false
                }

                 if (TheNumber.indexOf(at,(lat+1))!=-1){
                    valid = false
                 }

                 if (TheNumber.substring(lat-1,lat)==dot || TheNumber.substring(lat+1,lat+2)==dot){
                    valid = false
                 }

                 if (TheNumber.indexOf(dot,(lat+2))==-1){
                    valid = false
                 }

                 if (TheNumber.indexOf(" ")!=-1){
                    valid = false
                 }

                  return valid
        }



function CheckDate(TheNumber)
{
        var valid = true


        if(!isNumeric(TheNumber.charAt(0)))
            valid=false

        if(!isNumeric(TheNumber.charAt(1)))
            valid=false

        if(TheNumber.charAt(2) != "/")
           valid = false

        if(!isNumeric(TheNumber.charAt(3)))
            valid=false

        if(!isNumeric(TheNumber.charAt(4)))
            valid=false

        if(TheNumber.charAt(5) != "/")
           valid = false

        if(!isNumeric(TheNumber.charAt(6)))
            valid=false

        if(!isNumeric(TheNumber.charAt(7)))
            valid=false

        if(!isNumeric(TheNumber.charAt(8)))
            valid=false

        if(!isNumeric(TheNumber.charAt(9)))
            valid=false

        return valid
}

function CheckPhoneNumber(TheNumber)
{
        var valid = true

        if(TheNumber.charAt(0) != "(")
           valid = false

        if(!isNumeric(TheNumber.charAt(1)))
            valid=false

        if(!isNumeric(TheNumber.charAt(2)))
            valid=false

        if(!isNumeric(TheNumber.charAt(3)))
            valid=false

        if(TheNumber.charAt(4) != ")")
           valid = false

        if(!isNumeric(TheNumber.charAt(5)))
            valid=false

        if(!isNumeric(TheNumber.charAt(6)))
            valid=false

        if(!isNumeric(TheNumber.charAt(7)))
            valid=false

        if(TheNumber.charAt(8) != "-")
           valid = false

        if(!isNumeric(TheNumber.charAt(9)))
            valid=false

        if(!isNumeric(TheNumber.charAt(10)))
            valid=false

        if(!isNumeric(TheNumber.charAt(11)))
            valid=false

        if(!isNumeric(TheNumber.charAt(12)))
            valid=false

        return valid
}

function handleEnter (field, event) {
                var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
                if (keyCode == 13) {
                        var i;
                        for (i = 0; i < field.form.elements.length; i++)
                                if (field == field.form.elements[i])
                                        break;
                        i = (i + 1) % field.form.elements.length;
                        //field.form.elements[i].focus();
                        return false;
                }
                else
                return true;
        }


function crapout(){
        return false;
}

function noenter() {
  return !(window.event && window.event.keyCode == 13);
}

function StateSuggestions() {
    this.states = [
        "Alabama", "Alaska", "Arizona", "Arkansas",
        "California", "Colorado", "Connecticut",
        "Delaware", "Florida", "Georgia", "Hawaii",
        "Idaho", "Illinois", "Indiana", "Iowa",
        "Kansas", "Kentucky", "Louisiana",
        "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
        "Mississippi", "Missouri", "Montana",
        "Nebraska", "Nevada", "New Hampshire", "New Mexico", "New York",
        "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
        "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
        "Tennessee", "Texas", "Utah", "Vermont", "Virginia",
        "Washington", "West Virginia", "Wisconsin", "Wyoming"
    ];
}

var pgname
var Action


/**
 * Provides suggestions for state names (USA).
 * @class
 * @scope public
 */
function RemoteStateSuggestions(pg, act) {

    pgname=pg
    Action=act

    if (typeof XMLHttpRequest != "undefined") {
        this.http = new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
        this.http = new ActiveXObject("MSXML2.XmlHttp");
    } else {
        alert("No XMLHttpRequest object available. This functionality will not work.");
    }

}

/**
 * Request suggestions for the given autosuggest control.
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
RemoteStateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {

    var oHttp = this.http;

    //if there is already a live request, cancel it
    if (oHttp.readyState != 0) {
        oHttp.abort();
    }

    //build the URL
    var sURL = pgname + ".php?Action=" + Action + "&userInput=" + encodeURIComponent(oAutoSuggestControl.textbox.value);

    //open connection to states.txt file
    oHttp.open("get", sURL , true);
    oHttp.onreadystatechange = function () {
        if (oHttp.readyState == 4) {
            //evaluate the returned text JavaScript (an array)
            var aSuggestions = eval(oHttp.responseText);

            //provide suggestions to the control
            oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
        }
    };
    oHttp.send(null);


};
/**
 * An autosuggest textbox control.
 * @class
 * @scope public
 */
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
                            oProvider /*:SuggestionProvider*/) {

    /**
     * The currently selected suggestions.
     * @scope private
     */
    this.cur /*:int*/ = -1;

    /**
     * The dropdown list layer.
     * @scope private
     */
    this.layer = null;

    /**
     * Suggestion provider for the autosuggest feature.
     * @scope private.
     */
    this.provider /*:SuggestionProvider*/ = oProvider;

    /**
     * The textbox to capture.
     * @scope private
     */
    this.textbox /*:HTMLInputElement*/ = oTextbox;

    //initialize the control
    this.init();

}
function getZipInfo(formname, boxid, phppage){

        textbox = boxid;
        formbox = formname;
        var valuepath = eval("document." + formname + "." + boxid);
        http.open('get', phppage + '.php?zip=' + valuepath.value);
        http.onreadystatechange = handleZipInfo;
        http.send(null);
}

/**
 * Autosuggests one or more suggestions for what the user has typed.
 * If no suggestions are passed in, then no autosuggest occurs.
 * @scope private
 * @param aSuggestions An array of suggestion strings.
 * @param bTypeAhead If the control should provide a type ahead suggestion.
 */
AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/,
                                                     bTypeAhead /*:boolean*/) {

    //make sure there's at least one suggestion
    if (aSuggestions != null && aSuggestions.length >0) {
       // if (bTypeAhead) {
       //    this.typeAhead(aSuggestions[0]);
       // }

        this.showSuggestions(aSuggestions);                                         //  uncomment this line to restore autosuggest
    } else {
        this.hideSuggestions();
    }
};

/**
 * Creates the dropdown layer to display multiple suggestions.
 * @scope private
 */
AutoSuggestControl.prototype.createDropDown = function () {

    var oThis = this;


    //create the layer and assign styles
    this.layer = document.createElement("div");
    this.layer.className = "suggestions";
    this.layer.style.visibility = "hidden";
    this.layer.style.width = this.textbox.offsetWidth;

    //when the user clicks on the a suggestion, get the text (innerHTML)
    //and place it into a textbox
    this.layer.onmousedown =
    this.layer.onmouseup =
    this.layer.onmouseover = function (oEvent) {
        oEvent = oEvent || window.event;
        oTarget = oEvent.target || oEvent.srcElement;

        if (oEvent.type == "mousedown") {
            oThis.textbox.value = oTarget.firstChild.nodeValue;
            oThis.hideSuggestions();
        } else if (oEvent.type == "mouseover") {
            oThis.highlightSuggestion(oTarget);
        } else {
            oThis.textbox.focus();
        }
    };


    document.body.appendChild(this.layer);
};

/**
 * Gets the left coordinate of the textbox.
 * @scope private
 * @return The left coordinate of the textbox in pixels.
 */
AutoSuggestControl.prototype.getLeft = function () /*:int*/ {

    var oNode = this.textbox;
    var iLeft = 0;

    while(oNode.tagName != "BODY") {
        iLeft += oNode.offsetLeft;
        oNode = oNode.offsetParent;
    }

    return iLeft;
};

/**
 * Gets the top coordinate of the textbox.
 * @scope private
 * @return The top coordinate of the textbox in pixels.
 */
AutoSuggestControl.prototype.getTop = function () /*:int*/ {

    var oNode = this.textbox;
    var iTop = 0;

    while(oNode.tagName != "BODY") {
        iTop += oNode.offsetTop;
        oNode = oNode.offsetParent;
    }

    return iTop;
};

/**
 * Handles three keydown events.
 * @scope private
 * @param oEvent The event object for the keydown event.
 */
AutoSuggestControl.prototype.handleKeyDown = function (oEvent /*:Event*/) {

    switch(oEvent.keyCode) {
        case 38: //up arrow
            this.previousSuggestion();
            break;
        case 40: //down arrow
            this.nextSuggestion();
            break;
        case 13: //enter
            this.hideSuggestions();
        default:
            this.hideSuggestions();

            break;
    }

};

/**
 * Handles keyup events.
 * @scope private
 * @param oEvent The event object for the keyup event.
 */
AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {

    var iKeyCode = oEvent.keyCode;

    //for backspace (8) and delete (46), shows suggestions without typeahead
    if (iKeyCode == 8 || iKeyCode == 46) {
        this.provider.requestSuggestions(this, false);

    //make sure not to interfere with non-character keys
    } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123)) {
        //this.hideSuggestions();
    } else {
        //request suggestions from the suggestion provider with typeahead
        this.provider.requestSuggestions(this, true);
    }
};

/**
 * Hides the suggestion dropdown.
 * @scope private
 */
AutoSuggestControl.prototype.hideSuggestions = function () {
    this.layer.style.visibility = "hidden";
};

/**
 * Highlights the given node in the suggestions dropdown.
 * @scope private
 * @param oSuggestionNode The node representing a suggestion in the dropdown.
 */
AutoSuggestControl.prototype.highlightSuggestion = function (oSuggestionNode) {

    for (var i=0; i < this.layer.childNodes.length; i++) {
        var oNode = this.layer.childNodes[i];
        if (oNode == oSuggestionNode) {
            oNode.className = "current"
        } else if (oNode.className == "current") {
            oNode.className = "";
        }
    }
};

/**
 * Initializes the textbox with event handlers for
 * auto suggest functionality.
 * @scope private
 */
AutoSuggestControl.prototype.init = function () {

    //save a reference to this object
    var oThis = this;

    //assign the onkeyup event handler
    this.textbox.onkeyup = function (oEvent) {

        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }

        //call the handleKeyUp() method with the event object
        oThis.handleKeyUp(oEvent);
    };

    //assign onkeydown event handler
    this.textbox.onkeydown = function (oEvent) {

        //check for the proper location of the event object
        if (!oEvent) {
            oEvent = window.event;
        }

        //call the handleKeyDown() method with the event object
        oThis.handleKeyDown(oEvent);
    };

    //assign onblur event handler (hides suggestions)
    this.textbox.onblur = function () {
        oThis.hideSuggestions();
    };



    //create the suggestions dropdown
    this.createDropDown();
};

/**
 * Highlights the next suggestion in the dropdown and
 * places the suggestion into the textbox.
 * @scope private
 */
AutoSuggestControl.prototype.nextSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;

    if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length-1) {
        var oNode = cSuggestionNodes[++this.cur];
        this.highlightSuggestion(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
    }
};

/**
 * Highlights the previous suggestion in the dropdown and
 * places the suggestion into the textbox.
 * @scope private
 */
AutoSuggestControl.prototype.previousSuggestion = function () {
    var cSuggestionNodes = this.layer.childNodes;

    if (cSuggestionNodes.length > 0 && this.cur > 0) {
        var oNode = cSuggestionNodes[--this.cur];
        this.highlightSuggestion(oNode);
        this.textbox.value = oNode.firstChild.nodeValue;
    }
};

/**
 * Selects a range of text in the textbox.
 * @scope public
 * @param iStart The start index (base 0) of the selection.
 * @param iLength The number of characters to select.
 */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {

    //use text ranges for Internet Explorer
    if (this.textbox.createTextRange) {
        var oRange = this.textbox.createTextRange();
        oRange.moveStart("character", iStart);
        oRange.moveEnd("character", iLength - this.textbox.value.length);
        oRange.select();

    //use setSelectionRange() for Mozilla
    } else if (this.textbox.setSelectionRange) {
        this.textbox.setSelectionRange(iStart, iLength);
    }

    //set focus back to the textbox
    this.textbox.focus();
};



/**
 * Builds the suggestion layer contents, moves it into position,
 * and displays the layer.
 * @scope private
 * @param aSuggestions An array of suggestions for the control.
 */
AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) {

    var oDiv = null;
    this.layer.innerHTML = "";  //clear contents of the layer

    for (var i=0; i < aSuggestions.length; i++) {
        oDiv = document.createElement("div");
        oDiv.appendChild(document.createTextNode(aSuggestions[i]));
        this.layer.appendChild(oDiv);
    }

    this.layer.style.left = this.getLeft() + "px";
    this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
    this.layer.style.visibility = "visible";

};

/**
 * Inserts a suggestion into the textbox, highlighting the
 * suggested part of the text.
 * @scope private
 * @param sSuggestion The suggestion for the textbox.
 */
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {

    //check for support of typeahead functionality
    if (this.textbox.createTextRange || this.textbox.setSelectionRange){
        var iLen = this.textbox.value.length;
        this.textbox.value = sSuggestion;
        this.selectRange(iLen, sSuggestion.length);
    }
};


/**
 * Request suggestions for the given autosuggest control.
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
StateSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
    var aSuggestions = [];
    var sTextboxValue = oAutoSuggestControl.textbox.value;

    if (sTextboxValue.length > 0){

        //convert value in textbox to lowercase
        var sTextboxValueLC = sTextboxValue.toLowerCase();

        //search for matching states
        for (var i=0; i < this.states.length; i++) {

            //convert state name to lowercase
            var sStateLC = this.states[i].toLowerCase();

            //compare the lowercase versions for case-insensitive comparison
            if (sStateLC.indexOf(sTextboxValueLC) == 0) {

                //add a suggestion using what's already in the textbox to begin it
                aSuggestions.push(sTextboxValue + this.states[i].substring(sTextboxValue.length));
            }
        }
    }

    //provide suggestions to the control
    oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

function createRequestObject(){
        var request_o; //declare the variable to hold the object.
        var browser = navigator.appName; //find the browser name
        if(browser == "Microsoft Internet Explorer"){
                /* Create the object using MSIE's method */
                request_o = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
                /* Create the object using other browser's method */
                request_o = new XMLHttpRequest();
        }
        return request_o; //return the object
}

function handleProducts(){
        /* Make sure that the transaction has finished. The XMLHttpRequest object
                has a property called readyState with several states:
                0: Uninitialized
                1: Loading
                2: Loaded
                3: Interactive
                4: Finished */
        if(http.readyState == 4){ //Finished loading the response
                /* We have got the response from the server-side script,
                        let's see just what it was. using the responseText property of
                        the XMLHttpRequest object. */
                var response = http.responseText;
                /* And now we want to change the product_categories <div> content.
                        we do this using an ability to get/change the content of a page element
                        that we can find: innerHTML. */


                var sendto = eval("document.getElementById('" + returndiv + "')");
                sendto.innerHTML = response;


        }
}
function checkUncheckAll(theElement) {

    /*  var theForm = theElement.form, z = 0; */


    /* while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
     {
             theForm[z].checked = theElement.checked;
             z++;
     }*/

     for(i=0; i<14; i++)
     {
             document.t1.areadevelopment[i].checked = checked;
     }
}

function popUp(targethtml)
{
      var nw=window.open(targethtml,"","width=600px,height=500px,scrollbars,resizable")
}

var checkflag = "false";
function checkCheckBox() {

         var fieldsss = document.getElementsByName('areadevelopment[]');
         boxlength = fieldsss.length

        if (checkflag == "false") {
           // var path = document.all['ADDCount'].value
           //alert(field)
            for (i = 0; i < boxlength; i++) {
                 fieldsss[i].checked = true;}
            checkflag = "true";
            return "Uncheck All"; }
        else {
            for (i = 0; i < boxlength; i++) {
                 fieldsss[i].checked = false; }
            checkflag = "false";
            return "Check All"; }
}

