var da = document.all;

Array.prototype.indexOf = function (item){
	var idx = -1;
	
	for (var i=0; i < this.length;i++){
		//alert("ary[i]  = " + ary[i] + "; item  = " + item);
		if (this[i] == item){
			idx = i;
			break;
		}
	}
	
	return idx;	
};

Array.prototype.copy = function (){
	var newAry = Array();
	
	for (var i=0; i < this.length;i++){
		newAry[newAry.length] = this[i];
	}
	
	return newAry;
}

Array.prototype.subtract = function(ary){
	// Subtract ary from this array, 
	// leaving only elements not in ary
	this.tmpAry = this.copy();
	
	for (var i = 0; i < ary.length;i++){
		this.tmpAry.splice(this.tmpAry.indexOf(ary[i]),1);
	}
	
	return this.tmpAry;
}

Array.prototype.removeRandom =  function (numItems){
	/* 	Randomly remove [numItems] 
		elements from the array
	*/
	this.tmpAry = this.copy();

	var dt = new Date();
	idx = dt.getTime()%this.tmpAry.length;
	//var obj = this.tmpAry[idx];
	this.tmpAry.splice(idx,1);
	
	numItems--;
	
	if (numItems > 0){
		this.tmpAry = this.tmpAry.removeRandom(numItems);
	}

	return this.tmpAry;
}

Array.prototype.getRandom =  function (numItems){
	/* 	Return new array of [numItems] random elements 
		from the original array
	*/
	var aryB = this.removeRandom(numItems);
	var aryC = this.subtract(aryB);
	return aryC;
}

Array.prototype.reverseEach = function(){
	for (var i=0;i < this.length;i++){
		this[i] = this[i].reverse();
	}
}

String.prototype.trim = function() {   
	return this.replace(/^\s+|\s+$/g,"b");
}

String.prototype.reverse = function (){
    var ret = '', i = 0;
 	var str = new String(this);
 	
    for ( i = str.length-1; i >= 0; i-- ){
       ret += str.charAt(i);
    }
 
    return ret;
}

String.prototype.toAsc = function (){
	return this.charCodeAt(0);
}



String.prototype.demunge = function (){
	//takes a pipe-separated string value
	var ary = this.split("|");
	var sign = 64;

	sign = String.fromCharCode(sign);
	ary.reverseEach();

	return ary[0] + sign + ary[1] + "." + ary[2];
}

function refreshTableView() {
	var objForm = document.getElementById("refreshData");
	var objDates = objForm.queryDate;
	if (objDates.options[objDates.selectedIndex].value != "Date") {
		objForm.submit();
	}
}

function toggle(item,comp_id,pageNm){
	//alert('toggle');
	if (item.style.display == "block"){
		selectCompany(comp_id,pageNm);
		item.style.display = "none";
	}else{
		item.style.display = "block";
	}
}

function get(str){
	return document.getElementById(str);
}

var xhr;

function createXHR(){
	var xhr;
	try {
		if(!xhr && typeof XMLHttpRequest != 'undefined'){
			xhr= new XMLHttpRequest();
		} else {
			throw new Exception("not IE7");
		}
	}catch (b) {
		try{	
			xhr = new ActiveXObject("MSXML2.XMLHTTP");
	
		}catch(e){
			try{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xhr=false;
			}
		}
	}

	return xhr;
}

var key;

document.onkeydown = keyPress;
if (document.captureEvents) document.captureEvents(Event.KEYDOWN);
document.onkeyup = keyUp;
if (document.captureEvents) document.captureEvents(Event.KEYUP);

function keyUp(e){
	key = "";
}

function keyPress(e){
	key = (window.event)?event.keyCode:e.which;
}

var xmlDoc;

function loadXML(strXML)
{
//alert("loadXML in - " + strXML);
// code for IE
if (window.ActiveXObject)
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.loadXML(strXML);
  
  return xmlDoc;
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);

  var parser = new DOMParser();
  xmlDoc = parser.parseFromString(strXML, "text/xml");
  if(xmlDoc == null) alert("xml Doc Load Failed");

  return xmlDoc;
  
  //xmlDoc.onload=passXML(xmlDoc);
  }
else
  {
  alert('Your browser cannot handle this script');
  }
}

function passXML(doc){
	return doc;
}



var myPopupWindow = '';   
function popWin(url, name, width, height)   
{   
    //Remove special characters from name   
    name = name.replace(/\/|\-|\./gi, "");  
    width = (width == null)?"800":width;
    height = (height == null)?"550":height;
  
    //Remove whitespaces from name   
    var whitespace = new RegExp("\\s","g");
    name = name.replace(whitespace,"");   
  
    //If it is already open   
    if (!myPopupWindow.closed && myPopupWindow.location)
    {   
        myPopupWindow.location.href = encodeUrl(url);   
    }
    else  
    {   
        myPopupWindow= window.open(encodeUrl(url),name, "location=no, scrollbars=yes, resizable=yes, toolbar=no, menubar=no, width=" + width + ", height=" + height + ", left=" + ((window.screen.width - 596)/2) + ",top=" + ((window.screen.height - 460) /2.5));   
        if (!myPopupWindow.opener) myPopupWindow.opener = self;   
    }

     //If my main window has focus - set it to the popup   
    if (window.focus) {myPopupWindow.focus()}   
}


function encodeUrl(url)   
{   
    if (url.indexOf("?")>0)   
    {   
        encodedParams = "?";   
        parts = url.split("?");   
        params = parts[1].split("&");   
        for(i = 0; i < params.length; i++)   
        {   
            if (i > 0)   
            {   
                encodedParams += "&";   
            }   
            if (params[i].indexOf("=")>0) //Avoid null values   
            {   
                p = params[i].split("=");   
                encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));   
            }   
            else  
            {   
                encodedParams += params[i];   
            }   
        }   
        url = parts[0] + encodedParams;   
    }   
    return url;   
}  


function ajaxObject(url, callbackFunction) {  
	var that=this;        
	this.updating = false;  
	this.abort = function() {    
		if (that.updating) {      
			that.updating=false;      
			that.AJAX.abort();      
			that.AJAX=null;		
		}
	}

	this.update = function(passData,postMethod) {     
		if (that.updating) { return false; }    
		
		that.AJAX = null;                              
		if (window.XMLHttpRequest) {                    
			that.AJAX=new XMLHttpRequest();                  
		} else {                                        
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
		}                                                 
		
		if (that.AJAX==null) {                                   
			return false;                                   
		} else {      
			that.AJAX.onreadystatechange = function() {          
				if (that.AJAX.readyState==4) {                       
					that.updating=false;                          
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;                                                 
				}			                                                            
			}                                                              

			that.updating = new Date();                                    
			if (/post/i.test(postMethod)) { // regExp test againt postMethod
				var uri=urlCall+'?'+that.updating.getTime();        
				that.AJAX.open("POST", uri, true);        
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
				that.AJAX.setRequestHeader("Content-Length", passData.length);        
				that.AJAX.send(passData);      
			} else {        
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());         
				that.AJAX.open("GET", uri, true);                                     
				that.AJAX.send(null);                                               
			}

			return true;                                                 
		}                                                                          
	}

	var urlCall = url;          
	this.callback = callbackFunction || function () { };
}

function mailme(anchor){
	anchor.href="mailto:" + anchor.eml.demunge();
}

function toggleSeason(li){
	var e = null;
	var itm = li;
	while(e = itm.nextSibling){
		//alert(e);
		
		if(e.className == "seasonheader"){
			break;
		}
		
		e.style.display = (e.style.display == "none")?"inline-block":"none";
		itm = e;
	}
	
	//alert(li.nextSibling);
	//var nodes = li.parentNode.childNodes;
	//for(var i = 0; i < nodes; i++ ){
	//	nodes[i]
	//}
}
