var openInfoTimerId = 0;
var closeInfoTimerId = 0;

var aryImages = new Array(2);
aryImages[0] = "images/expand_button.gif";
aryImages[1] = "images/collapse_button.gif";
aryImages[2] = "images/red_arrow_down.gif";
aryImages[3] = "images/red_arrow_up.gif";

for (i=0; i < aryImages.length; i++) {
    var preload = new Image();
    preload.src = aryImages[i];
}

function charReplace(str) {
    str = str.replace("'", "&#39;");
    str = str.replace("\"", "&#34;");
    return str;
}

function isArray(obj) {
    return(typeof(obj.length)=="undefined")? false:true;
}

function isNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function pageRedirect(thisForm, p) {
    thisForm.form.action = p;
    thisForm.form.submit();
}

function showHideDescriptionBox(id) {
    if(document.getElementById("descriptionBox"+id).style.display=="block") {
        document.getElementById("descriptionBox"+id).style.display = 'none';
        document['arrowImg'+id].src = aryImages[0];
    } else {
        document.getElementById("descriptionBox"+id).style.display = 'block';
        document['arrowImg'+id].src = aryImages[1];
    }
}

function showHideDescriptionBoxExhibitor(id) {
    if(document.getElementById("descriptionBox"+id).style.display=="block") {
        document.getElementById("descriptionBox"+id).style.display = 'none';
        document['arrowImg'+id].src = aryImages[2];
    } else {
        document.getElementById("descriptionBox"+id).style.display = 'block';
        document['arrowImg'+id].src = aryImages[3];
    }
}

function hightlightDiv(elementid,c){
    if(c==1) {
        color="#E3E4FA";
    } else {
        color="#FFFFFF";
    }

    var thisDiv;

    thisDiv = getObjectById(elementid);

    if(document.layers){
        thisDiv.backgroundColor=color;
    } else {
        thisDiv.style.backgroundColor=color;
    }

}

function changeDivContent(elementid, content){

    var thisDiv;
    thisDiv = getObjectById(elementid);
    thisDiv.innerHTML=content;

}

function getObjectById(id) {

    var obj;

    if(document.layers){
        obj=eval("document.layers['" + id + "'];");
    }else if(document.all){
        obj=eval("document.all['" + id + "'];");
    }else if(document.getElementById){
        obj=eval("document.getElementById('" + id + "');");
    }

    return obj;
}

function printConfirmationWindow(url,token) {
    window.open (url + "?printable=true&t=" + token, "printwindow","menubar=0,resizable=0,scrollbars=1,width=655,height=700");
}

function checkCharSet(field) {
    var c;
    var escapedStr = encodeURI(field.value)
    var count = 0;
    var truncString = "";
    var isLatin = true;

    for(y=0; y < field.value.length; y++) {
        c = field.value.charAt(y);
        //if(!(c.charCodeAt(0) >= 65 && c.charCodeAt(0) <= 122) && !(c.charCodeAt(0) >= 192 && c.charCodeAt(0) <= 255) && (c.charCodeAt(0)!=45) && (c.charCodeAt(0)!=32))  {
        if(!(c.charCodeAt(0) >= 32 && c.charCodeAt(0) <= 126))  {
            isLatin = false;
        }
    }
    if(!isLatin) {
        alert("Invalid Character(s).  Only ISO Latin 1 Characters are allowed on that field");
        field.value = field.value.substring(0,field.value.length-1);
        field.focus;
    }


    for(y=0; y < field.value.length; y++) {
        c = encodeURI(field.value.charAt(y));
        if(c != "%20") {
            newChar = c.replace(/%/g, "");
            if(newChar.length > 1) {
                count = count + newChar.length/2;
            } else {
                count++;
            }
        } else {
            count++;
        }
        if(count <= field.maxLength) {
            truncString+=decodeURI(c);
        }
    }

    if(count > field.maxLength) {
        alert("You have exceeded the max size of " + field.maxLength + " characters for this field.  Note: Special characters like Accents and Umlauts can be equivalent to 2 or more characters");
        field.value = truncString;
        field.focus;
    }
}

function moveBanners() {
    if(document.body.scrollTop+document.getElementById('bannerLeft').offsetHeight <= document.getElementById('main').offsetHeight) {
        document.getElementById('bannerLeft').style.marginTop = document.body.scrollTop;
        document.getElementById('bannerRight').style.marginTop = document.body.scrollTop;
    }
}

function helpWindow(q) {
    window.open("Help.jsp?q=" + q, "help","menubar=false,titlebar=no,resizable=false,width=400,height=300");
}

function displaySourceCodeExample(x) {
    if(x==1) {
        imagePath = "images/brochure_code.jpg";
    } else {
        imagePath = "images/registration_form_code.jpg";
    }
    window.open(imagePath,'sourcecodewindow','width=515,height=395,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');

}

function loginRowDisplay(id) {

    var rows = document.getElementsByTagName('tr');
    var isExpanded = false;
    for(i=0; i<rows.length; i++) {
        if(rows[i].id!="" && rows[i].id.indexOf("R"+id+"E")>-1) {
            rows[i].style.display = ((rows[i].style.display == '') ? 'none' : '');
            if(rows[i].style.display=='none') {
                isExpanded = true;
            }
        }
    }

    if(isExpanded) {
        document['arrowImg'+id].src = aryImages[0];
    } else {
        document['arrowImg'+id].src = aryImages[1];
    }
}


function disableButtons(thisForm) {
    thisForm.value="Please Wait";
}


/******************************************************/
/**           BUTTON MOUSEOVER ALT TEXT              **/
/******************************************************/

function showButtonDescription(obj) {

    clearTimeout(openInfoTimerId);

    var posx = 0;
    var posy = 0;

    var objPos = getXY(obj);
    posx = objPos.x+(obj.offsetWidth-5) ;
    posy = objPos.y;
    
    switch(obj.value) {
        case "Change":
            var msg = "<div>Change the registration type for this person.</div>";
            break;
        case "Replace":
            var msg = "<div>Replace this person with another person.<br/>Everything is transferred (payments, etc.)<br/>to the new person.</div>";
            break;
        case "Purchase":
            var msg = "<div>Buy additional items not included with<br/>the registration for this person.</div>";
            break;
        case "Cancel":
            var msg = "<div>Cancel this person's registration.</div>";
            break;
        default:
            var msg = "<BLANK>";

    }
    openInfoTimerId = setTimeout("showInfoBox(" + posx + "," + posy + ",\"" + msg + "\")", 1000);
}

function hideButtonDescription() {
    clearTimeout(openInfoTimerId);
    var infoBox = getObjectById("hoverWindow");
    infoBox.innerHTML = "";
    infoBox.style.left="0px";
    infoBox.style.top="0px";
    infoBox.style.visibility="hidden";
}

function showInfoBox(xpos,ypos,msg) {
    var infoBox = getObjectById("hoverWindow");
    infoBox.innerHTML = msg;
    infoBox.style.left="" + xpos + "px";
    infoBox.style.top="" + (ypos-infoBox.offsetHeight+5) + "px";
    infoBox.style.visibility="visible";
}

function getXY(obj) {
    var curleft = 0;
    var curtop = 0;
    var border;

    if (obj.offsetParent) {
        do {
            if (getStyle(obj, 'position') == 'relative') {
                if (border = getStyle(obj, 'border-top-width')) {
                    curtop += parseInt(border);
                }
                if (border = getStyle(obj, 'border-left-width')) {
                    curleft += parseInt(border);
                }
            }
               
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent)

    } else if (obj.x) {
        curleft += obj.x;
        curtop += obj.y;
    }


    return {'x': curleft, 'y': curtop};
}

function getStyle(obj, styleProp) {
    if (obj.currentStyle) {
        //** IE **//
        return obj.currentStyle[styleProp];
    }
    else if (window.getComputedStyle) {
        //** MOZILLA **//
        return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
    }
}

/******************************************************/
/**           MODAL WINDOW FUNCTIONS                 **/
/******************************************************/
function showModal(mTxt,mWidth,mTop) {

    var modalPage = getObjectById("modalPage");
    var modalWindow = getObjectById("modalWindow");

    window.onscroll = function () { modalPage.style.top = document.body.scrollTop; };

    modalPage.style.top = document.body.scrollTop;
    modalPage.style.display = "block";

    modalWindow.innerHTML = mTxt;
    modalWindow.style.marginLeft = "-" + mWidth/2 + "px";
    modalWindow.style.top = "" + mTop +"px";
    modalWindow.style.width = "" + mWidth +"px";
    modalWindow.style.display = "block";

}

function closeModal() {
    var modalPage = getObjectById("modalPage");
    var modalWindow = getObjectById("modalWindow");

    modalWindow.innerHTML = "";

    modalPage.style.display = "none";
    modalWindow.style.display = "none";
}

function showRelatedItems(confId,regtypeId,token) {

    var url="ajaxRegTypeDetails.jsp";
    var params="confid=" + confId;
        params+="&regtypeid=" + regtypeId;
        params+="&token=" + token;

    var eventAjax = new Ajax(url,params);
    eventAjax.post();
    eventAjax.callback = function() {

        var mTxt = "<div class='modalWindowHeader'>\n";
            mTxt +="     <div class='pad' style='float:right;'><a href='javascript:void(0);' onclick='closeModal();'>close window <img src='images/close_icon.gif' width=18 height=18 border=0 alt='' align=top /></a></div>\n";
            mTxt +="     <div class='pad' style='font-size:14pt;font-weight:bold;'></div>\n";
            mTxt +="</div>\n";
            mTxt +="<div class='modalWindowMain' style='height:438px;'>" + this.txt + "</div>\n";
            mTxt +="<div class='modalWindowFooter'>\n";
            mTxt +="     <b>** Space Available is an estimate and does not guarantee availability.</b>\n";
            mTxt +="</div>\n";

        showModal(mTxt,720,15);
    }
}

function showAdditionalItems(confId,token) {

    var url="ajaxRegTypeDetails.jsp";
    var params="confid=" + confId;
        params+="&token=" + token;

    var eventAjax = new Ajax(url,params);
    eventAjax.post();
    eventAjax.callback = function() {

        var mTxt = "<div class='modalWindowHeader'>\n";
            mTxt +="     <div class='pad' style='float:right;'><a href='javascript:void(0);' onclick='closeModal();'>close window <img src='images/close_icon.gif' width=18 height=18 border=0 alt='' align=top /></a></div>\n";
            mTxt +="     <div class='pad' style='font-size:14pt;font-weight:bold;'></div>\n";
            mTxt +="</div>\n";
            mTxt +="<div class='modalWindowMain' style='height:438px;'>" + this.txt + "</div>\n";
            mTxt +="<div class='modalWindowFooter'>\n";
            mTxt +="     <div class='pad'><b>** Space Available is an estimate and does not guarantee availability.</b></div>\n";
            mTxt +="</div>\n";

        showModal(mTxt,600,15);
    }
}

function showChangeRegType(confid,contactid,personid,token) {

    var url="jsonGroupChangeRegType.jsp";
    var params="confid=" + confid;
        params+="&contactid=" + contactid;
        params+="&personid=" + personid;
        params+="&token=" + token;

    var regtypeAjax = new Ajax(url,params);
    regtypeAjax.post();
    regtypeAjax.callback = function() {

        var parsedArray = parseChangeRegTypeArray(this.txt);
        var personArray = parsedArray.person[0];
        var regtypesArray = parsedArray.regtypes;

        var mTxt =  "<div class='modalWindowHeader'>\n";
            mTxt += "     <div class='pad' style='float:right;'><a href='javascript:void(0);' onclick='closeModal();'>close window <img src='images/close_icon.gif' width=18 height=18 border=0 alt='' align=top /></a></div>\n";
            mTxt += "     <div class='pad' style='font-size:14pt;font-weight:bold;'>Change Registration Type</div>\n";
            mTxt += "</div>\n";
            mTxt += "<div class='modalWindowMain' style='text-align:left;'>";
            mTxt += "<br/><h3>" + personArray.name + "</h3><br/>";
            mTxt += "<form action='GroupCreateRegistrationServlet' method='post'>\n";
            mTxt += "<input type=hidden name=token value='" + token + "' />";
            mTxt += "<input type=hidden name=personid value='" + personid + "'>";
            mTxt += "<blockquote>";
        
            for(i=0;i<regtypesArray.length;i++) {
                if(regtypesArray[i].available>0) {
                    mTxt+= "<input type=radio name='regtypeid' id='radioRegType" + regtypesArray[i].regtypeid +  "' value='" + regtypesArray[i].regtypeid + "'";
                    if(regtypesArray[i].regtypeid==personArray.regtypeid) {
                        mTxt+= " CHECKED";
                    }
                    mTxt+= "><label for='radioRegType" + regtypesArray[i].regtypeid  + "'>" + regtypesArray[i].description + "</lable><br/>";
                } else {
                    mTxt+= "<input type=radio name='regtype' value='" + regtypesArray[i].regtypeid + "'";
                    if(regtypesArray[i].regtypeid==personArray.regtypeid) {
                        mTxt+= " CHECKED";
                    }
                    mTxt+= " DISABLED><strike>" + regtypesArray[i].description + "</strike><br/>";    


                }
            }
            mTxt += "</blockquote>";
            mTxt += "<center><input type='submit' value='Change' /> <input type='button' value='Cancel' onclick='closeModal();' /></center>";
            mTxt += "</form>";
            mTxt += "</div>";
            mTxt += "<div class='modalWindowFooter'></div>";

        showModal(mTxt,500,50);
    }
}

function parseChangeRegTypeArray(jsontext) {
    var myObj = eval('(' + jsontext + ')');
    return myObj;
}



function checkForExistingEmail(email,confid,personid,replaceid,regtypeid,target,token) {

    if(email!="" && document.contactForm.Email.value.indexOf(' ') > 0) {
        alert("Spaces are not allowed in Email Address");
        document.contactForm.Email.focus();
        return;
    } else if (email!="" && document.contactForm.Email.value.indexOf('@') == -1) {
        alert ("You need to put a @ in your email address.  Your email address should look like yourname@domain.com");
        document.contactForm.Email.focus();
        return;
    } else if (email!="" && document.contactForm.Email.value.indexOf('.') == -1) {
        alert ("You need to place a period in your email address.  Your email address should look like yourname@domain.com");
        document.contactForm.Email.focus();
        return;
    } else if (email!="") {

        var emailArray = new Array();

        var url="jsonEmailSearch.jsp";
        var params="personid=" + personid;
            params+="&email=" + email;
            params+="&confid=" + confid;
            params+="&token=" + token;


        var emailAjax = new Ajax(url,params);
        emailAjax.post();
        emailAjax.callback = function() {
            emailArray = parseEmailArray(this.txt);
            if(emailArray[0].personid!=0) {
                if(emailArray[0].personid!=personid) {
                    showDuplicationEmailAlert(emailArray,replaceid,regtypeid,target,token);
                }
            }
        }
    }    
}

function parseEmailArray(jsontext) {
    var myObj = eval('(' + jsontext + ')');
    return myObj.person;
}


function showDuplicationEmailAlert(emailArray,replaceid,regtypeid,target,token) {
    if(target=='GroupContactInformationServlet') {
        var mTxt  = "<div class='modalWindowHeader'>\n";
            mTxt +="     <div class='pad' style='float:right;'><a href=\"javascript:void(0);\" onclick=\"document.contactForm.Email.value='';closeModal();\">close window <img src='images/close_icon.gif' width=18 height=18 border=0 alt='' align=top /></a></div>\n";
            mTxt += "     <div class='pad' style='font-size:14pt;font-weight:bold;'><b>Email Already Exists</b></div>\n";
            mTxt += "</div>\n";
            mTxt += "<div id='modalWindowMain'><div class='pad'>\n";
            mTxt += "The email address <i>" + emailArray[0].email + "</i> is already in our system for <b>" + emailArray[0].name + "</b>.<Br/><br/>";
            mTxt += "<div id='modalWindowContent'>\n";
            mTxt += "Our system requires all email addresses to be unique and cannot be shared amongst multiple registrants.<Br/><br/>";
            if(emailArray[0].reg=='N') {
                mTxt += "Would you like to add this person to your group plan?<br/><br/>"
                mTxt += "<form action='GroupCreateRegistrationServlet' method='post'>\n";
                mTxt += "<input type=hidden name=token value='" + token + "' />";
                mTxt += "<input type=hidden name=regtypeid value='" + regtypeid + "'>";
                mTxt += "<input type=hidden name=replaceid value='" + replaceid + "'>";
                mTxt += "<input type=hidden name=personid value='" + emailArray[0].personid + "'>";
                mTxt += "<center><input type=submit name=\"loginbtn\" value=\"Yes\" /> <input type=button name=\"loginbtn\" value=\"No\" onClick=\"document.contactForm.Email.value='';document.contactForm.Email.focus();closeModal();\" /></center><br/>";
            } else {
                mTxt += "<center><b>This person is already registered for this event.</b><br/><br/>"
                mTxt += "<input type=button name=\"loginbtn\" value=\"Close\" onClick=\"document.contactForm.Email.value='';document.contactForm.Email.focus();closeModal();\" /></center><br/>";
            }

            mTxt += "</form>"
            mTxt += "</div>";
            mTxt += "</div></div>\n";

    } else {
        var mTxt  = "<div class='modalWindowHeader'>\n";
            mTxt +="     <div class='pad' style='float:right;'><a href=\"javascript:void(0);\" onclick=\"document.contactForm.Email.value='';closeModal();\">close window <img src='images/close_icon.gif' width=18 height=18 border=0 alt='' align=top /></a></div>\n";
            mTxt += "     <div class='pad' style='font-size:14pt;font-weight:bold;'><b>Email Already Exists</b></div>\n";
            mTxt += "</div>\n";
            mTxt += "<div id='modalWindowMain'><div class='pad'>\n";
            mTxt += "The email address <i>" + emailArray[0].email + "</i> is already in our system for <b>" + emailArray[0].name + "</b>.<Br/><br/>";
            mTxt += "<div id='modalWindowContent'>\n";
            mTxt += "Our system requires all email addresses to be unique and cannot be shared among multiple registrants.<Br/><br/>";
            mTxt += "Would you like to log in to that account?<br/><br/>"
            mTxt += "<center><input type=button name=\"loginbtn\" value=\"Yes\" onClick=\"showDuplicateEmailAlertLogin('" + emailArray[0].email + "','" + token +"');\" /> <input type=button name=\"loginbtn\" value=\"No\" onClick=\"document.contactForm.Email.value='';document.contactForm.Email.focus();closeModal();\" /></center><br/>";
            mTxt += "</div>";
            mTxt += "</div></div>\n";
    }
    showModal(mTxt,400,150);
}

function showDuplicateEmailAlertLogin(email,token) {
    var thisDiv = getObjectById('modalWindowContent');

    var mTxt  = "<form method=post action='LoginServlet' name='loginForm'>";
        mTxt += "<input type=hidden name=token value='" + token + "' />";
        mTxt += "<input type=hidden name=email_id value='" + email + "' />";
        mTxt += "<center>\n";
        mTxt += "Please enter the password for this account<br/><br/>";
        mTxt += "<b>Password: </b><input type=password name=\"password\" size=20 /><input type=submit value=\"Log In\" /></b>";
        mTxt += "</form><br/>";
        mTxt += "<div id='modalWindowSendPassword'>Forgot your password? Click Here: <a href=\"javascript:void(0);\" onClick=\"showDuplicateEmailAlertSendPassword('" + email + "','" + token + "');\">Retrieve Password</a></div>";
        mTxt += "</center>";

    thisDiv.innerHTML = mTxt;

}

function showDuplicateEmailAlertSendPassword(email,token) {
    var thisDiv = getObjectById('modalWindowSendPassword');
    thisDiv.innerHTML = "Please Wait - Sending Password";

    var url="ajaxSendPassword.jsp";
    var params="email=" + email;
        params+="&token=" + token;

    var passwordAjax = new Ajax(url,params);
    passwordAjax.post();
    passwordAjax.callback = function() {
        var results = trim(this.txt);
        if(results=="1") {
            thisDiv.innerHTML = "Your password has been sent to <i>" + email + "</i>";
        } else {
            thisDiv.innerHTML = "Your email address was not found in our system";    
        }


    }

}

function showDuplicateEmailAlertAdd(personid, regtypeid,token) {

    alert(personid + " " + regtypeid + " " + token);

}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function getCheckboxByValue(objs,value) {
    for(z=0;z<objs.length;z++) {
        if(objs[z].value==value) {
            return objs[z];
        }
    }
    return false;
}
