﻿///     edunya.js
function ShowHideAllNew(ctl, arrayOfIds) {
    for (var i = 0; i < (arrayOfIds.length); i++) {
        var thelistid;
        thelistid = document.getElementById("lid_" + arrayOfIds[i]);
        if (arrayOfIds[i] == 0)
            continue;
        if (thelistid.style['display'] == 'none') {
            thelistid.style['display'] = '';
        }
        else {
            thelistid.style['display'] = 'none';
        }

    }

    ctl.style["display"] = 'block';
    ctl.style["visibility"] = 'visible';
    if (getText(ctl) == "رفع القائمة") {
        changeText(ctl, "المزيد..");
    }
    else {
        changeText(ctl, "رفع القائمة");
    }

    return false;
}
function getText(ctl) {
    if (document.all) {
        return ctl.innerText;
    }
    else {
        return ctl.textContent;
    }
}
function changeText(ctl, text) {
    if (document.all) {
        ctl.innerText = text;
    }
    else {
        ctl.textContent = text;
    }
}
function AttachEventToControl(ctl, eventName, method) {
    if (ctl.addEventListener) { //Mozila, NetScap, FF
        ctl.addEventListener(eventName.replace('on', ''), method, false);
    }
    else { //IE
        ctl.attachEvent(eventName, method);
    }
}
function RemoveEventFromControl(ctl, eventName, method) {

    if (ctl.removeEventListener) { //Mozila, NetScap, FF
        ctl.removeEventListener(eventName.replace('on',''), method, false);
    }
    else { //IE
        ctl.detachEvent(eventName, method);
    }
}

function ShowHideAll(ctl, count, showCount) {
    var li_id;
    var thelistid;
    for (var i = showCount; i < ctl.parentNode.childNodes.length - 1; i++) {
        if (ctl.parentNode.childNodes[i].nodeType == 1) {
            li_id = ctl.parentNode.childNodes[i].id;
            if (li_id == '')
                continue;
            thelistid = document.getElementById(li_id);
            if (thelistid != null) {
                if (thelistid.style['display'] == 'none') {
                    thelistid.style['display'] = '';
                }
                else {
                    thelistid.style['display'] = 'none';
                }
            }
        }
    }
    ctl.style.display = 'block';
    ctl.style.visibility = 'visible';
    if (ctl.innerText == "رفع القائمة") {
        ctl.innerText = "المزيد..";
    }
    else {
        ctl.innerText = "رفع القائمة";
    }
    return false; 
 }
function SetDefaultValue(ctl, val) {
    if (ctl.value == val) { ctl.value = ""; }
    else if (ctl.value.replace(/^\s+|\s+$/g, "") == "")
    { ctl.value = val; }
}
function showScroll() {
    document.body.scroll = "yes";
    document.body.style.overflow = "visible";
}

function IsUserLoggedIn() {
    
    if(document.getElementById('account_info') != null)
        return true;
    else {
        showLoginPopup();
        return false;
    }
}

function IsUserLoggedInBezaati() {

    if (document.getElementById('account_info') != null)
        return true;
    else {
        document.getElementById('hiddenFldBezaati').value = "1";
        showLoginPopupForBezaati();
        return false;
    }
}

function showLoginPopupForBezaati() {
    if (document.getElementById('divsearch') != null)
        document.getElementById('divsearch').style.visibility = 'hidden';
    document.getElementById('bigLoginDiv').style.display = 'block';
    document.getElementById('loginDiv').style.display = 'block';

    if (document.getElementById('hidPageNo') != null) {
        document.getElementById('hiddenPageNo').value = document.getElementById('hidPageNo').value;
    }
    else {
        document.getElementById('hiddenPageNo').value = "1";
    }
    document.getElementById("hiddenFldAdID").value = "";
    document.getElementById("hiddenFldcommentboxID").value = "";
    scrol = document.body.scroll; // TODO: whats this line for ??
    document.body.scroll = "no";
    document.body.style.overflow = "hidden";
    document.body.scrollIntoView(100);
}

function showLoginPopup() {
    if (document.getElementById('divsearch') != null) 
        document.getElementById('divsearch').style.visibility = 'hidden';
    document.getElementById('bigLoginDiv').style.display = 'block';
    document.getElementById('loginDiv').style.display = 'block';
    
    if (document.getElementById('hidPageNo') != null) {
        document.getElementById('hiddenPageNo').value = document.getElementById('hidPageNo').value;
    }
    else {
        document.getElementById('hiddenPageNo').value = "1";
    }
    document.getElementById("hiddenFldAdID").value = "";
    document.getElementById("hiddenFldBezaati").value = "0";
    scrol = document.body.scroll; // TODO: whats this line for ??
    document.body.scroll = "no";
    document.body.style.overflow = "hidden";
    document.body.scrollIntoView(100);
}
function showLoginPopupForAddToFavorites(adId) {

    document.getElementById("hiddenFldAdID").value = adId;
    document.getElementById("hiddenFldcommentboxID").value = "";
    document.getElementById("hiddenFldBezaati").value = "0";
    if (document.getElementById('hidPageNo') != null) {
        document.getElementById('hiddenPageNo').value = document.getElementById('hidPageNo').value;
    }
    else {
        document.getElementById('hiddenPageNo').value = "1";
    }
    document.getElementById('bigLoginDiv').style.display = 'block';
    document.getElementById('loginDiv').style.display = 'block';
    scrol = document.body.scroll; // TODO: whats this line for ??
    document.body.scroll = "no";
    document.body.style.overflow = "hidden";
    document.body.scrollIntoView(100);
}
var scrol = null;
function hideLogin() {
    if (document.getElementById('divsearch') != null) 
        document.getElementById('divsearch').style.visibility = 'visible';
    document.getElementById('tbEmailLoginPopup').value = '';
    document.getElementById('tbPasswordLoginPopup').value = '';
    document.getElementById('passwordDummy').style.display = '';
    document.getElementById('textDummy').style.display = '';
    if (document.getElementById('lblErrorMessageLoginPopup') != null) {
        document.getElementById('lblErrorMessageLoginPopup').style.display = 'none';
    }
    document.getElementById('warningImage').style.display = 'none';
    document.getElementById('password').style.display = 'none';
    document.getElementById('Email').style.display = 'none';
    document.getElementById('bigLoginDiv').style.display = 'none';
    document.getElementById('loginDiv').style.display = 'none';    
    showScroll();


}

function showLoginPopUpWithError() 
{
    document.getElementById('bigLoginDiv').style.display = 'block';
    document.getElementById('loginDiv').style.display = 'block';
    document.getElementById('Email').style.display = 'inline';
    document.getElementById('textDummy').style.display = 'none';
    if (document.getElementById('lblErrorMessageLoginPopup') != null)
    {
        document.getElementById('lblErrorMessageLoginPopup').style.display = 'block';
    }
    document.getElementById('warningImage').style.display = 'block';
    scrol = document.body.scroll;
    document.body.scroll = "no";
    document.body.style.overflow = "hidden";
    document.body.scrollIntoView(100);
 }
 function changeBox() {
     document.getElementById('passwordDummy').style.display = 'none';
     document.getElementById('password').style.display = '';
     document.getElementById('tbPasswordLoginPopup').focus();
 }
 function restoreBox() {
     if (document.getElementById('tbPasswordLoginPopup').value == '') {
         document.getElementById('passwordDummy').style.display = '';
         document.getElementById('password').style.display = 'none';
     }
 }
 function changeEmailBox() {

     document.getElementById('textDummy').style.display = 'none';
     document.getElementById('Email').style.display = '';


     if (document.getElementById('tbDummyEmail').value == "بريدك الإلكتروني") {

         document.getElementById('tbEmailLoginPopup').value = '';
     }
     else {
         document.getElementById('tbEmailLoginPopup').value = document.getElementById('tbDummyEmail').value;
     }
     document.getElementById('tbEmailLoginPopup').focus();

 }
 function restoreEmailBox() 
 {
     if (document.getElementById('tbEmailLoginPopup').value == '') 
     {
         document.getElementById('textDummy').style.display = '';
         document.getElementById('Email').style.display = 'none';
     }

 }

 function checkMandatoryFields(ctlId, errorMsgCtlId, errorMsg) {
     if ($.trim(document.getElementById(ctlId).value) == '') {
         changeText(document.getElementById(errorMsgCtlId),errorMsg);
         document.getElementById(errorMsgCtlId).style.display = 'block';
         try {
             AttachEventToControl(document.getElementById(ctlId),'onblur', checkMandatoryFields);
         }
         catch (e) { }
             return false;
         }
         else {
             document.getElementById(errorMsgCtlId).style.display = 'none';
             document.getElementById(ctlId).onblur = null;
             return true;
         }
     
     
 }

 

 
 
 function ValidateFields(regPtrn, ctlId, errorMsgCtlId, errorMsg) {
 var result = true;

 var re = new RegExp(regPtrn);
 var m = re.exec(document.getElementById(ctlId).value);

 if (m == null) {
 document.getElementById(errorMsgCtlId).value = errorMsg;
 changeText(document.getElementById(errorMsgCtlId),errorMsg);
 document.getElementById(errorMsgCtlId).style.display = 'inline';
 if (document.getElementById(ctlId).onblur == null)
             
 window.status = regPtrn;
 result = false;
 }
 else {
 document.getElementById(errorMsgCtlId).style.display = 'none';
         
 }
 
 return result;
 }

 function validateEmailAddress(ctlId, errorMsgLabelId, errorMsg) {
 
 if (!ValidateFields("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.(\\w+([-.]\\w+)*)", ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validateEmailAddress);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }

 }
 /*
 function compareFields(compareToCtlId, toCompareCtlId, errorMsgCtlId, errorMsg) {

 if (document.getElementById(compareToCtlId).value != document.getElementById(toCompareCtlId).value) {
 document.getElementById(errorMsgCtlId).value = errorMsg;
 document.getElementById(errorMsgCtlId).style.display = 'block';
         
 AttachEventToControl(document.getElementById(toCompareCtlId),'onblur', inValidateFields);
 return false;
 }
 else {

 document.getElementById(errorMsgCtlId).style.display = 'none';
 document.getElementById(toCompareCtlId).onblur = null;
 return true;
 }

 }
 function validateNumericField(ctlId, errorMsgLabelId, errorMsg) 
 {

 if (!ValidateFields('', ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validateNumericField);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }
 
 }
 function validateNameField(ctlId, errorMsgLabelId, errorMsg) {

 if (!ValidateFields('^(.{1,21} )*(.{1,21})$', ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validateNameField);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }

 }
 function validateMoneyField(ctlId, errorMsgLabelId, errorMsg) {

 if (!ValidateFields('', ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validateMoneyField);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }
 }

 function validatePhoneNumberField(ctlId, errorMsgLabelId, errorMsg) {

 if (!ValidateFields('', ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validatePhoneNumberField);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }
 }
 
 function validatePasswordField(ctlId, errorMsgLabelId, errorMsg){

 if (!ValidateFields('([a-zA-Z0-9~!@#$%^&*()]{4,14})*', ctlId, errorMsgLabelId, errorMsg)) {
 AttachEventToControl(document.getElementById(ctlId),'onblur', validatePasswordField);
 return false;
 }
 else {
 document.getElementById(ctlId).onblur = null;
 return true;
 }

 }
 */
 function validateLoginPopup() {

     document.getElementById('lblErrorMessageLoginPopup').style.display = 'none';

     if (!checkMandatoryFields('tbEmailLoginPopup', 'lblErrorMessageLoginPopup', 'يرجى إدخال البريد الإلكتروني')) {

         document.getElementById('warningImage').style.display = 'block';
         document.getElementById('lblErrorMessageLoginPopup').style.display = 'block';
         
         return false;
     }
     if (!checkMandatoryFields('tbPasswordLoginPopup', 'lblErrorMessageLoginPopup', 'يرجى إدخال كلمة المرور')) {
         document.getElementById('warningImage').style.display = 'block';
         document.getElementById('lblErrorMessageLoginPopup').style.display = 'block';
         return false;
     }
     if (!validateEmailAddress('tbEmailLoginPopup', 'lblErrorMessageLoginPopup', 'يرجى التحقق من البريد الإلكتروني')) {
         document.getElementById('warningImage').style.display = 'block';
         document.getElementById('lblErrorMessageLoginPopup').style.display = 'block';
         return false;
     }
     
     return true;
 }

 /*
    //TEAM: Added By Akbar Khan [DONT CHANGE IT]w
 */
    //EmailPattren is defined in MasterPage
 var EmailPattren = "^(?=.{5,254})(?:(?:\\\"[^\\\"]{1,62}\\\")|(?:(?!\\.)(?!.*\\.[.@-])[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~^.-]{1,64}))@(?:(?:\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\])|(?:(?!-)(?!.*-\\$)(?!.*-\\.)(?!.*[^n]--)(?!.*[^x]n--)(?!n--)(?!.*[^.]xn--)(?:[a-zA-Z0-9-]{1,63}\\.){1,127}(?:[a-zA-Z0-9-]{1,63})))$";
 //"(?:[aA-zZ0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[aA-zZ0-9!#$%&\'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?\\.)+[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[aA-zZ0-9-]*[aA-zZ0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
 //"(?:[aA-zZ0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[aA-zZ0-9!#$%&\'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?\\.)+[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[aA-zZ0-9-]*[aA-zZ0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
 //"(?:[aA-zZ0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[aA-zZ0-9!#$%&\'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?\.)+[aA-zZ0-9](?:[aA-zZ0-9-]*[aA-zZ0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[aA-zZ0-9-]*[aA-zZ0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]\|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])";
    
     //"\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.(\\w+([-.]\\w+)*)"; //TEAM: This should be changed to the New version.
    var PhoneNoPattren = "^[+]?[0-9\\-\\ ]+\\d{4,}$";
     var PricePattren = "\\d{1,3}((,\\d{3})|\\d{1,3})*(\\d+)?"; //[issue# 0003175]"\\d{1,3}((,\\d{3})|\\d{1,3})*((\\.)?\\d+)?"; //"(\\d)*|(\\d{1,3},(\\d{3},)*\\d{3}(\\.\\d{1,3})?|\\d{1,3}(\\.\\d{3})?)$"; //"^$|^\\d{1,3}((,\\d{3})|\\d{1,3})*((\\.)?\\d+)?";
     //var PricePattren = "\\d{1,3}((,\\d{3})|\\d{1,3})*((\\.)?\\d+)?"; //"(\\d)*|(\\d{1,3},(\\d{3},)*\\d{3}(\\.\\d{1,3})?|\\d{1,3}(\\.\\d{3})?)$";  //"^$|^\\d{1,3}((,\\d{3})|\\d{1,3})*((\\.)?\\d+)?";
     var PasswordPattren = "^.{4,}$"; //TEAM: This doesn't support Arabic password.
     var NamePattren = "^(.{1,21} )*(.{1,21})$";
     var AlphabeticPattren = "[^0-9]*$";//
 function GetAssociatedPattern(ptrnName)
 {
    if(ptrnName == 'EmailPattren')
        return EmailPattren;
        if(ptrnName == 'PhoneNoPattren')
            return PhoneNoPattren;
        if(ptrnName == 'PricePattren')
            return PricePattren;
                    if(ptrnName == 'PasswordPattren')
            return PasswordPattren;
    if(ptrnName == 'NamePattren')
        return NamePattren;
    if (ptrnName == "Compare" || ptrnName == "CmprGroupCntrls")
        return ptrnName;
    if (ptrnName == "AlphabeticPattren")
        return AlphabeticPattren;   
 };
 function checkRequiredFields() {
     var result = true;
     var emptyValue = '';
     var instructionText = '';
     try {
         for (var i = 0; i < requiredFields.length; i++) {
             if (document.getElementById(requiredFields[i]).type == 'select-one') {
                 emptyValue = '0';
                 instructionText = '0';
             }
             else
             {
                 emptyValue = '';
                 instructionText = 'هذا الحقل لعنوان نص الإعلان وليس لعنوان المكان';
             }
             var valueToCheck = document.getElementById(requiredFields[i]).value;
             valueToCheck = $.trim(valueToCheck);
             if (valueToCheck == emptyValue || valueToCheck == instructionText)
             {
                 document.getElementById(requiredFields[i] + "Required").style.display = 'inline';
                 try {
                     AttachEventToControl(document.getElementById(requiredFields[i]),'onblur', checkRequiredFields);
                 }
                 catch (e)
                 { }
                 result = false;
             }
             else {
                 document.getElementById(requiredFields[i] + "Required").style.display = 'none';
                 RemoveEventFromControl(document.getElementById(requiredFields[i]), 'onblur', checkRequiredFields)
                // document.getElementById(requiredFields[i]).onblur = null;
             }
         }
     }
     catch (e) {
         //alert(e.message);
     }
     return result;
 };
 var groupedCntrls;
 function moveOnlyGroupedControlsInArray() {
     var j = 0;
     for (var i = 0; i < validateFields.length; i++) {

         if (validateFields[i].split('|')[1] == 'CmprGroupCntrls') {
             groupedCntrls[j] = validateFields[i].split('|')[0].replace('-', ''); j++;
             groupedCntrls[j] = validateFields[i].split('|')[2]; j++;
         }
     }
 }
 function searchInGroupedControls(needle) { 
     for (var i = 0; i < groupedCntrls.length; i++) {
         if (groupedCntrls[i] == needle)
             return true;
     }
 }
 function inValidateFields() {

     groupedCntrls = new Array();
     moveOnlyGroupedControlsInArray();
     var result = true,
         resultGrpCntrls = true; //this is used to show only 1 msg at a time for grouped controls
     try {
         for (var i = 0; i < validateFields.length; i++) {

             var ctlName = validateFields[i].split('|')[0];
             var regPtrn = GetAssociatedPattern(validateFields[i].split('|')[1]);

             if (regPtrn == 'Compare')
             {
                 var ctlCmpr = validateFields[i].split('|')[2];
                 if ($.trim(document.getElementById(ctlName).value) != $.trim(document.getElementById(ctlCmpr).value)) {
                     document.getElementById(ctlName + "Compare").style.display = 'inline';
                     if (document.getElementById(ctlName).onblur == null)
                         try {
                         AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     }
                     catch (e)
                         { }
                     result = false;
                     continue;
                 }
                 else {
                     document.getElementById(ctlName + "Compare").style.display = 'none';
                     //document.getElementById(ctlName).onblur = null;
                     RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     continue;
                 }
             }
             // issue#2409 - checks the grouped controls (textbox & drpdown) i.e. if textbox has value then dropdown should be selected and vice versa
             else if (regPtrn == 'CmprGroupCntrls') {
                 var ctl1Type = validateFields[i].split('|')[3];
                 var ctl2Type = validateFields[i].split('|')[4];
                 var ctlCmpr = validateFields[i].split('|')[2];
                 ctlName *= -1;

                 var bCondition = false, cnd1st = false;
                 if (resultGrpCntrls) {
                     var ctl1Invalid = document.getElementById(ctlName + "Invalid");
                     var ctl2Invalid = document.getElementById(ctlCmpr + "Invalid");
                     // checking whether there are textboxes present on which regular expression validation has applied
                     if (ctl1Invalid != null && ctl2Invalid == null)
                         cnd1st = ctl1Invalid.style.display != 'inline';
                     else if (ctl1Invalid == null && ctl2Invalid != null)
                         cnd1st = ctl2Invalid.style.display != 'inline';
                     else if (ctl1Invalid != null && ctl2Invalid != null) {
                         var a = ctl1Invalid.style.display != 'inline';
                         cnd1st = ctl2Invalid.style.display != 'inline';
                         cnd1st = a || cnd1st;
                     }
                     else if (ctl1Invalid == null && ctl2Invalid == null)
                         cnd1st = true;

                     //compairing group controls by its values according to child controls controlTypes
                     if (ctl1Type == "TB" && ctl2Type == "DD")
                         bCondition = ($.trim(document.getElementById(ctlName).value) != '' && document.getElementById(ctlCmpr).value == '0') ||
                                    ($.trim(document.getElementById(ctlName).value) == '' && document.getElementById(ctlCmpr).value != '0');
                     else if (ctl1Type == "TB" && ctl2Type == "TB")
                         bCondition = ($.trim(document.getElementById(ctlName).value) != '' && $.trim(document.getElementById(ctlCmpr).value) == '') ||
                                    ($.trim(document.getElementById(ctlName).value) == '' && $.trim(document.getElementById(ctlCmpr).value) != '');
                     else if (ctl1Type == "DD" && ctl2Type == "TB")
                         bCondition = (document.getElementById(ctlName).value == '0' && $.trim(document.getElementById(ctlCmpr).value) != '') ||
                                    (document.getElementById(ctlName).value != '0' && $.trim(document.getElementById(ctlCmpr).value) == '');
                     else if (ctl1Type == "DD" && ctl2Type == "DD")
                         bCondition = (document.getElementById(ctlName).value == '0' && document.getElementById(ctlCmpr).value != '0') ||
                                    (document.getElementById(ctlName).value != '0' && document.getElementById(ctlCmpr).value == '0');
                 }

                 if (cnd1st && bCondition && resultGrpCntrls) {
                     document.getElementById(ctlName + "_Required").style.display = 'inline';
                     if (document.getElementById(ctlName).onblur == null)
                         try {
                         AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     }
                     catch (e)
                             { }
                     result = false;
                     continue;
                 }
                 else {
                     document.getElementById(ctlName + "_Required").style.display = 'none';
                     RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     continue;
                 }
             }

             //if (result && ctlName != -100) continue; /*-100 will always be EmailAddressID*/
             
             var re = new RegExp(regPtrn);
             var m = $.trim(document.getElementById(ctlName).value).match(re);  //re.exec(document.getElementById(ctlName).value);
             if ((m != null && m[0] == $.trim(document.getElementById(ctlName).value)) || $.trim(document.getElementById(ctlName).value) == '') 
             {
                 if (document.getElementById(ctlName + "Invalid") != null && document.getElementById(ctlName + "Invalid").style.display == 'inline') {
                     document.getElementById(ctlName + "Invalid").style.display = 'none';
                     //document.getElementById(ctlName).onblur = null;
                     RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                 }
                 continue;
             }

             if (m == null) { //
             
                 document.getElementById(ctlName + "Invalid").style.display = 'inline';
                 if (document.getElementById(ctlName).onblur == null) {
                     try {
                         AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     }
                     catch (e)
                     { }
                 }
                 if (searchInGroupedControls(ctlName) > -1) resultGrpCntrls = false;
                 result = false;
             }
             else if (m != null && m[0] != document.getElementById(ctlName).value) {
             
                 document.getElementById(ctlName + "Invalid").style.display = 'inline';
                 if (document.getElementById(ctlName + "Required") != null) document.getElementById(ctlName + "Required").style.display = 'none';
                 if (document.getElementById(ctlName).onblur == null) {
                     try {
                         AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields);
                     }
                     catch (e)
                         { }
                 }
                 if (searchInGroupedControls(ctlName) > -1) resultGrpCntrls = false;
                 result = false;
             }          
         }
     } catch (e) {
         //alert(e.message);
     }
     return result;
 }
 //THIS Will check 
 function showMessage(ctlName, validation) {
     if (validation == 'true') {
         
     }
 }
 function ValidateControl() {
     var reqRes = checkRequiredFields();
     var valRes = inValidateFields();
     if (reqRes == true && valRes == true) {
         DetachAllRequiredFieldControls();
         DetachAllValidateFieldControls();
         return true;
     }
     else
         return false;
 }

 /*
    END of Akbar work
 */

 function showAdvertiserMobileInfoPopUp(mobileInfo) {
     $('#hdnMobileInfo').text(mobileInfo);
     
     document.getElementById('bgDiv_detail').style.display = 'block';
     document.getElementById('popupDiv_detail_advertiser_mobile').style.display = 'block';
     scrol = document.body.scroll;
     document.body.scroll = "no";
     document.body.style.overflow = "hidden";
     document.body.scrollIntoView(100);
 }
 function hideAdvertiserMobileInfoPopUp() {
     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_advertiser_mobile').style.display = 'none';
     showScroll();
 }
 function showEmailToAdvertiserPopUp(fname, email, adId) {

     document.getElementById('hdnAdIDForSendEmailToAdvertiser').value = adId;
     document.getElementById('tbName').value = fname;
     
     document.getElementById('tbViewerEmail').value = email;
     document.getElementById('tbMessage').value = '';
     document.getElementById('tbPhone').value = '';
     document.getElementById('bgDiv_detail').style.display = 'block';
     document.getElementById('popupDiv_detail_reply_to_advertiser').style.display = 'block';    
     scrol = document.body.scroll;
     document.body.scroll = "no";
     document.body.style.overflow = "hidden";
     document.body.scrollIntoView(100);
 }
 function hideEmailToAdvertiserPopUpAfterSuccess() {

     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_reply_to_advertiser').style.display = 'none';
     if (document.getElementById('messageWindowHolder') != null) 
     {
         changeText(document.getElementById('userMessageLabel'), "تم إرسال الرسالة بنجاح");
         document.getElementById('messageWindowHolder').style.display = 'block';
     }
     showScroll(); //document.body.scroll = scrol;
    
 }
 function hideEmailToAdvertiserPopUp() {
  
     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_reply_to_advertiser').style.display = 'none';
   
     showScroll(); //document.body.scroll = scrol;
     hideAllMessagesOfValidation();
 }
 function hideEmailToAdvertiserPopUpAfterFailure() {
 
     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_reply_to_advertiser').style.display = 'none';
     if (document.getElementById('messageWindowHolder') != null) 
     {
         changeText(document.getElementById('userMessageLabel'),"Error has occurred... Please try again");//TODO:This message should be in Arabic
         document.getElementById('messageWindowHolder').style.display = 'block';
     }
     showScroll(); //document.body.scroll = scrol;

 }

 function ValidateFieldsABC(ctlId, errorMsgCtlId, errorMsg) {
     var result = true;

     var re = new RegExp(EmailPattren);
     var m = re.exec(document.getElementById(ctlId).value);

     if (m == null) {
         document.getElementById(errorMsgCtlId).value = errorMsg;
         changeText(document.getElementById(errorMsgCtlId),errorMsg);
         document.getElementById(errorMsgCtlId).style.display = 'inline';
         result = false;
     }
     else {
         document.getElementById(errorMsgCtlId).style.display = 'none';
     }

     return result;
 }

 function checkMandatoryFlds() {//ctlId, errorMsgCtlId, errorMsg) {

     var result = true;
     var emptyValue = '';
     for (var i = 0; i < _requiredFields.length; i++) {
         if (document.getElementById(_requiredFields[i]).type == 'select-one') {
             emptyValue = '0';
         }
         else
             emptyValue = '';
         if (document.getElementById(_requiredFields[i]).value == emptyValue) {
             document.getElementById(_requiredFields[i] + "Req").style.display = 'inline';
             changeText(document.getElementById(_requiredFields[i] + "Req"),_requiredErrorMsgs[i]);
             AttachEventToControl(document.getElementById(_requiredFields[i]),'onblur', checkMandatoryFlds);
             window.event.cancelBubble = true;
             result = false;
         }
         else {
             document.getElementById(_requiredFields[i] + "Req").style.display = 'none';
              RemoveEventFromControl(document.getElementById(_requiredFields[i]),'onblur', checkMandatoryFlds);
             //document.getElementById(_requiredFields[i]).onblur = null;
         }
     }
     return result;

 }


 function ValidateRegisterView() {
     var reqRes = checkMandatoryFlds();
     
//     if (reqRes == false)
//         return false;

     var valRes = inValidateFlds();
//     if (valRes == false)
//         return false;

     if (reqRes == false || valRes == false)
         return false;
     
     return true;
 }

 function inValidateFlds() {
     var result = true;
     for (var i = 0; i < _validateFields.length; i++) {

         var ctlName = _validateFields[i].split('|')[0];
         var regPtrn = GetAssociatedPattern(_validateFields[i].split('|')[1]);
         if (regPtrn == 'Compare') {
             var ctlCmpr = _validateFields[i].split('|')[2];
             if (document.getElementById(ctlName).value != document.getElementById(ctlCmpr).value) {
                 document.getElementById(ctlName + "2Val").style.display = 'inline';
                 if (document.getElementById(ctlName).onblur == null)
                     AttachEventToControl(document.getElementById(ctlName),'onblur', inValidateFlds);
                 window.event.cancelBubble = true;
                 result = false;
                 continue;
             }
             else {
                 document.getElementById(ctlName + "2Val").style.display = 'none';
                 RemoveEventFromControl(document.getElementById(ctlName),'onblur', inValidateFlds); 
                 //document.getElementById(ctlName).onblur = null;
                 continue;
             }
         }
         var re = new RegExp(regPtrn);
         var m = re.exec(document.getElementById(ctlName).value);

         if (m == null) { //
             document.getElementById(ctlName + "2Val").style.display = 'inline';
             changeText(document.getElementById(ctlName + "2Val"),_validateErrorMsgs[i]);
             if (document.getElementById(ctlName).onblur == null)
                 AttachEventToControl(document.getElementById(ctlName),'onblur', inValidateFlds);
             window.status = regPtrn;
             result = false;
         }
         else {
             document.getElementById(ctlName + "2Val").style.display = 'none';
             RemoveEventFromControl(document.getElementById(ctlName),'onblur', inValidateFlds);
             //document.getElementById(ctlName).onblur = null;
         }
     }
     return result;
 }

/* Register view functions end */

 // this method is used to fire the default button
 // target parameter is the control id
 function FireDefaultButton(event, target) {
     if (event.keyCode == 13) {
         var src = event.srcElement || event.target;
         if (!src || (src.tagName.toLowerCase() != "textarea")) {
          
             var defaultButton;
             defaultButton = document.getElementById(target);
             if (defaultButton == null)
                 defaultButton = document.all[target];

             if (defaultButton && typeof (defaultButton.click) != "undefined") {
                 defaultButton.click();
                 event.cancelBubble = true;
                 if (event.stopPropagation) event.stopPropagation();
                 return false;
             }
         }
     }
     return true;
 }
 function hideEmailToFriendPopUpAfterSuccess() {

     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_send_to_friend').style.display = 'none';
     if (document.getElementById('messageWindowHolder') != null) {
         changeText(document.getElementById('userMessageLabel'), "تم إرسال الرسالة بنجاح");
         document.getElementById('messageWindowHolder').style.display = 'block';
     }
     showScroll(); //document.body.scroll = scrol;
     hideAllMessagesOfValidation();
 }
 function hideEmailToFriendPopUp() {
   
     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_send_to_friend').style.display = 'none';
     showScroll(); //document.body.scroll = scrol;
     hideAllMessagesOfValidation();
 }
 function hideEmailToFriendPopUpAfterFailure() {

     document.getElementById('bgDiv_detail').style.display = 'none';
     document.getElementById('popupDiv_detail_send_to_friend').style.display = 'none';
     if (document.getElementById('messageWindowHolder') != null)
     {
         changeText(document.getElementById('userMessageLabel'),"Error has occurred... Please try again");
         document.getElementById('messageWindowHolder').style.display = 'block';
     }
     showScroll(); //document.body.scroll = scrol;

 }
 function showEmailToFriendPopUp(fname, email, adId) {

     document.getElementById('hdnAdIDForEmailToFriend').value = adId;
     document.getElementById('tbNameOfSender').value = fname;
     document.getElementById('tbEmailOfSender').value = email;
     document.getElementById('tbMessageToFriend').value = '';
     document.getElementById('tbEmailOfReceiver').value = '';
     document.getElementById('bgDiv_detail').style.display = 'block';
     document.getElementById('popupDiv_detail_send_to_friend').style.display = 'block';
     scrol = document.body.scroll;
     document.body.scroll = "no";
     document.body.style.overflow = "hidden";
     document.body.scrollIntoView(100);
     hideAllMessagesOfValidation();
 }
 function changeFavoritesButton(advertId) {

     var buttonId = 'btnAddToFav_' + advertId;
     document.getElementById(buttonId).style.display = 'none';
     document.getElementById('addedToFavBtnHidden_'+advertId).style.display = 'block';
 }
 function ConfirmDelete() {
     
     var toBeReturned = confirm(" هل تريد إزالة إعلانك من الموقع تماما ؟");
     return toBeReturned;
        }
        function hideAllMessagesOfValidation() {

            for (var i = 0; i < requiredFields.length; i++) {
                document.getElementById(requiredFields[i] + "Required").style.display = 'none'
            }
            for (var i = 0; i < validateFields.length; i++) {
                var ctlName = validateFields[i].split('|')[0];
                try {
                    document.getElementById(ctlName + "Invalid").style.display = 'none';
                }
                catch (e) { }
            }

            for (var i = 0; i < requiredFields2.length; i++) {
                document.getElementById(requiredFields2[i] + "Required").style.display = 'none'
            }
            for (var i = 0; i < validateFields2.length; i++) {
                var ctlName = validateFields2[i].split('|')[0];
                try {
                    document.getElementById(ctlName + "Invalid").style.display = 'none';
                }
                catch (e) { }
            }

        }


        /*
            This a Temprory Solution I will resolve it soon\
            This is because we have two arrays of validation  
        */
        function checkRequiredFields2() {
            var result = true;
            var emptyValue = '';
            var instructionText = '';
            try {
                for (var i = 0; i < requiredFields2.length; i++) {
                    if (document.getElementById(requiredFields2[i]).type == 'select-one') {
                        emptyValue = '0';
                        instructionText = '0';
                    }
                    else {
                        emptyValue = '';
                        instructionText = 'هذا الحقل لعنوان نص الإعلان وليس لعنوان المكان';
                    }
                    var valueToCheck = document.getElementById(requiredFields2[i]).value;
                    valueToCheck = $.trim(valueToCheck);
                    if (valueToCheck == emptyValue || valueToCheck == instructionText) {
                        document.getElementById(requiredFields2[i] + "Required").style.display = 'inline';
                        try {
                            AttachEventToControl(document.getElementById(requiredFields2[i]), 'onblur', checkRequiredFields2);
                        }
                        catch (e)
                 { }
                        result = false;
                    }
                    else {
                        document.getElementById(requiredFields2[i] + "Required").style.display = 'none';
                        RemoveEventFromControl(document.getElementById(requiredFields2[i]), 'onblur', checkRequiredFields2)
                        // document.getElementById(requiredFields[i]).onblur = null;
                    }
                }
            }
            catch (e) {
                //alert(e.message);
            }
            return result;
        };
        function inValidateFields2() {
            var result = true;
            try {
                for (var i = 0; i < validateFields2.length; i++) {

                    var ctlName = validateFields2[i].split('|')[0];
                    var regPtrn = GetAssociatedPattern(validateFields2[i].split('|')[1]);

                    if (regPtrn == 'Compare') {
                        var ctlCmpr = validateFields2[i].split('|')[2];
                        if ($.trim(document.getElementById(ctlName).value) != $.trim(document.getElementById(ctlCmpr).value)) {
                            document.getElementById(ctlName + "Compare").style.display = 'inline';
                            if (document.getElementById(ctlName).onblur == null)
                                try {
                                AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields2);
                            }
                            catch (e)
                         { }
                            result = false;
                            continue;
                        }
                        else {
                            document.getElementById(ctlName + "Compare").style.display = 'none';
                            //document.getElementById(ctlName).onblur = null;
                            RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields2);
                            continue;
                        }
                    }

                    var re = new RegExp(regPtrn);
                    var m = $.trim(document.getElementById(ctlName).value).match(re);  //re.exec(document.getElementById(ctlName).value);
                    if ((m != null && m[0] == $.trim(document.getElementById(ctlName).value)) || $.trim(document.getElementById(ctlName).value) == '') {
                        if (document.getElementById(ctlName + "Invalid") != null && document.getElementById(ctlName + "Invalid").style.display == 'inline') {
                            document.getElementById(ctlName + "Invalid").style.display = 'none';
                            //document.getElementById(ctlName).onblur = null;
                            RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields2);
                        }
                        continue;
                    }

                    if (m == null) { //

                        document.getElementById(ctlName + "Invalid").style.display = 'inline';
                        if (document.getElementById(ctlName).onblur == null) {
                            try {
                                AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields2);
                            }
                            catch (e)
                     { }
                        }
                        result = false;
                    }
                    else if (m != null && m[0] != document.getElementById(ctlName).value) {

                        document.getElementById(ctlName + "Invalid").style.display = 'inline';
                        document.getElementById(ctlName + "Required").style.display = 'none';
                        if (document.getElementById(ctlName).onblur == null) {
                            try {
                                AttachEventToControl(document.getElementById(ctlName), 'onblur', inValidateFields2);
                            }
                            catch (e)
                         { }
                        }
                        result = false;
                    }
                }
            } catch (e) {
                //alert(e.message);
            }
            return result;
        }
        //THIS Will check 
        function showMessage(ctlName, validation) {
            if (validation == 'true') {

            }
        }
        //CR: major code review needed here..
        //TODO: We need to remove this method. only there for reply to advertiser control
        function ValidateControl2() {
  
            var reqRes = checkRequiredFields2();
            var valRes = inValidateFields2();
            if (reqRes == true && valRes == true)
                return true;
            else
                return false;
        }

        /*
            End of duplicated 
        */


        function toggleMyAdsMenuDiv() {
            if ($("#menuDiv")[0].style.display == '') {
                $("#menuDiv")[0].style.display = 'none';
            }
            else {
                $("#menuDiv")[0].style.display = '';
            }

        }

        function CheckMaxLengthOnKeyDown(ctl) {
            if (event.type == "keydown") {
                var charCode = null;
                if (event.charCode) {
                    charCode = event.charCode;
                }
                else {
                    charCode = event.keyCode;
                }
                if (charCode == 8 || charCode == 46 || charCode == 37 || charCode == 38 || charCode == 39
                    || charCode == 40 || charCode == 27)
                    return true;
            }
            return (parseInt(ctl.value.length) < parseInt(ctl.getAttribute("maxLength")));
        }
        
        function CheckMaxLengthOnPaste(ctl) {
            var maxLength = parseInt(ctl.getAttribute("maxLength"));
            var str = window.clipboardData.getData("Text");
            if (str != null) {
                window.clipboardData.setData("Text", str.substring(0, maxLength));
            }
            return true;
        }

        function removeMessageOnFocus(ctlId) {

            if ($("#" + ctlId).val() == 'هذا الحقل لعنوان نص الإعلان وليس لعنوان المكان') {
                $("#" + ctlId).val('');
                $("#" + ctlId).css("color", "#0690b3");
                
            }
                
        }

        function putMessageOnFocusLost(ctlId) 
        {
           
            if ($("#"+ctlId).val() == '') {
                $("#" + ctlId).val('هذا الحقل لعنوان نص الإعلان وليس لعنوان المكان');
                $("#" + ctlId).css("color", "#ff8a00");
            }

        }

        function DetachAllRequiredFieldControls() {

            try {
                for (var i = 0; i < requiredFields.length; i++) {

                    RemoveEventFromControl(document.getElementById(requiredFields[i]), 'onblur', checkRequiredFields);

                }
            }
            catch (e) {
                //alert(e.message);
            }

        }

        function DetachAllValidateFieldControls() {

            try {
                for (var i = 0; i < validateFields.length; i++) {

                    var ctlName = validateFields[i].split('|')[0];
                    RemoveEventFromControl(document.getElementById(ctlName), 'onblur', inValidateFields);

                }
            }
            catch (e) {
                //alert(e.message);
            }

        }

        /* For AdComments--------------------*/
        
        function RestrictMaxLengthOnPaste(ctl) {

            var maxLength = parseInt(ctl.getAttribute("maxLength"));
            var ctlValueLength = parseInt(ctl.value.length);
            var remAllowedLength = maxLength - ctlValueLength;
            var str = window.clipboardData.getData("Text");

            if (parseInt(ctl.value.length) < parseInt(ctl.getAttribute("maxLength"))) {

                if (str != null) {
                    if (str.length < remAllowedLength) {
                        return true;
                    }
                    else {
                        window.clipboardData.setData("Text", str.substring(0, remAllowedLength));
                        return true;
                    }

                }
            }
            else {
                return false;
            }

        }

        function trimAll(sString) {
            while (sString.substring(0, 1) == ' ') {
                sString = sString.substring(1, sString.length);
            }
            while (sString.substring(sString.length - 1, sString.length) == ' ') {
                sString = sString.substring(0, sString.length - 1);
            }
            return sString;
        }

        function ShowComments(pageNo) {
            
            if (pageNo == 0) {
                document.getElementById('maincommentdiv').style.display = 'none';
                document.getElementById("hiddenCommentsSectionState").value = "0";

            }
            else {
                GetAdComments(pageNo);
                if (document.getElementById('hidPageNo') != null) {
                    document.getElementById('hiddenPageNo').value = document.getElementById('hidPageNo').value;
                }
                else {
                    document.getElementById('hiddenPageNo').value = "1";
                }
                document.getElementById('maincommentdiv').style.display = 'inline';
                document.getElementById("hiddenCommentsSectionState").value = "1";
                if (document.getElementById('spanafterclick') != null) {
                    document.getElementById('spanafterclick').style.display = 'inline';
                    document.getElementById('linkshowcomment').style.display = 'none';
                }
                else {
                    var idOfCommentsListingDiv = "#CommentsListingDiv";
                    $(idOfCommentsListingDiv).html("");
                    $(idOfCommentsListingDiv).hide();
                }


            }
        }

        function ShowAddCommentBox(divtoshow, divtohide, txtareatoclear) {

            if (document.getElementById('account_info') != null) {
                document.getElementById(divtoshow).style.display = 'inline';
                document.getElementById(divtohide).style.display = 'none';
                document.getElementById(txtareatoclear).value = "";

                if (txtareatoclear == 'commenttextupper') {
                    document.getElementById('commenttextlower').focus();
                }
                else {
                    document.getElementById('commenttextupper').focus();
                }
                document.getElementById(txtareatoclear + 'errmsg').style.display = "none";
                return true;
            }
            else {

                document.getElementById('hiddenFldcommentboxID').value = divtoshow;

                showLoginPopup();
                return false;
            }
        }


        function HideAddCommentBox(divtohide, txtareatoclear) {

            if (document.getElementById('account_info') != null) {

                document.getElementById(divtohide).style.display = 'none';
                document.getElementById(txtareatoclear).value = "";
                document.getElementById(txtareatoclear + 'errmsg').style.display = "none";
                document.getElementById(txtareatoclear + 'errmsgmax').style.display = "none";

                return true;
            }
            else {
                showLoginPopup();
                return false;
            }
        }

        function SubmitAddCommentBox(txtareatosubmit, errmsgctlid) {

            var maxLength = parseInt(document.getElementById(txtareatosubmit).getAttribute("maxLength"));
            if (trimAll(document.getElementById(txtareatosubmit).value) == "") {

                document.getElementById(errmsgctlid).style.display = "inline";
                document.getElementById(errmsgctlid+"max").style.display = "none";
            }
            else if (document.getElementById(txtareatosubmit).value.length > maxLength) {
                document.getElementById(errmsgctlid).style.display = "none";
                document.getElementById(errmsgctlid + "max").style.display = "inline";
            }
            else
            {
                var value = trimAll(document.getElementById(txtareatosubmit).value);
                document.getElementById(txtareatosubmit).value = value;
                document.forms["frmComment"].submit();
            }

        }

        function ScrollToDiv(divID) {
            if ($.browser.mozilla) {
                window.location = divID;
            }
            else {
                $("#scrolltodiv").attr("href", divID);
                document.getElementById("scrolltodiv").click();
                $("#scrolltodiv").click();
            }
        }
        
        /*----------------------------*/