
// INITIALISATION:
var SelectOver = "";
var SelectClick = "";
var ErrorDebug = false;

 
////////////////////////////////////////////////////////////////////////////////
//////////////////////////// FORM VALIDATION ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function send_form() {
    var reg = new Array();
    var FormError = false;
    var states = new Array(
                    "alabama", "arizona", "arkansas", "california", "colorado",
                    "connecticut", "delaware", "district of columbia", "florida",
                    "georgia", "idaho", "illinois", "indiana", "iowa", "kansas",
                    "kentucky", "louisiana", "maine", "maryland", "massachusetts",
                    "michigan", "minnesota", "mississippi", "missouri", "montana",
                    "nebraska", "nevada", "new hampshire", "new jersey", "new mexico",
                    "new york", "north carolina", "north dakota", "ohio", "oklahoma",
                    "oregon", "pennsylvania", "rhode island", "south carolina",
                    "south dakota", "tennessee", "texas", "utah", "virginia",
                    "vermont", "washington", "west virginia", "wisconsin", "wyoming",
                    "alaska", "hawai",
                    "al", "az", "ar", "ca", "co", "ct", "de", "dc", "fl", "ga",
                    "id", "il", "in", "ia", "ks", "ky", "la", "me", "md", "ma",
                    "mi", "mn", "ms", "mo", "mt", "ne", "nv", "nh", "nj", "nm",
                    "ny", "nc", "nd", "oh", "ok", "or", "pa", "ri", "sc", "sd",
                    "tn", "tx", "ut", "va", "vt", "wa", "wv", "wi", "wy", "al", "ha"
                    );

if (ErrorDebug == true) { var tap = document.getElementById("taping"); }
    reg["email"] = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{2,4})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{2,4})(\.[a-z]{2,3})*$)/i;
    reg["text"] = /^[a-z\.\-,;:\'\s]+$/i;
    reg["number"] = /^([\-]\s?)?(\d+|\d+[.,]\d+?)$/;
    reg["price"] = /^(\$?\s?)(\d+|\d+[.,]\d{1,2}?)$/;
    reg["zipcode"] = /(^\d{5}$)|(^\d{5}[-\s]\d{4}$)/;
    reg["phone"] = /^([1-9]\d{2})[\-\/\.\s]?\d{3}[\-\/\.\s]?\d{4}$/;
    reg["date"] = /(^([0]?\d|1[0-2])([\-\/\.])([0]?\d|[1-2]\d|3[0-1])([\-\/\.])((19|20|21)\d{2}|\d{2})$)|(^([0]?\d|1[0-2])([\-\/\.])((19|20|21)\d{2}|\d{2})$)/;
    reg["time"] = /(^([0]?\d|1[0-2]):[0-5]\d\s?([ap]m)?$)|(^([0]?\d|1[0-2])\s?([ap]m)?(\s\-\s)?([0]?\d|1[0-2])\s?([ap]m)?$)/i;
    reg["alphanum"] = /^[a-z0-9\.\-\_,;:\"\'\s!\?]+$/i;
    reg["state"] = /^[a-z\'\s]{4, 20}$/i;
    reg["creditcard"] = /^(4[0-9]{12}|4[0-9]{15}|5[1-5][0-9]{14}|3[47][0-9]{14})$/i;
    reg["visa"] = /^(4[0-9]{12})|(4[0-9]{15})$/i;
    reg["mastercard"] = /^5[1-5][0-9]{14}$/i;
    reg["americanexpress"] = /^3[47][0-9]{14}$/i;
if (ErrorDebug == true) { tap.innerHTML = "BEGIN<br />"; }

    var Inputs = document.getElementsByTagName("input");
    var countI = Inputs.length - 1;
if (ErrorDebug == true) { tap.innerHTML += "countI="+countI+"<br />"; }
    for (x=0; x<=countI; x++) {
if (ErrorDebug == true) { tap.innerHTML += "x="+x+"<br />"; }
      var ex_id = /hid_(.+)/;
      ex_id.exec(Inputs[x].id);
      id = RegExp.$1;
if (ErrorDebug == true) { tap.innerHTML += "id="+id+"<br />"; }
      if (document.getElementById(id+"_star")) {
        if (Inputs[x].className == "hidden" && Inputs[x].type != "submit") {
            //text = Inputs[x].replace(/[<].{0,6}[>]/g, "");
            if (document.getElementById(id).type) {
                if (document.getElementById(id).type == "text") { id = "input_box_"+id; }
            }
            if (Inputs[x].title == "creditcard") {
                Inputs[x].value = Inputs[x].value.replace(/[^0-9]+/g, "");
            }
if (ErrorDebug == true) { tap.innerHTML += "Title="+reg[Inputs[x].title].test(Inputs[x].value)+"<br />"; }
            if (reg[Inputs[x].title].test(Inputs[x].value) == false) {
if (ErrorDebug == true) { tap.innerHTML += "reg="+reg[Inputs[x].title].test(Inputs[x].value)+"<br />"; }
                if (document.getElementById(id).className == "radiobox") {
                    Iname = document.getElementById(id).name;
                    Imax = Inputs[x].lang;
                    radioErr = true;
                    for (y=1; y<=Imax; y++) {
                        if (document.getElementById("hid_"+Iname+y).value != "") {
                            radioErr = false;
                            break;
                        }
                    }
                    if (radioErr == true) {
                        change_textcolor(id, "_error");
                        FormError = true;
if (ErrorDebug == true) { tap.innerHTML += "Error!<br />"; }
                    } else {
                        change_textcolor(id, "");
                    } 
                } else if (Inputs[x].title == "state") {
                    nb = states.length - 1;
                    value = Inputs[x].value;
                    error_state = true;
                    for (y=0; y<=nb; y++) {
if (ErrorDebug == true) { tap.innerHTML += states[y]+" ?= "+value.toLowerCase()+"<br />"; }
                        if (states[y] == value.toLowerCase()) {
                            error_state = false;
if (ErrorDebug == true) { tap.innerHTML += "YES!<br />"; }
                        }
                    }
                    if (error_state == true) {
                        change_textcolor(id, "_error");
                        FormError = true;
if (ErrorDebug == true) { tap.innerHTML += "Error!<br />"; }
                    }
                } else {
if (ErrorDebug == true) { tap.innerHTML += "Title = "+Inputs[x].title+"<br />"; }
//if (ErrorDebug == true) { tap.innerHTML += "Type = "+cctype+"<br />"; }
if (ErrorDebug == true) { tap.innerHTML += "Value = "+Inputs[x].value+"<br />"; }
//if (ErrorDebug == true) { tap.innerHTML += "Reg = "+reg[cctype]+"<br />"; }
                    change_textcolor(id, "_error");
                    FormError = true;
if (ErrorDebug == true) { tap.innerHTML += "Error!<br />"; }
                }
            } else if (Inputs[x].title == "creditcard") {
if (ErrorDebug == true) { tap.innerHTML += "Title = "+Inputs[x].title+"<br />"; }
                cctype = document.getElementById("hid_cardtype").value.toLowerCase();
                cctype = cctype.replace(/\s/, '');
if (ErrorDebug == true) { tap.innerHTML += "Type = "+cctype+"<br />"; }
                if (cctype != "") {
if (ErrorDebug == true) { tap.innerHTML += "Value = "+Inputs[x].value+"<br />"; }
if (ErrorDebug == true) { tap.innerHTML += "Reg = "+reg[cctype]+"<br />"; }
                    test = reg[cctype].test(Inputs[x].value);
                    if (reg[cctype].test(Inputs[x].value) == false) {
if (ErrorDebug == true) { tap.innerHTML += "Test = "+test+"<br />"; }
if (ErrorDebug == true) { tap.innerHTML += "Id = "+id+"<br />"; }
                        change_textcolor(id, "_error");
                        FormError = true;
if (ErrorDebug == true) { tap.innerHTML += "Error!<br />"; }
                    }
                }
            } else {
if (ErrorDebug == true) { tap.innerHTML += "Good!<br />"; }
            }
        }
      }
    }
if (ErrorDebug == true) { tap.innerHTML += "FormError = "+FormError+"<br>"; }
    var TextAreas = document.getElementsByTagName("textarea");
    var countT = TextAreas.length - 1;
    for (x=0; x<=countT; x++) {
      if (document.getElementById(TextAreas[x].id+"_star")) {
        if (reg[TextAreas[x].lang].test(TextAreas[x].value) == false) {
            change_textcolor(TextAreas[x].id, "_error");
            FormError = true;
if (ErrorDebug == true) { tap.innerHTML += "Error!<br />"; }
        } else {
if (ErrorDebug == true) { tap.innerHTML += "Good!<br />"; }
        }
      }
    }

if (ErrorDebug == true) { tap.innerHTML += "FormError = "+FormError+"<br>"; }
    if (FormError == false) {
if (ErrorDebug == true) { tap.innerHTML += "FormError = "+FormError+"<br>"; }
if (ErrorDebug == true) { tap.innerHTML += "Form = "+document.forms[0].method+"<br>"; }
//        return false;
        return true;
    } else {
if (ErrorDebug == true) { tap.innerHTML += "FormError = "+FormError+"<br>"; }
if (ErrorDebug == true) { tap.innerHTML += "Form = "+document.forms[0].method+"<br>"; }
        return false;
    }

}

function close_posted() {
    document.getElementById("emailsuccess").style.display = "none";
    document.getElementById("submitform").style.display = "none";
//    document.getElementById("form")[0].reset();
}

function smallwindow(str, opts, wsize) {
  var URL = str;
  if (!opts) {
  	opts = "toolbar=0,menubar=0,";}
  if (!wsize) {
  	wsize = "width=600,height=400,";}
  var winOpts = opts + wsize + "location=0,status=0,directories=0,resizable=0,scrollbars=1";
  sw = window.open (URL, "sessionWin", winOpts);
  if(NewBrowser) { sw.focus(); }
}


////////////////////////////////////////////////////////////////////////////////
///////////////////////////// VISUAL EFFECTS ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

function Init() {
    var HideDiv = document.getElementsByTagName("div");
    for (var x=0; x < HideDiv.length; x++) {
        if (HideDiv[x].className == "hide") { HideDiv[x].style.display = "none"; }
    }
}

function changebox(id, who, what, path) {
    var ex_check = /checkbox_(\d)_(.+).png/;
    var ex_radio = /radiobox_(\d)_(.+).png/;
    var checked = 0;
    var over = "off";
    obj = document.getElementById(id);
if (ErrorDebug == true) { var tap = document.getElementById("taping"); }
if (ErrorDebug == true) { tap.innerHTML = "id="+id+"<br>"; }
if (ErrorDebug == true) { tap.innerHTML = "who="+who+"<br>"; }
if (ErrorDebug == true) { tap.innerHTML = "what="+what+"<br>"; }

    if (obj.style.backgroundImage != "") {
        if (who == "checkbox") { ex_check.exec(obj.style.backgroundImage); }
        if (who == "radiobox") { ex_radio.exec(obj.style.backgroundImage); }
        checked = RegExp.$1;
        over = RegExp.$2;
    }

    if (what == "click") {
        if (who == "checkbox") {
            if (checked == 0) {
                obj.style.backgroundImage = "url("+path+"images/"+who+"_1_"+over+".png)";
                document.getElementById("hid_"+id).value = document.getElementById("text_span_"+id).innerHTML;
            } else if (checked == 1) {
                obj.style.backgroundImage = "url("+path+"images/"+who+"_0_"+over+".png)";
                document.getElementById("hid_"+id).value = "";
            }
        } else if (who == "radiobox") {
            name = obj.name;
            for (x=1; x<=obj.getAttribute('maxlength'); x++) {
                document.getElementById(name+x).style.backgroundImage = "url("+path+"images/"+who+"_0_off.png)";
            }
            obj.style.backgroundImage = "url("+path+"images/"+who+"_1_on.png)";
            for (x=1; x<=obj.getAttribute('maxlength'); x++) {
                document.getElementById("hid_"+name+x).value = document.getElementById("text_span_"+id).innerHTML;
            }
        }
    } else if (what == "over") {
        obj.style.backgroundImage = "url("+path+"images/"+who+"_"+checked+"_on.png)";
    } else if (what == "out") {
        obj.style.backgroundImage = "url("+path+"images/"+who+"_"+checked+"_off.png)";
    }
}

function change_select(id, what, path) {
    var ex_select = /select_(\d+)_(.+).png/;
    var checked = 0;
    var over = "off";
//var tap = document.getElementById("taping");
    obj = document.getElementById(id);
    div = document.getElementById(id+"_opt");
//tap.innerHTML += "obj="+obj+" | div="+div+"<br>";

    if (obj.style.backgroundImage != "") {
        ex_select.exec(obj.style.backgroundImage);
        checked = RegExp.$1;
        over = RegExp.$2;
    }

    if (what == "over") {
        obj.style.backgroundImage = "url("+path+"images/select_"+checked+"_on.png)";
        document.getElementById("target_"+id).className = "hover";
        SelectOver = id;
    } else if (what == "out") {
        obj.style.backgroundImage = "url("+path+"images/select_"+checked+"_off.png)";
        document.getElementById("target_"+id).className = "none";
        SelectOver = "";
    } else if (what == "down") {
        if (SelectClick == "") {
            SelectClick = id;
            obj.style.backgroundImage = "url("+path+"images/select_1_"+over+".png)";
            div.style.display = "";
        } else {
            if (SelectClick != SelectOver) {
                document.getElementById(SelectClick+"_opt").style.display = "none";
                document.getElementById(SelectClick).style.backgroundImage = "url("+path+"images/select_0_off.png)";
                SelectClick = id;
                obj.style.backgroundImage = "url("+path+"images/select_1_"+over+".png)";
                div.style.display = "";
            } else {
                SelectClick = "";
                obj.style.backgroundImage = "url("+path+"images/select_0_"+over+".png)";
                div.style.display = "none";
            }
        }
    } else if (what == "up") {
        document.getElementById("target_"+id).focus();
    } else if (what == "blur") {
        if (SelectOver == "") {
            SelectClick = "";
            obj.style.backgroundImage = "url("+path+"images/select_0_"+over+".png)";
            div.style.display = "none";
        }
    }
}

function new_select(id, val, realId) {
    document.getElementById("target_"+id).innerHTML = document.getElementById(realId).innerHTML;
    document.getElementById("hid_"+id).value =  document.getElementById(realId).innerHTML;
}

function update(id, what) {
    hide = document.getElementById("hid_"+id);
    obj = document.getElementById(id);
    if (what == "text") {
        hide.value = obj.value;
    }
}

function button(id, type, path) {
    img = document.getElementById(id);
    if (type == "over") {
        img.src = ""+path+"images/submit_on.png";
    } else if (type == "out") {
        img.src = ""+path+"images/submit_off.png";
    } else if (type == "down") {
        img.src = ""+path+"images/submit_click.png";
    } else if (type == "up") {
        img.src = ""+path+"images/submit_on.png";
    }
}


////////////////////////////////////////////////////////////////////////////////
/////////////////////////// CSS MODIFICATIONS //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


function highlight(id, box, how) {
    if (box == "input_box") {
        document.getElementById(box+"_"+id).className = box+how;
        document.getElementById(id).className = "textbox"+how;
        change_textcolor(box+"_"+id, how);
    } else {
        document.getElementById(id).className = box+how;
        change_textcolor(id, how);
    }
}

function change_textcolor(id, how) {
    document.getElementById("text_span_"+id).className = "text_box"+how;
}



