﻿var IK = Class.create({});

IK.addMethods ({
Menu :{toggle:function(that, class1, class2)
	{
	
	menu = $(that);
	submenu = $('sub' +that.id.substring(3,50));	
	
	menu.toggleClassName(class1);
	menu.toggleClassName(class2);
	
	submenu.toggle();	
	
	}
}
});


var CUtil = Class.create({
 Nav: function(url){window.location.href = url;}
,isUnsignedInteger: function(s) {return (s.toString().search(/^[0-9]+$/) == 0);}
,ToMoney: function(original){return  Math.round(original*100)/100}
,open_window: function(url, width, height){new_window = window.open(url,'window_name','toolbar=0,menubar=0,resizable=1, scrollbars=1, dependent=0,status=0,width='+width+',height='+height+',left=25,top=25')}
,LogAjaxError: function(error){
var x;
Util.log('Ajax Error: ready State:' + error.readyState + '                                responseText' + error.responseText + '           textStatus: ' + error.textStatus,'yellow',1);
}
,getElementByEndingId: function(tagName, id)
{
	var elements = document.getElementsByTagName(tagName);
	for (var i=0; i < elements.length; i++) 
	{
		if (elements[i].id.lastIndexOf(id) == (elements[i].id.length - id.length) && elements[i].id.lastIndexOf(id) >= 0) 
            return elements[i];
	}
}
, addLoadEvent: function (func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          oldonload();
          func();
        }
      }
    }


,loadTextBox: function(that,beginText)
{
that = $(that);
that.value = beginText;
that.observe('focus', function(event){
var element = Event.element(event);
element.style.color = 'black';
element.value = "";
Event.stopObserving(element, "focus", this.loadTextBox);
      

});

that.observe('blur', function(event){
var element = Event.element(event);
if(element.value == '')
element.value = beginText;
});


}

,log: function(message, color, priority)
{
	if((/localhost|dellserver|debug/i.test(location.hostname)) && priority != -1)
	{ 
		if (!this.log.window_ || this.log.window_.closed) 
		{
			this.win = window.open("", null, "width=400,height=200," + "scrollbars=yes,resizable=yes,status=no," + "location=no,menubar=no,toolbar=no");
			if (!this.win) return;
			this.doc = this.win.document;
			this.doc.write("<html><head><title>Debug Log</title></head><style>.red{color:red}</style>" + "<body></body></html>");
			this.doc.close();
			this.log.window_ = this.win;
        }
    this.logLine = this.log.window_.document.createElement("div");
    if(color == 'red')
		this.logLine.className = "red";
    this.logLine.appendChild(this.log.window_.document.createTextNode(message ));
    this.log.window_.document.body.appendChild(this.logLine);
    this.log.window_.focus();
	}else 
	if(!/Syntax\serror|urchinTracker/i.test(message))
		new Ajax.Request('/services/log.ashx', {method:'get', parameters: {Message:  escape(message)  ,a:escape(Date())}}); 
	if(!/localhost/i.test(location.hostname))return true;
  }
 
 , maxLength:function(field, maxChars)
 {
  $(field).observe('keypress',function(event)
  {
	if($(field).value.length >= maxChars) 
	{
		event.returnValue=false;
		return false;
    }	
  })

  $(field).observe('paste',function(event)
  {
	if($(field).value.length >= maxChars) 
	{
		event.returnValue=false;
		return false;
    }	
  })

 }
 
 ,urlParam:function(name, url)
 {
   name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
   var regexS = "[\\?&]"+name+"=([^&#]*)";  
   var regex = new RegExp( regexS );  
   var results = regex.exec(url); //window.location.href   
   if( results == null )    
   return "";  
   else    
   return results[1];
 
 },
 Preload:function(args)
 {
	for(var i=0, images=[]; src=arguments[i]; i++)
	{
		images.push(new Image());
		images.last().src = src;
	}

 },
 ImageRetry:0
 ,ImageError: function(that,maxWidth,maxHeight)
 {
 IK.Util.log('IMAGE LOAD ERROR ' + that.src, null, -1)

//if(that.getWidth() != 0 && !Prototype.Browser.IE) return;
 
 var tryURL = that.src;
 if(tryURL.indexOf('?') == -1)
 tryURL += '?1'
 else
 tryURL += '&1'

 var maxH = maxHeight + 'px;'
 
if(typeof(that.style.maxHeight) != 'undefined')
{
	that.style.maxHeight = maxHeight;
	that.style.maxWidth = maxWidth;
 }
 that.src = "http://www.imagekind.com/images/banners/missing_image.gif";
window.setTimeout(function(){if(/missing/.test(that.src))that.src = tryURL},5000 + (this.ImageRetry += 5000))
 }
});

var txt="";

var Util = new CUtil();

//var IK = new CIK();
IK.Util = Util;
function handleErr(msg,url,l){txt+="JS Error: " + msg + " @: " + url + " Line: " + l ;Util.log(txt, 'red', 1);}
$E = Util.getElementByEndingId;
onerror=handleErr;

String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}
