var Common =
{
  isInteger: function (s) {
    var i;
    for (i = 0; i < s.length; i++) {
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
    }
    return true;
  },

  isNumber: function (str) {
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
      return false;
    }
    return true;
  },

  trim: function (str, chars) {
    return Common.ltrim(Common.rtrim(str, chars), chars);
  },

  ltrim: function (str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
  },

  rtrim: function (str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
  },

  IsAnyRBSelected: function (name, msg, actionMsg) {
    var isSelected = 0;
    var elem = document.getElementsByTagName("input");
    var arr = new Array();
    var i;
    for (i = 0; i < elem.length; i++) {
      if (elem[i].type == "radio") {
        att = elem[i].getAttribute("name");
        if (att == name) {
          if (document.getElementById(elem[i].id).checked == true) {
            isSelected = 1;
            break;
          }
        }
      }
    }
    if (isSelected == 0) {
      alert(msg);
      return false;
    }
    else {
      if (actionMsg != "") {
        return confirm(actionMsg);
      }
      else {
        return true;
      }
    }
  },

  IsAnyCBSelected: function (name, msg, actionMsg, ddlvalue) {
    if (ddlvalue != -1) {
      var isSelected = 0;
      var elem = document.getElementsByTagName("input");
      var arr = new Array();
      var i;
      for (i = 0; i < elem.length; i++) {
        if (elem[i].type == "checkbox") {
          att = elem[i].getAttribute("name");
          if (att == name) {
            if (document.getElementById(elem[i].id).checked == true) {
              isSelected = 1;
              break;
            }
          }
        }
      }
      if (isSelected == 0) {
        alert(msg);
        return false;
      }
      else {
        if (actionMsg != "") {
          return confirm(actionMsg);
        }
        else {
          return true;
        }
      }
    }
    else {
        alert('Please select atleast one record');
      return false;
    }
  },

  SelectAllCB: function (name, cbMain) {
    var elem = document.getElementsByTagName("input");
    var arr = new Array();
    var i;
    for (i = 0; i < elem.length; i++) {
      if (elem[i].type == "checkbox") {
        att = elem[i].getAttribute("name");
        if (att == name) {
          if (cbMain.checked == true) {
            document.getElementById(elem[i].id).checked = true;
          }
          else {
            document.getElementById(elem[i].id).checked = false;
          }
        }
      }
    }
  },

  CheckSelectAllCB: function (name, cbMainId) {
    var elem = document.getElementsByTagName("input");
    var cbMainId = document.getElementById(cbMainId);
    var isChecked = true;
    var arr = new Array();
    var i;
    for (i = 0; i < elem.length; i++) {
      if (elem[i].type == "checkbox") {
        att = elem[i].getAttribute("name");
        if (att == name) {
          if (elem[i].checked == false) {
            isChecked = false;
            break;
          }
        }
      }
    }
    cbMainId.checked = isChecked;
  },

  ValidateTextLength: function (txtbox, length) {
    if (txtbox.value.length > length) {
      txtbox.value = txtbox.value.substring(0, length);
    }
  }
}


$(document).ready(function () {
    $("input[placeholder],textarea[placeholder]").each(function () {                    
        if ($(this).attr("type") == "password") {
            var prevInput = $(this).prev("input");

            $(prevInput).val($(this).attr("placeholder"));
            if ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")) {
                $(this).hide();
                $(prevInput).show();
            }
            else {
                $(prevInput).hide();
            }

            $(prevInput).focus(function () {
                $(this).hide();
                $(this).next("input[type=password]").val("");
                $(this).next("input[type=password]").show();
                $(this).next("input[type=password]").focus();
            });

            $(this).blur(function () {
                if ($(this).val() == $(this).attr("placeholder") || $(this).val() == "") {
                    $(this).hide();
                    $(this).prev("input").show();
                }
            });
        }
        else {
            if ($(this).val() == "") {
                $(this).val($(this).attr("placeholder"));
            }
            if ($(this).val() == $(this).attr("placeholder")) {
                $(this).addClass('light_color');
            }

            $(this).focus(function () {
                if ($(this).val() == $(this).attr("placeholder")) {
                    $(this).val('');
                    $(this).removeClass('light_color');
                    $(this).select();
                }
            });

            $(this).change(function () {
                $(this).removeClass('light_color');
            });

            $(this).blur(function () {
                if ($.trim($(this).val()) == '') {
                    $(this).val($(this).attr("placeholder"));
                    $(this).addClass('light_color');
                }
            });
        }
    });
});

//Set Forgot Password Popup Title
function SetPopupTitle(title) {
    parent.$("div.fancy_bg_n").children("h1").html(title);
}

function OpenFancyBox(url, title, width, height) {
    $(document).ready(function () {
        $("<a href='" + url + "' title='" + title + "' />").fancybox({
            'frameWidth': width,
            'frameHeight': height
        }).click();
    });
}


function ShowAlert(title, msg, width, height) {
    if ($('body #AlertBox').size() > 0) {
        $('body #AlertBox').remove();
    }

    var divWarning = $('<div id="AlertBox" class="hide"><div class="popup_msg">' + msg + '</div><div class="btn_container" onclick=\"$(\'#fancy_close\').click();\" style="width:119px; cursor:pointer;"></div></div>');
    divWarning.appendTo('body');

    //Show Fancy Box
    $("<a href='#AlertBox' title='" + title + "' />").fancybox({
        'frameWidth': width,
        'frameHeight': height
    }).click();
}



function BindDefaultLinkButtons(main_lnkSave, containerId, DefaultButtonId, validationGroup) {
    $('#' + containerId + ' input').keypress(function (e) {
        if (e.keyCode == 13) {
            var flag = true;

            if ($('#' + main_lnkSave).hasClass("aspNetDisabled")) {
                flag = false;
            }

            if (flag == true) {
                if ($('#ui-datepicker-div').length) {
                    if (($("#ui-datepicker-div").is(":visible") && $("#ui-datepicker-div").html() != "")) {
                        // datepicker is not open. you need the second condition because it starts off as visible but empty 
                        flag = false;
                    }
                }
            }

            //            if (flag == true) {
            //                if ($('.ac_results').length) {
            //                    $(".ac_results").each(function () {
            //                        if ($(this).is(":visible") && $(this).html() != "") {
            //                            flag = false;
            //                        }
            //                    });
            //                }
            //            }
            if (flag == true) {
                if (Page_ClientValidate(validationGroup)) {
                    __doPostBack(DefaultButtonId, '');
                }
                else {
                    try {
                        ShowMsgPanel();
                    } catch (err) {
                    }
                }
            }
        }
    });
}

//Add favorites Properties
//moved from search.js
function AddToFavorites(PropertyId) {
    var URL = "Search.aspx/AddToFavorites";
    var options = {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: URL,
        data: '{propertyId:"' + PropertyId + '"}',
        success: function (msg) {
            if (msg.d == "Failed") {
                ShowAlert('Warning', '<ul><li>Please login first.</li></ul>', '350', '70');
            }
            else if (msg.d == "FavoritePropertyAlreadyExists") {
                ShowAlert('Add to favorites', '<ul><li>Already added.</li></ul>', '350', '70');
            }
            else if (msg.d == "Success") {
                ShowAlert('Add to favorites', '<ul><li>Successfully added.</li></ul>', '350', '70');
            }

        },
        error: function (msg) {
            alert("error: " + msg);
        }
    };
    $.ajax(options);
}

//ExpediaAddToFavorites Properties
//moved from search.js
function ExpediaAddToFavorites(PropertyId) {
    var URL = "Search.aspx/ExpediaAddToFavorites";
    var options = {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: URL,
        data: '{propertyId:"' + PropertyId + '"}',
        success: function (msg) {
            if (msg.d == "Failed") {
                ShowAlert('Warning', '<ul><li>Please login first.</li></ul>', '350', '70');
            }
            else if (msg.d == "FavoritePropertyAlreadyExists") {
                ShowAlert('Add to favorites', '<ul><li>Already added.</li></ul>', '350', '70');
            }
            else if (msg.d == "Success") {
                ShowAlert('Add to favorites', '<ul><li>Successfully added.</li></ul>', '350', '70');
            }

        },
        error: function (msg) {
            alert("error: " + msg);
        }
    };
    $.ajax(options);
}


function AddToFavoritesBySearch(PropertyId, url) {
    var URL = url+"/AddToFavorites";
    var options = {
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: URL,
        data: '{propertyId:"' + PropertyId + '"}',
        success: function (msg) {
            if (msg.d == "Failed") {
                ShowAlert('Warning', '<ul><li>Please login first.</li></ul>', '350', '70');
            }
            else if (msg.d == "FavoritePropertyAlreadyExists") {
                ShowAlert('Add to favorites', '<ul><li>Already added.</li></ul>', '350', '70');
            }
            else if (msg.d == "Success") {
                ShowAlert('Add to favorites', '<ul><li>Successfully added.</li></ul>', '350', '70');
            }

        },
        error: function (msg) {
            alert("error: " + msg);
        }
    };
    $.ajax(options);
}

function ValidateSearch() {
    var msg = '<ul>';

    if (Date.parse($('#txtFromDate').val()) > Date.parse($('#txtToDate').val())) {
        msg += '<li>Please enter the valid dates</li>';
    }

    msg += '</ul>'

    if (msg != '<ul></ul>') {
        ShowAlert('Warning', msg, '350', '130');
        return false;
    }
    else {
        return true;
    }
}

