﻿///<Reference path="jquery-1.3.2.js" />
// version : 0.1

jQuery.noConflict();
// we need jQuery to run
(function(jQuery) { if (!jQuery) return; });

//-----------Debug--------------------------------------------------------------------------------------------

jQuery.fn.debug = function(what) {
  if (!what)
    what = "";

  var value;
  jQuery(this).each(function() {
    value = "";
    if (what.toLowerCase() == "html")
      value = jQuery(this).html();
    else if (what.toLowerCase() == "text")
      value = jQuery(this).text();
    else if (what.toLowerCase() == "val" || what.toLowerCase() == "value")
      value = jQuery(this).val();
    else
      if (jQuery(this).get(0))
      value = jQuery(this).get(0).toString();
    else
      return;

    if (window.console)
      console.log(value);
    else
      alert(value);
  });
};

jQuery.extend({
  debug: function(element, what) {
    if (!element) {
      jQuery("<div>" + element + "</div>").debug("text");
      return;
    }

    if (!jQuery(element).get(0))
      jQuery("<div>" + element + "</div>").debug("text");
    else
      jQuery(element).debug(what);
  }
});

//-----------Log--------------------------------------------------------------------------------------------

jQuery.extend({
  log: function(serverUrl, type, message, data) {
    try {
      var request = jQuery.ajax({
        type: "GET",
        url: (serverUrl + "?qs=log&type=" + type + "&msg=" + message),
        data: data,
        cache: false,
        dataType: "html",
        error: function(XMLHttpRequest, textStatus, errorThrown) {
        },
        success: function(msg) {
        }
      });
    } catch (e) {
    }
  }
});

jQuery.fn.log = function(serverUrl, type, data) {
  try {
    jQuery(this).each(function() {
      jQuery.Log(serverUrl, type, jQuery(this).text(), data);
    });
  } catch (e) {
  }
};

//-----------GetServerTagText--------------------------------------------------------------------------------------------
//Gets the text of Response text that is formatted using HTML tags.
jQuery.fn.getServerTagText = function(key) {
  return jQuery("*[id='" + key + "']", this).text();
};

//-----------Alert--------------------------------------------------------------------------------------------
jQuery.extend({
  alert: function(title, message, imagePath, callback) {
    var isActive = 0;
    var html = "<div id='alert_outerDIV' style='background-color: #000000; * html height: auto; '></div>";
    html += "<div id='alert_innerDIV' style='text-align: left; padding-top: 4px; background-color: #FFFFFF; border: solid 1px #000000; margin: 0; padding: 0;'>";
    html += "<div id='alert_titleDIV' style='background-color: #DDDDDD; padding: 3px 3px 0 10px; border-top: solid 1px #000000; border-bottom: solid 1px #000000; font-weight: bold; color: Red'>";
    html += title;
    html += "</div>";
    html += "<img id='alert_img' alt='' src='" + imagePath + "' style='float: left; padding: 5px; vertical-align: middle;' title='" + message  + "' />";
    html += "<div id='alert_messageDIV' style=' color: #000000; text-align: left; padding: 5px 15px 15px 5px; '>";
    html += "<label for='alert_btnOK'>" + message + "</label>";
    html += "</div><br />";
    html += "<div id='alert_buttonsDIV' style='clear: both; text-align: center; padding: 5px 0 0 5px; margin-bottom: 5px;'>";
    html += "<input id='alert_btnOK' type='button' value='  OK  ' style='border: solid 1px; font-weight: bold;' />";
    html += "</div>";
    html += "</div>";

    jQuery(document).ready(function() {
      try {
        jQuery("#alert_outerDIV", document).remove();
        jQuery("#alert_innerDIV", document).remove();

        jQuery("body > form", document).append(html);

        jQuery("#alert_outerDIV", document).click(function() { jQuery("#alert_btnOK", document).focus(); });
        jQuery("#alert_innerDIV", document).click(function() { jQuery("#alert_btnOK", document).focus(); });

        Alert_LoadOuterDIV();
        Alert_LoadInnerDIV();

        jQuery(window).resize(function(e) { Alert_window_onresize(e); });
        jQuery(window).scroll(function(e) {
          Alert_window_onresize(e);
          jQuery("#alert_outerDIV", document).css("height", jQuery("#alert_outerDIV", document).outerHeight() + jQuery(document).scrollTop() + "px");
        });
        jQuery("#alert_btnOK", document).focus();
        jQuery("#alert_btnOK", document).click(function() {
          Alert_UnloadOuterDIV(); Alert_UnLoadInnerDIV();
          if (callback)
            callback();
        });
      } catch (e) {
        alert(message);
      }
    });
    function Alert_window_onresize(e) {
      if (isActive == 1) {
        Alert_LoadOuterDIV();
        Alert_LoadInnerDIV();
      }
    }
    function Alert_LoadOuterDIV() {
      var windowHeight;
      try {
        // supported in Mozilla, Opera, and Safari
        if (window.innerHeight)
          windowHeight = window.innerHeight;
        // supported in standards mode of IE, but not in any other mode
        else if (window.document.documentElement.clientHeight)
          windowHeight = document.documentElement.clientHeight;
        // supported in quirks mode, older versions of IE, and mac IE (anything else).
        else
          windowHeight = window.document.body.clientHeight;

        jQuery("#alert_outerDIV", document).css("display", "block");
        jQuery("#alert_outerDIV", document).css("visibility", "visible");
        jQuery("#alert_outerDIV", document).css("opacity", "0.5");
        jQuery("#alert_outerDIV", document).css("top", "0");
        jQuery("#alert_outerDIV", document).css("left", "0");
        jQuery("#alert_outerDIV", document).css("width", "100%");
        jQuery("#alert_outerDIV", document).css("height", windowHeight + "px");
        jQuery("#alert_outerDIV", document).css("z-index", "998");
        jQuery("#alert_outerDIV", document).css("position", "absolute");

        isActive = 1;
      } catch (e) {
        alert(message);
      }
    }
    function Alert_LoadInnerDIV() {
      var windowWidth, windowHeight, width, height, left, top;
      try {
        windowWidth = jQuery("#alert_outerDIV", document).outerWidth();
        windowHeight = jQuery("#alert_outerDIV", document).outerHeight();

        //if (jQuery.browser.msie) {
        jQuery("#alert_innerDIV", document).css("width", "350");
        //}
        jQuery("#alert_innerDIV", document).css("max-width", (windowWidth / 2));

        width = jQuery("#alert_innerDIV", document).outerWidth();
        height = jQuery("#alert_innerDIV", document).outerHeight();
        left = (windowWidth - width) / 2;
        top = (windowHeight - height) / 2;

        jQuery("#alert_innerDIV", document).css("display", "block");
        jQuery("#alert_outerDIV", document).css("visibility", "visible");
        jQuery("#alert_innerDIV", document).css("left", left + "px");
        jQuery("#alert_innerDIV", document).css("top", top + "px");
        jQuery("#alert_innerDIV", document).css("z-index", "999");
        jQuery("#alert_innerDIV", document).css("position", "absolute");

        isActive = 1;
      } catch (e) {
        alert(message);
      }
    }
    function Alert_UnloadOuterDIV() {
      jQuery("#alert_outerDIV", document).css("display", "none");
      jQuery("#alert_outerDIV", document).css("visible", "hidden");
      isActive = 0;
    }
    function Alert_UnLoadInnerDIV() {
      jQuery("#alert_innerDIV", document).css("display", "none");
      jQuery("#alert_innerDIV", document).css("visible", "hidden");
      isActive = 0;
    }
    return this;
  }
});

//------ Form Validation ----------------------------------------------------------------------------------
function SetInvalidFormFieldStyle(sender, isValid, invalidControl) {
    if (!isValid) {
        jQuery(sender).css("display", "block");

        if (!jQuery(invalidControl).hasClass("invalidField"))
	        jQuery(invalidControl).addClass("invalidField");				

       //jQuery(invalidControl).focus();
    }
    else
    {
        jQuery(sender).css("display", "none");

	    if (jQuery(invalidControl).hasClass("invalidField"))
		    jQuery(invalidControl).removeClass("invalidField");

    }
}

function CheckExpression(text, pattern) {
    var reg = new RegExp(pattern);
    return reg.test(text);
}
