﻿/// <reference path="~/Resources/shared/scripts/jquery/jquery-1.4.1-vsdoc.js" />
/// <reference path="~/Resources/shared/scripts/jquery/jquery-ui-vsdoc_1.js"

function SendReceiveXMLHttp(URL, data) {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        ShowMessage(1, "AJAX Error", "Your browser does not support AJAX!");
        return false;
      }
    }
  }

  xmlHttp.open("POST", URL, false);
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  xmlHttp.send(data);

  var xml = xmlHttp.responseXML;
  var result = "";

  if (xml.firstChild.childNodes.length == 0) {
    for (var i = 1; i < xml.childNodes.length; i++) {
      result += xml.childNodes[i].text;
    }
  }
  else {
    for (var i = 0; i < xml.firstChild.childNodes.length; i++) {
      result += xml.firstChild.childNodes[i].nodeValue;
    }
  }

  if (result.indexOf("Error") == -1) {
    return result;
  }
  else {
    ShowMessage(1, "Data Error", result);
    return false;
  }
}

function ShowImage(fadein, imageURL) {
  posY = getScreenCenterY();
  posX = getScreenCenterX();

  if (fadein == 1) {
    grayOut(true);

    var popupImg = document.getElementById("popupImg");
    if (!popupImg) {
      var tbody = document.getElementsByTagName("body")[0];
      var popupImg = document.createElement('img');
      popupImg.style.display = "block";
      popupImg.style.position = "absolute";
      popupImg.style.width = "324px";
      popupImg.style.height = "324px";
      popupImg.style.cursor = "pointer";
      popupImg.onclick = function CallShowImage() { ShowImage(0); }
      popupImg.style.zIndex = 100;
      popupImg.id = "popupImg";
      popupImg.title = "Click to hide";
      tbody.appendChild(popupImg);
    }
    $("#popupImg").fadeIn("medium").attr("src", imageURL).css("top", (posY - 162) + "px").css("left", (posX - 162) + "px");
  }
  else {
    $("#popupImg").fadeOut("fast");
    grayOut(false);
  }
}

// jquery plugin : $.include
//     $.include('file/ajaxa.js');$.include('file/ajaxa.css');
//  or $.includePath  = 'file/';$.include(['ajaxa.js','ajaxa.css']);

$.extend({
  includePath: '',
  include: function (file) {
    var files = typeof file == "string" ? [file] : file;
    for (var i = 0; i < files.length; i++) {
      var name = files[i].replace(/^\s|\s$/g, "");
      var att = name.split('.');
      var ext = att[att.length - 1].toLowerCase();
      var isCSS = ext == "css";
      var tag = isCSS ? "link" : "script";
      var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
      var link = (isCSS ? "href" : "src") + "='" + $.includePath + name + "'";
      if ($(tag + "[" + link + "]").length == 0) document.write("<" + tag + attr + link + "></" + tag + ">");
    }
  }
});

var posn = 150;

$.include('/Resources/Shared/scripts/jquery/jquery-ui-1.8.1.custom.min.js');
$.include('/Resources/Shared/scripts/jquery/themes/blitzer/jquery.ui.all.css');

$(function () {
  var $ErrorMessages = $("<div id='ErrorMessages' style='width: 400px; display: none; background-color: White;'></div>").prependTo("#Body");
  var $ErrorMsg = $("<p id='ErrorMsg' style='text-align: left;'></p>").appendTo($ErrorMessages);

  $("#ErrorMessages").dialog({
    modal: true,
    autoOpen: false,
    width: 550,
    minHeight: 90,
    buttons: {
      Ok: function () {
        $(this).dialog('close');
        try{
        //hide hour glass
        document.getElementById("hourglass").style.visibility = "hidden";
        //show submit button
        document.getElementById("!send").style.visibility = "visible";
        }catch(Error){

        }
      //  clearForm('form');

      }
    }
  });

  $("#ErrorMessages").dialog({
    open: function (event, ui) { $(this).focus(); }
  });

  $("#ErrorMessages").keypress(function (e) {
    $(this).dialog('close');
  });
});

function ShowMessage(fadein, heading, msg, Height) {

  $("#ErrorMsg").html('<span class="ui-icon ui-icon-circle-close" style="float:left; margin:0 7px 50px 0;"></span>' + msg);
  
  $("#ErrorMessages").dialog("option", "title", (heading !== undefined ? heading : 'An error has occurred')).dialog('open');
}

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
  }
  else var expires = "";
  document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') c = c.substring(1, c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function numbersonly(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key == null) || (key == 0) || (key == 8) ||
    (key == 9) || (key == 13) || (key == 27))
    return true;

  // numbers
  else if ((("0123456789.").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == ".")) {
    myfield.form.elements[dec].focus();
    return false;
  }
  else
    return false;
}

function HideRow(startdate, finishdate, RowID) {
  var cookiedate = readCookie("SimulateStartDate");
  if (cookiedate != undefined && cookiedate.length != 0) {
    var now = Number(cookiedate.substr(6, 4) + cookiedate.substr(3, 2) + cookiedate.substr(0, 2) + ".1");
    if (now > Number(startdate) && now <= Number(finishdate)) {
      //do nothing
    }
    else {
      document.getElementById("datarow_" + RowID + "_" + startdate).style.display = "none";
    }
  }
}

function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {};
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 90;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#0069b7';
  var darkNode = document.getElementById('darkenScreenObject');
  if (!darkNode) {
    // The darkNode layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.

    tnode.style.position = 'absolute';                 // Position absolutely
    tnode.style.top = '0px';                           // In the top
    tnode.style.left = '0px';                          // Left corner of the page
    tnode.style.overflow = 'hidden';                   // Try to avoid making scroll bars            
    tnode.style.display = 'none';                      // Start out Hidden
    tnode.id = 'darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    darkNode = document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height

    if (document.body && (document.body.scrollWidth || document.body.scrollHeight)) {
      var pageWidth = document.body.scrollWidth + 'px';
      var pageHeight = (document.body.scrollHeight + 150) + 'px';
    } else if (document.body.offsetWidth) {
      var pageWidth = document.body.offsetWidth + 'px';
      var pageHeight = document.body.offsetHeight + 'px';
    } else {
      var pageWidth = '100%';
      var pageHeight = '100%';
    }
    //set the shader to cover the entire page and make it visible.
    darkNode.style.opacity = opaque;
    darkNode.style.MozOpacity = opaque;
    darkNode.style.filter = 'alpha(opacity=' + opacity + ')';
    darkNode.style.zIndex = zindex;
    darkNode.style.backgroundColor = bgcolor;
    darkNode.style.width = pageWidth;
    darkNode.style.height = pageHeight;
    darkNode.style.display = 'block';
  } else {
    darkNode.style.display = 'none';
  }
}
function getScreenCenterY() {
  var y = 0;
  y = getScrollOffset() + (getInnerHeight() / 2);
  return (y);
}

function getScreenCenterX() {
  return (document.body.clientWidth / 2);
}

function getInnerHeight() {
  var y;
  if (self.innerHeight) // all except Explorer
  {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  // Explorer 6 Strict Mode
  {
    y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    y = document.body.clientHeight;
  }
  return (y);
}

function getScrollOffset() {
  var y;
  if (self.pageYOffset) // all except Explorer
  {
    y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  // Explorer 6 Strict
  {
    y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
    y = document.body.scrollTop;
  }
  return (y);
}


function clearForm(form) {

  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function () {
    var type = this.type;
    var tag = this.tagName.toLowerCase(); // normalize case
    // it's ok to reset the value attr of text inputs,
    // password inputs, and textareas
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = "";
    // checkboxes and radios need to have their checked state cleared
    // but should *not* have their 'value' changed
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    // select elements need to have their 'selectedIndex' property set to -1
    // (this works for both single and multiple select elements)
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};


