function voidFunction() {}

function showModalMessageBox(msg, title, callback) {
	callback = (typeof(callback)==typeof(voidFunction))?callback:voidFunction;
	var dialogDom = $("<div>"+msg+"</div>");
	dialogDom.attr("id", "__common_modal_msg_box__");
	dialogDom.dialog({
		title: title,
		close: callback,
		buttons: {
			"OK": function() {
				dialogDom.dialog("close");
				callback();
			}
		},
		modal: true,
		overlay: {
			opacity: 0.5,
			background: "black"
		}
	});
}

function showModelPromoteBox(msg, title, callback){
  var dialogDom = $("<div>" + msg+ "<div>");
  dialogDom.attr("id", "__commom_modal_promote_box__");
  dialogDom.dialog({
		title: title,
		buttons: {
			"Save": function() {
				dialogDom.dialog("close");
				callback();
			},
			"Cancel": function() {
				dialogDom.dialog("close");
			}
		},
		modal: true,
		overlay: {
			opacity: 0.5,
			background: "black"
		}
	});
}

function showModalConfirmBox(msg, title, callback) {
	var dialogDom = $("<div>"+msg+"</div>");
	dialogDom.attr("id", "__common_modal_confirm_box__");
	dialogDom.dialog({
		title: title,
		buttons: {
			"Yes": function() {
				dialogDom.dialog("close");
				callback();
			},
			"No": function() {
				dialogDom.dialog("close");
			}
		},
		modal: true,
		overlay: {
			opacity: 0.5,
			background: "black"
		}
	});
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function isValidEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}

function isValidUrl(url){
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(url);
}


function checkAll(){
     
     $("input[type='checkbox']").each(function(){
         this.checked = true;
     });
}

function uncheckAll(){
	     //uncheck all selected business
     $("input[type='checkbox']").each(function(){
        this.checked = false;
     });
}

/**
 * function to validate integer
 * @param {} s
 * @return {Number}
 */
function isInteger (s)
   {
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
   }
   
/**
 * Check if variable is empty
 * @param {} e
 * @return {}
 */
function isEmpty(e){
	return ((e == null) || (e.length == 0))
}

/**
 * Check if variable is integer or not
 * @param {} c
 * @return {}
 */
function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function ajaxLoading(element){
	element.html('<img style="margin-left: 5px; margin-top: 5px; " src="/img/ajax_loader.gif" />');
}

/**
 * Open popup window
 */

function openPopupWindow(url, width, height){
	var newwindow;
	newwindow = window.open(url, 'tester', 'height=' +height +',width='+ width + ',resizable=yes, scrollbars=yes, toolbar=yes, status=yes');
	if(window.focus){newwindow.focus()};
}

/**
* extract from jVal, because it make this method private.
* @param {Object} selectElement
* @param {Object} message
* @param {Object} autoHide
* @param {Object} styleType
*/
function showWarning(selectElement, message, autoHide, styleType){
   $(selectElement).each(function(ind){
       var par = $(this).parent().get(0);
       $(this).css({
           'margin-top': '',
           'position': '',
           'borderColor': 'red'
       }).parent().find('.jfVal').stop().remove();
       $(par).find('.jValRelWrap').remove().end().append('<div class="jValRelWrap" style="display:none;"></div>').find('.jValRelWrap').append($(this).clone());
       var fieldWidth = $(par).find('.jValRelWrap').width(), fieldHeight = $(par).find('.jValRelWrap').height();
       $(par).find('.jValRelWrap').css({
           'width': fieldWidth,
           'height': fieldHeight
       }).empty();
       var paddedHeight = (fieldHeight + ($.fn.jVal.defaultPadding * 2));
       $(this).before('<div class="jfVal' + (styleType ? ' jfVal' + styleType : '') + '" style="left:' + ($(this).position().left - $.fn.jVal.defaultPadding - $.fn.jVal.defaultBorderWidth) + 'px; ' +
       'top:' +
       ($(this).position().top - $.fn.jVal.defaultPadding - $.fn.jVal.defaultBorderWidth + $.fn.jVal.IETopNudge) +
       'px;">' +
       ((styleType == 'pod') ? '<div class="spacerBorder" style="height:' + paddedHeight + 'px;">' : '') +
       '<div class="spacer' +
       (styleType ? ' spacer' + styleType : '') +
       '" style="height:' +
       paddedHeight +
       'px;"></div>' +
       ((styleType == 'pod') ? '</div>' : '') +
       '<div class="icon' +
       (styleType ? ' icon' + styleType : '') +
       '" style="height:' +
       paddedHeight +
       'px;"><div class="iconbg"></div></div>' +
       '<div class="content' +
       (styleType ? ' content' + styleType : '') +
       '" style="height:' +
       paddedHeight +
       'px; line-height:' +
       paddedHeight +
       'px;">' +
       message +
       '</div>' +
       '</div>');
       var spacerWidth = fieldWidth + ($.fn.jVal.defaultPadding * 2) + 8;
       $(par).find(styleType == 'pod' ? '.spacerBorder' : '.jfVal').css('padding', parseInt($.fn.jVal.defaultBorderWidth) + 'px').corner("round tr br 3px");
       $(par).find('.jfVal').width(spacerWidth + $(par).find('.icon').width() + $(par).find('.content').width() + $.fn.jVal.defaultPadding + $.fn.jVal.defaultBorderWidth);
       if (autoHide) {
           $(par).find('.spacer').width(spacerWidth).animate({
               'opacity': 0.95
           }, 2000).animate({
               'width': 0
           }, 200);
           $(par).find('.jfVal').css({
               'opacity': 0.93,
               'borderWidth': 0
           }).animate({
               'borderWidth': 0
           }, 2000).animate({
               'opacity': 0
           }, 200, function(){
               $(this).remove();
           });
           $(this).stop().animate({
               'opacity': 0.95
           }, 2000, function(){
               $(this).css('borderColor', '');
           });
       }
       else {
           $(par).find('.spacer').width(0).animate({
               'width': spacerWidth
           }, 200);
           $(par).find('.jfVal').css('opacity', 0).animate({
               'opacity': 0.95
           }, 400);
       }
       $(this).css(($.browser.msie) ? {
           'margin-top': 1,
           'position': 'absolute'
       } : {
           'position': 'absolute'
       }).parent().find('.jValRelWrap').css('display', 'block');
   });
};

var changeAd = function() {
	adId = $("#select_ads").val();
	$.ajax({
    type: "POST",
    url: gBaseUrl + "/index/changeadvertiser",
    cache: false,
    dataType: "json",
    data: {newAd:adId},
    success: function(reply, status){
      if (reply["success"]) {
        window.location = window.location.href;
      } else {
        showModalMessageBox(reply["message"], "Change account failed");
      }
    },
    error: function(){
      showModalMessageBox("Failed to connect server", "Ajax Failed");
    }
  });
	return false;
};
