var personObj;
var rtnPersonObj;

function confirmCancelReplace(thisUrl, formMethod, thisId, replaceId, cancelId, regTypeId, threshold, quantity) {
    var agree=confirm("You are only allowed to Cancel or Replace " + threshold + " staff members per day.\n\nYou have Cancelled or Replaced " + quantity + " today.\n\nDo you wish to continue?");
    if (agree) {
        document.groupForm.action=thisUrl;
        document.groupForm.method=formMethod;
        document.groupForm.id.value=thisId;
        document.groupForm.replaceid.value=replaceId;
        document.groupForm.cancelid.value=cancelId;
        document.groupForm.regtypeid.value=regTypeId;
        document.groupForm.submit();
        return true;
    } else {
        return false ;
    }
}

function gotoPurchaseItems(thisId) {
    document.groupForm.action='GroupEventsItemsServlet';
    document.groupForm.method="GET";
    document.groupForm.id.value=thisId;
    document.groupForm.replaceid.value="";
    document.groupForm.cancelid.value="";
    document.groupForm.regtypeid.value="";
    document.groupForm.submit();
}

function confirmUpgrade(thisUrl,formMethod) {

    var rt;

    if(isArray(document.groupForm.upgraderegtypeid)) {
        for (i=0;i<document.groupForm.upgraderegtypeid.length;i++) {
            if (document.groupForm.upgraderegtypeid[i].checked) {
                rt = document.groupForm.upgraderegtypeid[i].value;
            }
        }
    } else {
        rt = document.groupForm.upgraderegtypeid.value;
    }
    var agree=confirm("You are about to upgrade your account and incur a balance due.\n\nYou will have to pay the balance on your account before your staff can receive their badges.");
    if (agree) {
        document.groupForm.action=thisUrl;
        document.groupForm.method=formMethod;
        document.groupForm.id.value="";
        document.groupForm.replaceid.value="";
        document.groupForm.cancelid.value="";
        document.groupForm.regtypeid.value=rt;
        document.groupForm.submit();
        return true ;
    } else {
        return false ;
    }
}

function checkToLockField(thisField) {

    if(thisField.name=='people_id' && thisField.value.length > 0) {
         document.searchForm.email.disabled=true;
         document.searchForm.firstname.disabled=true;
         document.searchForm.lastname.disabled=true;
         return;
    } else if(thisField.name=='people_id' && thisField.value.length == 0) {
         document.searchForm.email.disabled=false;
         document.searchForm.firstname.disabled=false;
         document.searchForm.lastname.disabled=false;
         return;
    }

    if(thisField.name=='email' && document.searchForm.email.value.length >0) {
        document.searchForm.people_id.disabled=true;
        document.searchForm.firstname.disabled=true;
        document.searchForm.lastname.disabled=true;
        return;
    } else if(thisField.name=='email' && document.searchForm.email.value.length==0) {
        document.searchForm.people_id.disabled=false;
        document.searchForm.firstname.disabled=false;
        document.searchForm.lastname.disabled=false;
        return;
    }

    if((thisField.name=='firstname' || thisField.name=='lastname') && (document.searchForm.firstname.value.length >0 || document.searchForm.lastname.value.length >0)) {
        document.searchForm.people_id.disabled=true;
        document.searchForm.email.disabled=true;
        return;
    } else if((thisField.name=='firstname' || thisField.name=='lastname') && (document.searchForm.email.value.length==0 && document.searchForm.firstname.value.length==0 && document.searchForm.lastname.value.length==0)) {
        document.searchForm.people_id.disabled=false;
        document.searchForm.email.disabled=false;
        return;
    }

}

function verifySearchForm() {
    if(document.searchForm.people_id.value.length<1 && document.searchForm.email.value.length<1 && document.searchForm.firstname.value.length<1 && document.searchForm.lastname.value.length<1) {
        alert("Please provide at least one search criteria");
        return false;
    }

    if(document.searchForm.email.value.length!=0) {
        if(document.searchForm.email.value.indexOf(' ') > 0) {
            alert("Spaces are not allowed in Email Address");
            return false;
        } else if (document.searchForm.email.value.indexOf('@') == -1) {
            alert ("You need to put a @ in your email address.  Your email address should look like yourname@domain.com");
            return false;
        } else if (document.searchForm.email.value.indexOf('.') == -1) {
            alert ("You need to place a period in your email address.  Your email address should look like yourname@domain.com");
            return false;
        }
    }
    /*
    if(document.searchForm.people_id.value.length<1 && document.searchForm.email.value.length<1 && document.searchForm.lastname.value.length<3)  {
        alert("Please Enter at least the first 3 letters of the last name");
        return false;
    }
    */
}

function groupAdd(id) {
    document.addForm.personid.value = id;

    if(isArray(document.addForm.Add)) {
        for(i=0;i<document.addForm.Add.length; i++) {
            document.addForm.Add[i].disabled=true;
            document.addForm.Add[i].value="Please Wait";
        }
    } else {
        document.addForm.Add.disabled=true;
        document.addForm.Add.value="Please Wait"
    }

    personObj = null;
    rtnPersonObj = null;
    
    var url="jsonPerson.jsp";
    var params ="personid=" + document.addForm.personid.value;
        params+="&a=r";
        params+="&confid=" + document.addForm.confid.value;
        params+="&token=" + document.addForm.token.value;

    var personAjax = new Ajax(url,params);
    personAjax.post();
    personAjax.callback = function() {
        var rtnObj = eval('(' + trim(this.txt) + ')');
        personObj = rtnObj.person[0];
        if(personObj.personid!=0) {
            if(personObj.email=='null' || personObj.email=="") {
                showEmailModal();
            } else {
                document.addForm.submit();
            }
        }
    }
}

function submitAddEmail() {
    if(verifyAddEmail()) {
        var url="jsonPerson.jsp";
        var params ="personid=" + document.addEmail.personid.value;
            params+="&a=u";
            params+="&confid=" + document.addEmail.confid.value;
            params+="&email=" + document.addEmail.email.value
            params+="&token=" + document.addEmail.token.value;

        var personAjax = new Ajax(url,params);
        personAjax.post();
        personAjax.callback = function() {
            var rtnObj = eval('(' + trim(this.txt) + ')');
            rtnPersonObj = rtnObj.person[0];
            if(personObj.personid!=0) {
                finishEmailUpdate();
            } else {
                alert("An Error Occurred");
            }
        }
    }
}

function finishEmailUpdate() {

    if(rtnPersonObj.personid==document.addEmail.personid.value) {
        closeModal();
        document.addForm.submit();
    } else {
        showDuplicationEmailModal()
    }

}

function closeGroupModal() {
    if(isArray(document.addForm.Add)) {
        for(i=0;i<document.addForm.Add.length; i++) {
            document.addForm.Add[i].disabled=false;
            document.addForm.Add[i].value="Add"
        }
    } else {
        document.addForm.Add.disabled=false;
        document.addForm.Add.value="Add"
    }
    closeModal();
}


function showEmailModal() {
    var mTxt =  "<div class='modalWindowHeader'>\n";
        mTxt += "     <div class='pad' style='float:right;'><a href='javascript:void(0);' onclick='closeGroupModal();'><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;'>Missing Email Address</div>\n";
        mTxt += "</div>\n";
        mTxt += "<div class='modalWindowMain' style='text-align:center;'>";
        mTxt += "   <br/><h4>Please Provide " + personObj.name + "'s Email Address</h4><br/><br/>";
        mTxt += "   <div class='box_curve_shadow_white' style='width:330px;'>";
        mTxt += "       <span class='top'><span class='topright'></span></span>";
        mTxt += "       <span class='bottom'><span class='bottomleft'></span></span>";
        mTxt += "           <div class='content' style='text-align:center;'>";
        mTxt += "           <form name='addEmail' method='post' action='javascript:void(0);'>\n";
        mTxt += "           <input type=hidden name=token value='" + document.addForm.token.value + "' />";
        mTxt += "           <input type=hidden name=personid value='" + personObj.personid + "'>";
        mTxt += "           <input type=hidden name=confid value='" + document.addForm.confid.value + "'>";
        mTxt += "           <b>Email: </b><input type='text' name='email' size='30' maxlength='50' />";
        mTxt += "           <input type='button' value='Update' onClick='submitAddEmail();' />"
        mTxt += "           </form>";
        mTxt += "           </div>";
        mTxt += "   </div>";
        mTxt += "</div>";
        mTxt += "<div class='modalWindowFooter'></div>";

    showModal(mTxt,400,150);
}

function showDuplicationEmailModal() {

    var mTxt  = "<div class='modalWindowHeader'>\n";
        mTxt +="     <div class='pad' style='float:right;'><a href=\"javascript:void(0);\" onclick=\"closeGroupModal();\"><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'>\n";
        mTxt += "   <div class='pad' style='text-align:left;margin:10px;'>";
        mTxt += "       The email address <i>" + rtnPersonObj.email + "</i> is already in our system for <b>" + rtnPersonObj.name + " ("+ rtnPersonObj.company + ")</b>.<Br/><br/>";
        mTxt += "       Our system requires all email addresses to be unique and cannot be shared among multiple registrants.<Br/><br/>";
        mTxt += "   </div>\n";
        mTxt += "   <center>\n";
        mTxt += "   <h4>Please Provide " + personObj.name + "'s Email Address</h4><br/><br/>";
        mTxt += "   <div class='box_curve_shadow_white' style='width:330px;'>";
        mTxt += "       <span class='top'><span class='topright'></span></span>";
        mTxt += "       <span class='bottom'><span class='bottomleft'></span></span>";
        mTxt += "           <div class='content' style='text-align:center;'>";
        mTxt += "           <form name='addEmail' method='post' action='javascript:void(0);'>\n";
        mTxt += "           <input type=hidden name=token value='" + document.addForm.token.value + "' />";
        mTxt += "           <input type=hidden name=personid value='" + personObj.personid + "'>";
        mTxt += "           <input type=hidden name=confid value='" + document.addForm.confid.value + "'>";
        mTxt += "           <b>Email: </b><input type='text' name='email' size='30' maxlength='50' />";
        mTxt += "           <input type='button' value='Update' onClick='submitAddEmail();' />"
        mTxt += "           </form>";
        mTxt += "           </div>";
        mTxt += "   </div>";
        if(rtnPersonObj.reg=="N") {
            mTxt += "   <br/> - or - <br/><br/>";
            mTxt += "   <a href='javascript:void(0);' onClick='document.addForm.personid.value=" + rtnPersonObj.personid + ";document.addForm.submit();'>ADD '" + rtnPersonObj.name + " ("+ rtnPersonObj.company + ")'</a>";
        }
        mTxt += "   </center>\n";
        mTxt += "</div>\n";
        mTxt += "<div class='modalWindowFooter'></div>";

    showModal(mTxt,400,150);
}

function verifyAddEmail() {
    if(document.addEmail.email.value.length < 6) {
        alert("Please Provide Your Email");
        return false;
    } else {
        if(document.addEmail.email.value.indexOf(' ') > 0) {
            alert("Spaces are not allowed in Email Address");
            return false;
        } else if (document.addEmail.email.value.indexOf('@') == -1) {
            alert ("You need to put a @ in your email address.  Your email address should look like yourname@domain.com");
            return false;
        } else if (document.addEmail.email.value.indexOf('.') == -1) {
            alert ("You need to place a period in your email address.  Your email address should look like yourname@domain.com");
            return false;
        }
    }
    return true;
}
