﻿
$(function() {
    $("#tdTabPage1").addClass("tab_selected");
    $('#imgValidateLocation').hide();


    $("#lnkLanguage").hover(
	            function() {
	                $("#divLanguage").fadeIn(500);
	            },
	            function() {
	                setTimeout("checkHover()", 4000);
	            }
	         );

    $("#lnkSubmitAnotherCase").click(
	    function() {
	        location = 'create-case.aspx?ReqType=SubmitAnother';
	    }

	 )

    $("#lnkViewAnotherCase").click(
	    function() {
	        $("#divShow").hide();
	        $("#divGet").show();
	        $("#txtGet_CaseId").attr("value", "");
	        $("#txtGet_CaseAccessCode").attr("value", "");
	    }

	 )


    $("#lnkSubmitViewCase").click(
	    function() {
	        TabPage2Clicked();
	    }
	 )

    $("#tdTabPage1").click(
	    function() {
	        TabPage1Clicked();
	    }
	 )

    $("#tdTabPage2").click(
	    function() {
	        TabPage2Clicked();
	    }
	 )

    $("#tdTabPage3").click(
	            function() {
	                removeTabClass();
	                $("#tdTabPage3").addClass("tab_selected");
	            }
	 )

    $(".languageLbClass").hover(
	            function() { $(this).addClass("languageLbClass_hover") }
	            ,
	            function() { $(this).removeClass("languageLbClass_hover") }
        );

    $("#imgTelephone").mousedown(function() {

        $.weeboxs.open('#frmCallMeBack',
	                {
	                    title: $("#hidTran_CallMeBack").attr("value"),
	                    okBtnName: $("#btnSubmit").attr("value"),
	                    cancelBtnName: $("#hidTran_Cancel").attr("value"),
	                    width: 400,
	                    height: 180,
	                    contentChange: true,
	                    onok: onCallbackDiaolgOk,
	                    oncancel: onCallbackDialogCancel
	                });

        $("#txtCallbackTel").attr("value", getTelephoneTextarea());
        $("#txtCallbackQuestion").attr("value", getDetailTextarea());




    });

  
    onGenCaptcha();

})
    function removeTabClass() {
        $("#tdTabPage1").removeClass("tab_selected");
        $("#tdTabPage2").removeClass("tab_selected");
        $("#tdTabPage3").removeClass("tab_selected");
    }

    function hideTabPage() {
        $("#pagediv_CreateCase").hide();
        $("#pagediv_ViewCase").hide();
    }

    function TabPage1Clicked() {
        removeTabClass();
        hideTabPage();
        $("#tdTabPage1").addClass("tab_selected");
        $("#pagediv_CreateCase").show();

        $("#divCaseSubmitResult_Success").hide();
        $("#divCaseSubmitResult_Fail").hide();
        $("#divCreateCase").show();

        hideshowCallerDetails();
    }
    
    function TabPage2Clicked(){
         removeTabClass();
         hideTabPage();
         $("#tdTabPage2").addClass("tab_selected");
         $("#pagediv_ViewCase").show();
    }
    

   function checkHover() {
       $("#divLanguage").fadeOut(500);
   }


   function onCallbackDiaolgOk() {

       var telValue = $("#txtCallbackTel").attr("value").replace(/(^\s*)|(\s*$)/g, "");
       var detailValue = $("#txtCallbackQuestion").attr("value").replace(/(^\s*)|(\s*$)/g, "");
       if(telValue=="" || detailValue==""){
           alert($("#hidTran_FillAllFields").attr("value"));           
        }
       else if(telValue.length>150){
            alert($("#hidTran_MaxlengthOver150").attr("value"));
        }
       else if(detailValue.length>2000){
            alert($("#hidTran_MaxlengthOver2000").attr("value"));   
       }
       else {
            //save the value before close dialog
           $("#hidCallbackTelephone").attr("value", telValue);
           $("#hidCallbackDetails").attr("value", detailValue);

           $.weeboxs.close();  

       }

   }

   function onCallbackDialogCancel() {
   
       $("#txtCallbackTel").attr("value","");
       $("#txtCallbackQuestion").attr("value", "");

//       var telValue = $("#txtCallbackTel").attr("value");
//       var detailValue = $("#txtCallbackQuestion").attr("value");
//       alert(telValue);
//       alert(detailValue);
       $.weeboxs.close();       
       
   }
   

    // get the display text content in call back dialog telephone area.
   function getTelephoneTextarea() {
       var sRet = "";
       var objChkAnonymous = ge(_ANONYMOUS_CHECK);
       if (objChkAnonymous.checked == true ) {
               return "";
      }
           
       var telHome = $("#frmTelephone").attr("value");
       var telWork = $("#frmWork").attr("value");
       var telMobile = $("#frmMobile").attr("value");

       if (telHome != "") { sRet += telHome; sRet += "; " }
       if (telWork != "") { sRet += telWork; sRet += "; " }
       if (telMobile != "") { sRet += telMobile; sRet += "; " }
       return sRet;
       
   }
   
   // get the display text contnet in call back dialog detail area.
   function getDetailTextarea() {
       var sRet = "";
       var elementContent = "";
       var aryLabel = $(".emptyCss");

       var objChkAnonymous = ge(_ANONYMOUS_CHECK);

       
       for (j = 0; j < aryLabel.length; j++) {
           elementContent = "";

           //if anonymous caller is checked, we no longer need caller infomation for call back detail
           if (objChkAnonymous.checked == true && isCtrlBelongToCallerinfo(aryLabel[j].id)) {
               continue;
           }

           if ((getRadioBoxValue("frmLocationType") != "address") && aryLabel[j].id == "lbfrmIncUnit") {
               continue;
           }
           
           if (aryLabel[j].id == "lbfrmIncCity") {
               if (getRadioBoxValue("frmLocationType") == "address" || getRadioBoxValue("frmLocationType") == "intersection") {
                   var ddl = document.getElementById('frmIncCity');
                   elementContent = ddl.options[ddl.selectedIndex].text;
               }
           }
           else {       
               elementContent = $("#" + aryLabel[j].id.substring(2)).attr("value");
           }

           if (aryLabel[j].id == "lbfrmIncCaseType") elementContent=$("#hidSelectedCaseType").val();
           if (elementContent != "" && elementContent != "- select one -")
           {
               sRet += aryLabel[j].innerText;
               sRet += " ";
               sRet += elementContent;
               sRet += "\n"; 
           }
           
         
       }
       return sRet;
   }

   function handleLanguageChange(sLanguage) {
       $("#hidSelectedLanguage").attr("value", sLanguage);
       alert( $("#hidSelectedLanguage").attr("value"));
       $("#hidbtn_ChangeLanguage").click();
   }

   function isIE() { //ie? 
       if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
           return true;
       else
           return false;
   }

   if (!isIE()) { //firefox innerText define
       HTMLElement.prototype.__defineGetter__("innerText", function() {var anyString = ""; var childS = this.childNodes; for (var i = 0; i < childS.length; i++) {if (childS[i].nodeType == 1) anyString += childS[i].innerText; else if (childS[i].nodeType == 3)anyString += childS[i].nodeValue;} return anyString; } );
       HTMLElement.prototype.__defineSetter__("innerText", function(sText) { this.textContent = sText; });
   }

   function getRadioBoxValue(radioName) {
       var obj = document.getElementsByName(radioName);             
       for (i = 0; i < obj.length; i++) {
           if (obj[i].checked) {
               return obj[i].value;
           }
       }
       return "undefined";
   }
   
   
   function isCtrlBelongToCallerinfo(sCtrlID) {
       var CallerInfoCtrlID = "lbfrmFirstName|lbfrmLastName|lbfrmEmail|lbfrmOrganization|lbfrmTelephone|lbfrmWork|lbfrmFax|lbfrmMobile|lbfrmStreetNumber|lbfrmStreetName|lbfrmUnitNumber|lbfrmCity|lbfrmState|lbfrmCountry|lbfrmPostal|";
       //alert(CallerInfoCtrlID + "      " + sCtrlID);
       return contains(CallerInfoCtrlID , sCtrlID, false)
}


//string:original string
//substr:string to look up
//isIgnoreCase:ignore case
 function contains(ostring,substr,isIgnoreCase){
    if(isIgnoreCase)
    {
        ostring = ostring.toLowerCase();
        substr=substr.toLowerCase();
    }
     var startChar=substr.substring(0,1);
     var strLen=substr.length;
     for (var j = 0; j < ostring.length - strLen; j++)
         {
             if (ostring.charAt(j) == startChar)
             {
                 if (ostring.substring(j, j + strLen) == substr)
                   {
                         return true;
                   }   
             }
         }
         return false;
}



//--------show category and case type--------//
$(function() {
    $('#frmIncCategory').change(function() { OnCategorySelected(); });
    $('#frmIncCaseType').change(function() { OnCaseTypeSelected(); });
    $('#btnSubmit').click(function() { OnbtnSubmitClicked(); });
    $('#frmAnonymous').click(function() { OnAnonymoustClicked(); });
    $('#imgCategoryChanged').hide();
    $('#imgCaseTypeChanged').hide();
    HidLocationUdefAttachmentDiv();
})

function OnbtnSubmitClicked() {
    var iCount = $('#hidDetectPageRefresh').val() + 1;
    $('#hidDetectPageRefresh').val(iCount);
}

function IsCaseTypeValid() {
    return CheckCategoryInput() & CheckCaseTypeInput() & SaveCaseUdefValue();
}

function CheckCategoryInput() {
    var bReturn = true;
    var bShowCategoryList = $('#hidShowCategoryList').val();
    if (bShowCategoryList == '1') {
        document.getElementById('frmIncCategoryError').style.display = 'none';
        var iCategoryIndex = document.getElementById('frmIncCategory').selectedIndex;
        if (iCategoryIndex < 1) {
            document.getElementById('frmIncCategoryError').style.display = 'block';
            bReturn = false;
        }
    }
    return bReturn;
}

function CheckCaseTypeInput() {
    document.getElementById('frmIncCaseTypeError').style.display = 'none';
    var oSelect = document.getElementById('frmIncCaseType');
    if (oSelect.options.length == 0) {
        document.getElementById('frmIncCaseTypeError').style.display = 'block';
        return false;
    }
    if (oSelect.selectedIndex < 1) {
        document.getElementById('frmIncCaseTypeError').style.display = 'block';
        return false;
    }
    return true;
}

function OnCategorySelected() {
    $('#imgValidateLocation').hide();
    $('#lbCategoryDescription').text('');
    $('#lbCaseTypeDescription').text('');
    if (CheckCategoryInput()) {
        document.getElementById('frmIncCaseType').options.length = 0;

        var sSelectedCategory = $('#frmIncCategory').val();
        $('#hidSelectedCategory').val(sSelectedCategory);

        $('#imgCategoryChanged').show();
        try {
            GetCaseTypeList(sSelectedCategory);
        }
        catch (error) {
            $('#imgCategoryChanged').hide();
        }        
    }
}

function OnAnonymoustClicked() {
    var bShowCategoryList = $('#hidShowCategoryList').val();
    $('#lbCategoryDescription').text('');
    $('#lbCaseTypeDescription').text('');
    $('#divUdefContainer').hide();
    $('#divLocationContainer').hide();    
    document.getElementById('frmIncCaseType').options.length = 0;
    if (bShowCategoryList == '1') {
        document.getElementById('frmIncCategory').selectedIndex = 0;
    } else {
        $('#imgCaseTypeChanged').show();
        try {
            GetCaseTypeList('');
        }
        catch (error) {
            $('#imgCaseTypeChanged').hide();
        }
    }
}

function HidLocationUdefAttachmentDiv() {
    $('#divUdefContainer').hide();
    $('#divLocationContainer').hide();
    $('#pnlUploadAttachment').hide();
}

function GetCaseTypeList(sSelectedCategory) {
    HidLocationUdefAttachmentDiv();
    try {
        $.get('show-casetype.aspx', { ReqType: 'ShowCaseTypeList', SelectedCategory: sSelectedCategory, LanguageName: $('#hidSelectedLanguage').val() }, function(data) {
            if (data) {
                var objResult = eval('(' + data.split('|')[0] + ')');
                if (objResult.result == '1') {
                    for (var i = 0; i < objResult.CaseTypeNameArray.length; i++) {
                        var bToAdd = true;
                        if (objResult.AllowAnonymous[i] == '0' && document.getElementById('frmAnonymous').checked == true) {
                            bToAdd = false;
                        }
                        if (bToAdd) {
                            document.getElementById('frmIncCaseType').options.add(new Option(unescape(objResult.CaseTypeTranslatedNameArray[i]), unescape(objResult.CaseTypeNameArray[i])));
                        }
                    }
                }
            }

            //show description of category
            if ($("#hidCategoryList").val() != '') {
                var objCategoryList = eval('(' + $("#hidCategoryList").val() + ')');
                for (var i = 0; i < objCategoryList.CategoryNameArray.length; i++) {
                    if (sSelectedCategory == objCategoryList.CategoryNameArray[i]) {
                        $('#lbCategoryDescription').text(unescape(objCategoryList.CategoryDescriptionArray[i]));
                    }
                }
            
            };

            $('#imgCategoryChanged').hide();
            $('#imgCaseTypeChanged').hide();
        })
    }
    catch (error) {
        $('#imgCategoryChanged').hide();
        $('#imgCaseTypeChanged').hide();
    }
}

function OnCaseTypeSelected() {
    $('#imgValidateLocation').hide();
    $('#lbCaseTypeDescription').text('');
    try {
        if (CheckCaseTypeInput()) {
            var sSelectedCaseType = $('#frmIncCaseType').val();
            $('#hidSelectedCaseType').val(sSelectedCaseType);
            $('#imgCaseTypeChanged').show();
            $.get('show-casetype.aspx', { ReqType: 'ShowCaseTypeDetails', SelectedCaseType: sSelectedCaseType, LanguageName: $('#hidSelectedLanguage').val() }, function(data) {
                if (data) {
                    $('#divUdefContainer').show();
                    $('#divLocationContainer').show();
                    var objResult = eval('(' + data.split('|')[0] + ')');
                    if (objResult.result == '1') {
                        $('#lbCaseTypeDescription').text(objResult.CaseTypeDetails.Description);

                        //set location control displaying
                        HideAllLocationControls();
                        var objLocationType = objResult.CaseTypeDetails.AllowedLocationTypeList
                        if (objLocationType.Unknown == '1') {
                            $('#frmUnknownType').show();
                            $('#frmUnknownType').attr('checked', 'checked');
                            $('#lbLocationUnknown').show();
                        }
                        if (objLocationType.Other == '1') {
                            $('#frmOtherType').show();
                            $('#frmOtherType').attr('checked', 'checked');
                            $('#lbLocationOther').show();
                        }
                        if (objLocationType.Intersection == '1') {
                            $('#frmIntersectionType').show();
                            $('#frmIntersectionType').attr('checked', 'checked');
                            $('#lbLocationIntersection').show();
                        }
                        if (objLocationType.Address == '1') {
                            $('#frmAddressType').show();
                            $('#frmAddressType').attr('checked', 'checked');
                            $('#lbLocationAddress').show();
                        }
                        hideshowLocationFields();

                        if (objLocationType.Address == '1') {
                            $('#pnlAddress').show();
                        } else if (objLocationType.Intersection == '1') {
                            $('#pnlIntersection').show();
                        } else if (objLocationType.Other == '1') {
                            $('#pnlOther').show();
                        }

                        //set attachment controls displaying
                        if (objResult.CaseTypeDetails.IsAttachmentAllowed == '1') {
                            $('#pnlUploadAttachment').show();
                        } else {
                            $('#pnlUploadAttachment').hide();
                        }

                        //show udef
                        var s = data.substring(data.split('|')[0].length + 1);
                        s = s.replace('__VIEWSTATE', 'hidethecontrol_1').replace('__VIEWSTATE', 'hidethecontrol_2');
                        $('#divUdefContainer').html(s);
                    }
                    $("#hidCaseTypeDetails").val(data);
                }
                $('#imgCaseTypeChanged').hide();
            })
        }
    }
    catch (error) {
        $('#imgCaseTypeChanged').hide();
    }
}

function SaveCaseUdefValue() {
    var bValid = true;
    var lstUdef = null;
    if ($("#hidCaseTypeDetails").val() != "") {
        lstUdef = eval('(' + $("#hidCaseTypeDetails").val().split('|')[0] + ')').CaseTypeDetails.CaseUdefList;
    }
    var sValue = '';

    if (!lstUdef) {
        return true; //no udef
    }

    for (var i = 0; i < lstUdef.length; i++) {
        if (bValid) {
            try {
                var oUdef = lstUdef[i];
                sValue += oUdef.Id + '|';
                if (oUdef.Type == 'CheckBox') {
                    if (document.getElementById(oUdef.ControlId).checked == true) {
                        sValue += 'Yes;';
                    } else {
                        sValue += 'No;';
                    }
                } else if (oUdef.Type == 'CheckBoxList' || oUdef.Type == 'RadioButton') {
                    var bSet = false;
                    for (var j = 0; j < oUdef.SubControlList.length; j++) {
                        if (document.getElementById(oUdef.SubControlList[j]).checked == true) {
                            sValue += j + ',';
                            bSet = true;
                        }
                    }
                    sValue += ';';
                    try {
                        if (!bSet && oUdef.IsRequired == 'True') {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'inline';
                            bValid = false;
                        } else {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'none';
                        }
                    } catch (error) { }
                } else if (oUdef.Type == 'List') {
                    var olist = document.getElementById(oUdef.ControlId);
                    for (var j = 1; j < olist.options.length; j++) {
                        if (olist.options[j].selected) {
                            sValue += (j - 1) + ',';
                        }
                    }
                    sValue += ';';
                    var s = $('#' + oUdef.ControlId).val();
                    try {
                        if ((!s || s == '') && oUdef.IsRequired == 'True') {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'inline';
                            bValid = false;
                        } else {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'none';
                        }
                    } catch (error) { }
                } else {
                    var s = $('#' + oUdef.ControlId).val();
                    sValue += escape(s) + ';';
                    try {
                        if ((!s || s == '') && oUdef.IsRequired == 'True') {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'inline';
                            bValid = false;
                        } else {
                            document.getElementById('lblErr' + oUdef.ControlId).style.display = 'none';
                        }
                    } catch (error) { }
                }
            } catch (error) { }
        }
    }
    $("#hidCaseUdefValues").val(sValue);
    return bValid;
}

function HideAllLocationControls() {
    $('#frmAddressType').hide();
    $('#lbLocationAddress').hide();
    $('#frmIntersectionType').hide();
    $('#lbLocationIntersection').hide();
    $('#frmOtherType').hide();
    $('#lbLocationOther').hide();
    $('#frmUnknownType').hide();
    $('#lbLocationUnknown').hide();

    $('#pnlAddress').hide();
    $('#pnlIntersection').hide();
    $('#pnlOther').hide();
}

function StoreHtmlToHidfield() {

    var sScript = $("#frmCaseDetails").html();
    $("#hidStorePageReload").val(sScript);
}

function CreateReq_GenCaptcha() {
    return { ReqType: 'GenCaptcha' };
}

function CreateReq_VlidateCaptcha() {
    var input = $("#txtCaptchaInput").attr('value');
    return { ReqType: input };
}

function onGenCaptcha() {
    $.get('GenCaptcha.aspx', CreateReq_GenCaptcha(), function(data) {
    if (data) {
            var s = data.substring(data.split('|')[0].length + 1);
            s = s.replace('__VIEWSTATE', 'hidethecontrol_3').replace('__VIEWSTATE', 'hidethecontrol_4');

            $('#divCaptchaPlaceHolder').html(s);
            $("#lbCaptchaError").hide();
            $("#imgCaptchaOK").hide();
            $("#lbCaptchaTipCaseSensitive").show();
            $("#txtCaptchaInput").attr('value','');
        }
    })
}

function onVlidateCaptcha() {
    var input = $("#txtCaptchaInput").attr('value');

    //alert(input);
    $.get('GenCaptcha.aspx', CreateReq_VlidateCaptcha(), function(data) {
        if (data) {
            var objResult = eval('(' + data.split('|')[0] + ')');
            if (objResult.result == '1') {
                $("#lbCaptchaError").hide();
                $("#imgCaptchaOK").show();
                $("#lbCaptchaTipCaseSensitive").hide();
              
            }
            else {
                $("#lbCaptchaError").show();
                $("#imgCaptchaOK").hide();
                $("#lbCaptchaTipCaseSensitive").hide();
            }
        }
    })

}

function onInputCaptchakeyup() {
    var input = $("#txtCaptchaInput").attr('value');
    if (input.length == 6) onVlidateCaptcha();
    else {
        $("#lbCaptchaError").hide();
        $("#imgCaptchaOK").hide();
        $("#lbCaptchaTipCaseSensitive").show();
        
    }
 }