﻿var baseUrl = "/";

addOnLoad(disableStartDate);
addOnLoad(fixIe6);
addOnLoad(initEditLinks);
addOnLoad(printProposal);
addOnLoad(printReceipt);
addOnLoad(sfHover); // menu drop-downs

function acceptedTerms(sender, e)
{
  var isValid = false;
  
  var terms = document.getElementById("terms");
  
  if (terms)
  {
    var controls = terms.getElementsByTagName("input");
    
    var checkbox = controls[0];
    
    if (checkbox)
    {
      if (checkbox.checked)
      {
        isValid = true;
      }
    }
  }
  
  e.IsValid = isValid;
}

function addOnLoad(newFunction)
{
	var oldOnLoad = window.onload;
	
	if (typeof oldOnLoad == "function")
	{
		window.onload = function()
		{
			if (oldOnLoad)
			{
				oldOnLoad();
			}

			newFunction();
		}
	}
	else
	{
		window.onload = newFunction;
	} 
}

//function checkVersion()
//{
//  var msg = "You're not using Internet Explorer.";
//  var ver = getInternetExplorerVersion();

//  if ( ver > -1 )
//  {
//    if ( ver >= 6.0 ) 
//      msg = "You're using a recent copy of Internet Explorer."
//    else
//      msg = "You should upgrade your copy of Internet Explorer.";
//  }
//  alert( msg );
//}

function disableStartDate()
{
  var start_date = document.getElementById("start_date");
  
  if (start_date)
  {
    var textboxes = start_date.getElementsByTagName("input");
    
    if (textboxes.length > 0)
    {
      for (var i = 0; i < textboxes.length; i++)
      {
        textboxes[i].readOnly = true;
      }
    }
  }
}

function fixIe6()
{
  if (document.all)
  {
    var c_main = document.getElementById("c_main");
    
    if (c_main)
    {
      // Adjust height of main container
      //if (c_main.clientHeight < 400)
      //{
      //  c_main.style.height = "400px";
      //}
    }
    
    pngFix();
  }
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function help(option, msg)
{
  var helpText = new StringBuilder();
  helpText.AppendLine("<html>");
  helpText.AppendLine("<head>");
  helpText.AppendLine("  <title>Help</title>");
  //helpText.AppendLine("  <script src=\"" + baseUrl + "Scripts/internetsalesmgr.js\" type=\"text/javascript\"></style>");
  helpText.AppendLine("  <style type=\"text/css\">");
  helpText.AppendLine("    a:link, a:visited { color: white; text-decoration: none; }");
  helpText.AppendLine("    a:hover, a:active { color: white; text-decoration: underline; }");
  helpText.AppendLine("    body { font: 0.69em Verdana, Tahoma, Arial, Helvetica, Sans-Serif; margin: 0px; }");
  helpText.AppendLine("    #close { cursor: pointer; float: right; }");
  helpText.AppendLine("    #content { clear: both; padding: 10px; }");
  helpText.AppendLine("    #header { background-color: #004e98; border-bottom: 1px solid #cccccc; color: white; padding: 10px; }");
  helpText.AppendLine("    #icon { float: left; }");
  // TODO: Replace PNGs with GIFs.
  helpText.AppendLine("    #title { background: url(" + baseUrl + "Images/Icons/help.png) no-repeat scroll left center; font-size: 1.27em; padding-left: 20px; }");
  helpText.AppendLine("  </style>");
  helpText.AppendLine("</head>");
  helpText.AppendLine("<body>");
  helpText.AppendLine("  <div id=\"header\">");
  helpText.AppendLine("    <img src=\"" + baseUrl + "Images/Icons/cross.png\" id=\"close\" onclick=\"window.close();\" title=\"Close\" />");
  helpText.AppendLine("      <div id=\"title\">Help</div>");
  helpText.AppendLine("  </div>");
  helpText.Append("  <div id=\"content\">");
  
  if (option.length != 0)
  {
    helpText.Append("<strong>" + option + "</strong><br /><br />");
  }
  
  helpText.AppendLine("\n    " + msg);
  helpText.AppendLine("  </div>");
  helpText.AppendLine("</body>");
  helpText.AppendLine("</html>");
  
  var help = openWindow("", "help", 400, 300, "yes", "yes");
  help.document.write(helpText.ToString());
  help.document.close();
}

function initEditLinks()
{
  var edit_links = document.getElementById("edit_links");
  var order_summary = document.getElementById("order_summary");
  
  if (edit_links && order_summary)
  {
    var links = edit_links.getElementsByTagName("a");
    var headings = order_summary.getElementsByTagName("h3");
    
    if (links.length == 4 & headings.length > 3)
    {
      for (var i = 0; i < links.length; i++)
      {
        var span = document.createElement("span");
        span.style.fontSize = "0.83em";
        
        var span_txt = document.createTextNode(" [");
        span.appendChild(span_txt);
        
        var edit_link = document.createElement("a");
        edit_link.setAttribute("href", links[i]);
        
        var edit_link_txt = document.createTextNode("edit");
        edit_link.appendChild(edit_link_txt);
        
        span.appendChild(edit_link);
        
        span_txt = document.createTextNode("]");
        span.appendChild(span_txt);
        
        headings[i].appendChild(span);
      }
    }
  }
}

function openWindow(url, n, w, h, sb, r, mb, tb, s, loc, dir)
{
  var screenWidth = window.screen.availWidth;
  var screenHeight = window.screen.availHeight;

  var offsetXWinXP = 5;
  var offsetYWinXP = 18;

  var halfScreenWidth = Number(window.screen.availWidth / 2);
  var halfScreenHeight = Number(window.screen.availHeight / 2);

  var halfWindowWidth = Number(w / 2);
  var halfWindowHeight = Number(h / 2);

  var diffWidth = Number(halfScreenWidth-halfWindowWidth);
  var diffHeight = Number(halfScreenHeight-halfWindowHeight);

  diffWidth -= offsetXWinXP;
  diffHeight -= offsetYWinXP;
  
  var windowName = n;
  
  if (windowName.length == 0)
  {
    //The windowName is a zero-based integer corresponding to the number of milliseconds
    //since January 1, 1970, to the date specified by the instance of the Date object;
    //therefore, the windowName will almost always be unique.
    var dtm = new Date();
    windowName = dtm.getTime();
  }
  
  //if (url == null) { url = "" };
  if (w == null) { w = 640 };
  if (h == null) { h = 480 };
  if (sb == null) { sb = "no" };
  if (r == null) { r = "no" };
  if (mb == null) { mb = "no" };
  if (tb == null) { tb = "no" };
  if (s == null) { s = "no" };
  if (loc == null) { loc = "no" };
  if (dir == null) { dir = "no" };

  var features = new StringBuilder();
  features.Append("directories=" + dir + ",");
  features.Append("location=" + loc + ",");
  features.Append("menubar=" + mb + ",");
  features.Append("resizable=" + r + ",");
  features.Append("scrollbars=" + sb + ",");
  features.Append("status=" + s + ",");
  features.Append("toolbar=" + tb +",");
  features.Append("width=" + w + ",");
  features.Append("height=" + h + ",");
  features.Append("top=" + diffHeight + ",");
  features.Append("left=" + diffWidth);
  
  return window.open(url, windowName, features.ToString());
}

function pngFix()
{
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])

  if ((version >= 5.5 && version <= 6) && (document.body.filters)) 
  {
     for(var i=0; i<document.images.length; i++)
     {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
           var imgID = (img.id) ? "id='" + img.id + "' " : ""
           var imgClass = (img.className) ? "class='" + img.className + "' " : ""
           var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
           var imgStyle = "display:inline-block;" + img.style.cssText 
           if (img.align == "left") imgStyle = "float:left;" + imgStyle
           if (img.align == "right") imgStyle = "float:right;" + imgStyle
           if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
           var strNewHTML = "<span " + imgID + imgClass + imgTitle
           + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
           + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
           + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
           img.outerHTML = strNewHTML
           i = i-1
        }
     }
  }
}

function printProposal()
{
  var print = document.getElementById("print_proposal");

  if (print)
  {
    var links = print.getElementsByTagName("a");
    
    if (links.length > 0)
    {
      links[0].onclick = function()
      {
        var preview = document.getElementById("preview");
        
        if (preview)
        {
          var o = new StringBuilder();
          o.AppendLine("<html>");
          o.AppendLine("<head>");
          o.AppendLine("  <title>Proposal</title>");
          o.AppendLine("  <link href=\"" + baseUrl + "App_Themes/Default/Default.css\" rel=\"stylesheet\" type=\"text/css\"");
          o.AppendLine("</head>");
          o.AppendLine("<body>");
          o.AppendLine("<div id=\"proposal\" style=\"width: 100%;\">");
          o.Append(preview.innerHTML);
          o.AppendLine("</div>");
          o.AppendLine("</body>");
          o.AppendLine("</html>");
          
          var w = openWindow("", "print", 800, 600, "yes", "yes", "yes");
          w.document.write(o.ToString());
          w.document.close();
          
          w.print();
          
          return false;
        }
      }
    }
  }
}

function printReceipt()
{
  var print = document.getElementById("print_receipt");
  
  if (print)
  {
    var links = print.getElementsByTagName("a");

    var lnkPrint = links[0];
    
    if (lnkPrint)
    {
      lnkPrint.onclick = function()
      {
        var summary = document.getElementById("summary");
        
        if (summary)
        {
          var o = new StringBuilder();
          o.AppendLine("<html>");
          o.AppendLine("<head>");
          o.AppendLine("  <title>Order Summary</title>");
          o.AppendLine("  <link href=\"" + baseUrl + "App_Themes/Default/Default.css\" rel=\"stylesheet\" type=\"text/css\"");
          o.AppendLine("</head>");
          o.AppendLine("<body>");
          o.AppendLine("<div><img src=\"" + baseUrl + "Images/logo_ism.gif\" alt=\"InternetSalesMGR.com\" title=\"InternetSalesMGR.com\" /></div>");
          o.AppendLine("<div id=\"summary\" style=\"width: 100%;\">");
          o.Append(summary.innerHTML);
          o.AppendLine("</div>");
          o.AppendLine("</body>");
          o.AppendLine("</html>");
          
          var w = openWindow("", "summary", 800, 600, "yes", "yes", "yes");
          w.document.write(o.ToString());
          w.document.close();
          
          w.print();
          
          return false;
        }
      }
    }
  }
}

function reference(href)
{
  openWindow(href, "reference", 900, 400, "yes", "yes");
}

function sfHover()
{
  if (window.attachEvent && document.getElementById("c_menu"))
  {
	  var sfEls = document.getElementById("c_menu").getElementsByTagName("LI");
  	
	  for (var i = 0; i < sfEls.length; i++)
	  {
		  sfEls[i].onmouseover = function()
		  {
			  this.className += " sfhover";
			  this.className += " sftab";
		  };

		  sfEls[i].onmouseout = function()
		  {
			  this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
			  this.className = this.className.replace(new RegExp(" sftab\\b"), "");
		  };
	  }
  }
}

// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
{
  this.strings = new Array("");
  this.Append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.Append = function (value)
{
  if (value)
  {
    this.strings.push(value);
  }
}

// Appends the given value to the end of this instance with a line break.
StringBuilder.prototype.AppendLine = function (value)
{
  if (value)
  {
    this.strings.push(value + "\n");
  }
}

// Clears the string buffer
StringBuilder.prototype.Clear = function ()
{
  this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.ToString = function ()
{
  return this.strings.join("");
}
