var strPreFix = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_editmodepanel1_";
var blnIsSubmitted = false;

function SubmitCheckFields(){
    var strError = "";
    var count = 0;
    
    var hdCheckBoxList = document.getElementById(strPreFix + "hdCheckBoxList");
    var strCheckList = hdCheckBoxList.value.split('|');
    if (strCheckList.length > 0)
    {
        var strCheck = "";
        for (i=0;i<strCheckList.length;i++)
        {
            var chk = document.getElementById(strPreFix + strCheckList[i]);
            if (chk != null){
                if (chk.checked)
                    strCheck += strCheckList[i] + '|';
            }
        }
        var hdCheckBoxCheck = document.getElementById(strPreFix + "hdCheckBoxCheck");
        strCheck = strCheck.substring(0,strCheck.length-1);
        hdCheckBoxCheck.value = strCheck;
    }
    
    var txtFirstName = document.getElementById(strPreFix + "txtFirstName");
    if (txtFirstName.value.replace(/ /g, '').length == 0){
        strError = "<li>Please enter your first name.</li>";
        count++;    
    }
    
    var txtLastName = document.getElementById(strPreFix + "txtLastName");
    if (txtLastName.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your last name.</li>";
        count++;    
    }
    
    var txtPhone = document.getElementById(strPreFix + "txtPhone");
    if (txtPhone.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your phone number.</li>";
        count++;    
    }
    
    //Start Fixed for Issue no : 4230 - Sugar>Problem with empty Company (Account Name).
    //Fixed Date : 06-June-08
    var txtCompany = document.getElementById(strPreFix + "txtCompany");
    if (txtCompany.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your company name.</li>";
        count++;    
    }
    //End Fixed for Issue no : 4230 - Sugar>Problem with empty Company (Account Name).
    
    //Start - Add new field named "Job Title"
    var txtCompany = document.getElementById(strPreFix + "txtJobTitle");
    if (txtCompany.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your job title name.</li>";
        count++;    
    }
    //End - Add new field named "Job Title"
    
    //Start Fixed for Issue ID : 4235 - Event Registration Page - add City Field
    //Fixed Date : 27-June-08
    var txtCity = document.getElementById(strPreFix + "txtCity");
    if (txtCity.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your city name.</li>";
        count++;    
    }
    //End Fixed for Issue ID : 4235 - Event Registration Page - add City Field
    
    
    var txtEmail = document.getElementById(strPreFix + "txtEmail");
    if (txtEmail.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your email address.</li>";
        count++;    
    }
    else
    {
        if (!validateEmail(txtEmail.value)){
            strError = strError + "<li>Please enter a valid email address.</li>";
            count++; 
        }
    }
    var hdIsUpdateData = document.getElementById(strPreFix + "hdIsUpdateData");
    var txtPassword = document.getElementById(strPreFix + "txtPassword");
    if (hdIsUpdateData.value == "false"){
        if (txtPassword.value.indexOf(' ') < 0){
            if (txtPassword.value.replace(/ /g, '').length == 0){
                strError = strError + "<li>Please enter your login password.</li>";
                count++;    
            }
            else{
                if (txtPassword.value.replace(/ /g, '').length < 6 || txtPassword.value.replace(/ /g, '').length > 8){
                     //Start Change for Issue no : 4208
                     //strError = strError + "<li>Password must be 6 to 8.</li>";
                     strError = strError + "<li>Password must be 6 to 8 characters in length.</li>";
                     //End Change.
                     count++;
                }
            }
        }
        else{
            strError = strError + "<li>Password is not allowed to use space.</li>";
            count++;
        }
    }
    else{
        if (txtPassword.value.indexOf(' ') < 0){
            if (txtPassword.value.replace(/ /g, '').length != 0){
                if (txtPassword.value.replace(/ /g, '').length < 6 || txtPassword.value.replace(/ /g, '').length > 8){
                     //Start Change for Issue no : 4208
                     //strError = strError + "<li>Password must be 6 to 8.</li>";
                     strError = strError + "<li>Password must be 6 to 8 characters in length.</li>";
                     //End Change.
                     count++;
                }
            }
        }
        else{
            strError = strError + "<li>Password is not allowed to use space.</li>";
            count++;
        }
    }
    
    var txtRePassword = document.getElementById(strPreFix + "txtRePassword");
    if (txtPassword.value != txtRePassword.value){
        strError = strError + "<li>Password must be matched Re-enter Password.</li>";
        count++;
    }
    
    var dvError = document.getElementById('dvError');
    if (strError != ''){
        var lblErrorMsg = document.getElementById(strPreFix + "lblErrorMsg");
        if (count > 1)
            lblErrorMsg.innerHTML = "<ul>" + strError + "</ul>";
        else{
            strError = strError.replace("<li>", '');
            lblErrorMsg.innerHTML = strError.replace("</li>", '');
        }
        window.scrollTo(0,0);
        dvError.style.display = '';
    }
    else
        dvError.style.display = 'none';
        
    void(0);
}

function validateEmail(strEmail){
    /*Start Fixed for Issue No : 4215 - Dash is incorrectly forbidden in email address.
    Fixed Date : 04-06-08*/
    //var strEmailRegExp = /^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$/;
    var strEmailRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
    /*End Fixed for Issue No : 4215 - Dash is incorrectly forbidden in email address.*/
    return strEmailRegExp.test(strEmail);
}

function showErrorOnPage(strError){
    var dvError = document.getElementById('dvError');
    if (strError != ''){
        var lblErrorMsg = document.getElementById(strPreFix + "lblErrorMsg");
        lblErrorMsg.innerHTML = strError;
        window.scrollTo(0,0);
        dvError.style.display = '';
    }
 }
 
function checkBoxTic(strCheck){
    var i = 0;
    var chk = strCheck.split('|');
    for (i = 0;i<chk.length;i++){
        var chkBox = document.getElementById(strPreFix + chk[i]);
        if (chkBox != null)
            chkBox.checked = true;
    }
}

// Description: validate unsubscription fields
// Author: Danchai.S    23 April 2008
function validateUnsubscriptionFields()
{
    if (blnIsSubmitted)
    {
        blnIsSubmitted = false;
        
        var strErrorMsg = "";
        var intCount = 0;
        var strPrefix = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_Editmodepanel1_";
        var txtEmail = document.getElementById(strPrefix + "Email").value;
        var txtPwd = document.getElementById(strPrefix + "Password").value;
        var lblErrorMsg = document.getElementById("ErrorMessage");

        if (txtEmail.replace(/ /g, '').length == 0 || txtPwd.length == 0)
        {
            strErrorMsg += "<li>Email or password fields cannot be blank</li>";
            intCount++;
        }

        if (txtEmail.replace(/ /g, '').length > 0 && !validateEmail(txtEmail))
        {
            strErrorMsg += "<li>Please enter a valid email address</li>";
            intCount++;
        }

        if (intCount > 0)
        {  
            if (intCount == 1)
                strErrorMsg = strErrorMsg.replace('<li>', '').replace('</li>', '');
            else
                strErrorMsg = "<ul>" + strErrorMsg + "</ul>";
                
            lblErrorMsg.innerHTML = strErrorMsg;
            lblErrorMsg.style.display = 'block';
            
            return false;
        }
        else
            lblErrorMsg.style.display = 'none';
    }
}

// Description: validate user email field
// Author: Danchai.S    23 April 2008
function validateForgottenPasswordFields()
{
    if (blnIsSubmitted)
    {
        blnIsSubmitted = false;
        
        var strErrorMsg = "";
        var strPrefix = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_Editmodepanel1_";
        var txtEmail = document.getElementById(strPrefix + "Email").value;
        var lblErrorMsg = document.getElementById("ErrorMessage");

        if (txtEmail.replace(/ /g, '').length == 0)
            strErrorMsg = "Email field cannot be blank";
        else if (txtEmail.replace(/ /g, '').length > 0 && !validateEmail(txtEmail))
            strErrorMsg = "Please enter a valid email address";

        if (strErrorMsg.length > 0)
        {              
            lblErrorMsg.innerHTML = strErrorMsg;
            lblErrorMsg.style.display = 'block';
            
            return false;
        }
        else
            lblErrorMsg.innerHTML = "";
            //lblErrorMsg.style.display = 'none';
        
    }
}

// Description: validate user email field
// Author: Danchai.S    02 May 2008
function validateLoginFields()
{
    if (blnIsSubmitted)
    {
        blnIsSubmitted = false;
        
        var strErrorMsg = "";
        var intCount = 0;
        var strPrefix = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_Editmodepanel1_";
        var txtEmail = document.getElementById(strPrefix + "Email").value;
        var txtPwd = document.getElementById(strPrefix + "Password").value;
        var lblErrorMsg = document.getElementById("ErrorMessage");

        if (txtEmail.replace(/ /g, '').length == 0 || txtPwd.length == 0)
        {
            strErrorMsg += "<li>Email or password fields cannot be blank</li>";
            intCount++;
        }

        if (txtEmail.replace(/ /g, '').length > 0 && !validateEmail(txtEmail))
        {
            strErrorMsg += "<li>Please enter a valid email address</li>";
            intCount++;
        }

        if (intCount > 0)
        {  
            if (intCount == 1)
                strErrorMsg = strErrorMsg.replace('<li>', '').replace('</li>', '');
            else
                strErrorMsg = "<ul>" + strErrorMsg + "</ul>";
                
            lblErrorMsg.innerHTML = strErrorMsg;
            lblErrorMsg.style.display = 'block';
            
            return false;
        }
        else
            lblErrorMsg.innerHTML = "";
          //  lblErrorMsg.style.display = 'none';
    }
}

// get parameter from query string by name
// Author: Danchai.S    10/03/2008  Created
function getParameterByName(strParamName)
{
    var strValue = "";
    var arrQueryData = null;
    var arrParams = null;
    
    arrParams = window.location.href.split('?');
    if (arrParams != null && arrParams.length > 0)
    {
        arrParams = arrParams[arrParams.length - 1].split('&');
        
        if (arrParams != null & arrParams.length > 0)
            for (i = 0; i < arrParams.length; i++)
            {
                arrQueryData = arrParams[i].split('=');
                if (arrQueryData != null && arrQueryData.length > 0)
                    if (arrQueryData[0].toUpperCase() == strParamName.toUpperCase())
                    {
                        strValue = arrQueryData[1];
                        break;
                    }
            }
    }
    
    arrParams = null;
    arrQueryData = null;
    
    return strValue;    
}

// help to redirect to the right page together with parameters.
// Auther: Danchai.S    06/05/2008  Created
function loginRedirectPage()
{
    var strEventIDs = getParameterByName('EventIDs');
    var strPage = getParameterByName('P');
    var strCity = getParameterByName('City');
    var strType = getParameterByName('Type');
    var strYear = getParameterByName('Year');
    var strMonth = getParameterByName('Month');
    var blnParamFound = false;
    
    var strLocation = "";
    var strPrefixControl = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_Editmodepanel1_";
    if (strType == "" || strType == 'N')//inpSubscriptionLocation
        strLocation = document.getElementById(strPrefixControl  + 'inpSubscriptionLocation').value; 
    else
        strLocation = document.getElementById(strPrefixControl  + 'inpEventLocation').value; 
        
    strLocation += '?';
    
    if (strEventIDs != "")
    {
        strLocation += 'EventIDs=' + strEventIDs;
        blnParamFound = true;
    }
    
    if (strPage != "")
    {
        if (strEventIDs != "")
            strLocation += '&P=' + strPage;
        else
            strLocation += 'P=' + strPage;
            
        blnParamFound = true;
    }
    
    if (strCity != "")
    {
        if (strEventIDs != "" && strPage != "")
            strLocation += '&City=' + strCity;
        else
            strLocation += 'City=' + strCity;
            
        blnParamFound = true;
    }
    
    if (strType != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "")
            strLocation += '&Type=' + strType;
        else
            strLocation += 'Type=' + strType;
            
        blnParamFound = true;
    }
    
    if (strYear != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "" && strYear != "")
            strLocation += '&year=' + strYear;
        else
            strLocation += 'year=' + strYear;
            
        blnParamFound = true;
    }
    
    if (strMonth != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "" && strYear != "" && strMonth != "")
            strLocation += '&month=' + strMonth;
        else
            strLocation += 'month=' + strMonth;
            
        blnParamFound = true;
    }
    
    if (!blnParamFound)
    {
        strLocation = strLocation.replace('?', '');
    }
    
    window.location.href = strLocation;
}

function EventFormSubmitCheckFields(){
    var strError = "";
    var count = 0;
    var strEventPreFix = "ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_editmodepanel1_";
    
    var txtFirstName = document.getElementById(strEventPreFix + "txtFirstName");
    if (txtFirstName.value.replace(/ /g, '').length == 0){
        strError = "<li>Please enter your first name.</li>";
        count++;    
    }
    
    var txtLastName = document.getElementById(strEventPreFix + "txtLastName");
    if (txtLastName.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your last name.</li>";
        count++;    
    }
    
    var txtPhone = document.getElementById(strEventPreFix + "txtPhone");
    if (txtPhone.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your phone number.</li>";
        count++;    
    }
 
    var txtCompany = document.getElementById(strEventPreFix + "txtCompany");
    if (txtCompany.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your company name.</li>";
        count++;    
    }
    
    //Start - Add new field named "Job Title"
    var txtCompany = document.getElementById(strEventPreFix + "txtJobTitle");
    if (txtCompany.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your job title name.</li>";
        count++;    
    }
    //End - Add new field named "Job Title"
    
    var txtCity = document.getElementById(strEventPreFix + "txtCity");
    if (txtCity.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your city name.</li>";
        count++;    
    }
    
    var txtEmail = document.getElementById(strEventPreFix + "txtEmail");
    if (txtEmail.value.replace(/ /g, '').length == 0){
        strError = strError + "<li>Please enter your email address.</li>";
        count++;    
    }
    else
    {
        if (!validateEmail(txtEmail.value)){
            strError = strError + "<li>Please enter a valid email address.</li>";
            count++; 
        }
    }
    
    var dvError = document.getElementById('dvError');
    if (strError != ''){
        var lblErrorMsg = document.getElementById("lblErrorMsg");
        if (count > 1)
            lblErrorMsg.innerHTML = "<ul>" + strError + "</ul>";
        else{
            strError = strError.replace("<li>", '');
            lblErrorMsg.innerHTML = strError.replace("</li>", '');
        }
        window.scrollTo(0,0);
        dvError.style.display = '';
    }
    else
        dvError.style.display = 'none';
        
    void(0);
}

function bindingParameterURL(){
    var strParameter = getParameterURL();
    var hpLogin = document.getElementById("hpLogin");
    if (hpLogin != null)
    {
        var strLink = hpLogin.href;
        hpLogin.href = strLink + strParameter;
    }
    
    var hpSubscription = document.getElementById("hpSubscription");
    if (hpSubscription != null)
    {
        var strLink2 = hpSubscription.href;
        hpSubscription.href = strLink2 + strParameter;
    }
}
function getParameterURL(){
    var strEventIDs = getParameterByName('EventIDs');
    var strPage = getParameterByName('P');
    var strCity = getParameterByName('City');
    var strType = getParameterByName('Type');
    var strYear = getParameterByName('year');
    var strMonth = getParameterByName('month');
    var blnParamFound = false;
     
    var strLocation = '';
               
    strLocation += '?';
    
    if (strEventIDs != "")
    {
        strLocation += 'EventIDs=' + strEventIDs;
        blnParamFound = true;
    }
    
    if (strPage != "")
    {
        if (strEventIDs != "")
            strLocation += '&P=' + strPage;
        else
            strLocation += 'P=' + strPage;
            
        blnParamFound = true;
    }
    
    if (strCity != "")
    {
        if (strEventIDs != "" && strPage != "")
            strLocation += '&City=' + strCity;
        else
            strLocation += 'City=' + strCity;
            
        blnParamFound = true;
    }
    
    if (strType != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "")
            strLocation += '&Type=' + strType;
        else
            strLocation += 'Type=' + strType;
            
        blnParamFound = true;
    }
    
    if (strYear != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "" && strYear != "")
            strLocation += '&year=' + strYear;
        else
            strLocation += 'year=' + strYear;
            
        blnParamFound = true;
    }
    
    if (strMonth != "")
    {
        if (strEventIDs != "" && strPage != "" && strType != "" && strYear != "" && strMonth != "")
            strLocation += '&month=' + strMonth;
        else
            strLocation += 'month=' + strMonth;
            
        blnParamFound = true;
    }
    
    if (!blnParamFound)
    {
        strLocation = strLocation.replace('?', '');
    }
    
    return strLocation
}
function showErrorOnEventRegisterPage(strError){
    var dvError = document.getElementById('dvError');
    if (strError != ''){
        var lblErrorMsg = document.getElementById("lblErrorMsg");
        lblErrorMsg.innerHTML = strError;
        window.scrollTo(0,0);
        dvError.style.display = '';
    }
 }

// send event informaton to the request page
// Author: Danchai.S    10/03/2008  Created
function sendInfo()
{
    var objControls = document.getElementsByTagName("input");
    var blnFoundItem = false;
    var strQueryString = document.getElementById("ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_editmodepanel2_inpLoginLocation").value + "?EventIDs=";

    for (var i = 0; i < objControls.length; i++)
    {
        if (objControls[i].type == 'checkbox' && objControls[i].checked == true)
        {
	        var objSplitedText = objControls[i].name.split('$');
	        strQueryString += escape(objSplitedText[objSplitedText.length-1]) + "|";
	        blnFoundItem = true;
        }
    }
	
    if (blnFoundItem)
    {
        var strActualQueryString = '';
        strQueryString = strQueryString.substring(0, strQueryString.length - 1);
        strActualQueryString = strQueryString;
        
        var objSltCity = document.getElementById("ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_editmodepanel2_sltCity");
        if (objSltCity != null)
            strActualQueryString += '&City=' + escape(objSltCity.options[objSltCity.options.selectedIndex].value);
        
        strQueryString = getParameterByName("Page");
        strYear = getParameterByName("year");
        strMonth = getParameterByName("month");
        if (strQueryString != '')
            strActualQueryString += '&Page=' + strQueryString;
            
        if (strYear != '')
            strActualQueryString += '&year=' + strYear;
            
        if (strMonth != '')
            strActualQueryString += '&month=' + strMonth;
        
        var objCurrentPage = document.getElementById("ctl00_ctl00_PlaceHolderMain_PlaceHolderMain_editmodepanel2_inpCurrentPage");
        if (objCurrentPage != null)
            strActualQueryString += '&P=' + escape(objCurrentPage.value) + '&Type=' + escape('E');
        
        window.location.href = strActualQueryString;
    }
    else
        alert('Please select at least one Event.');
}
