// JavaScript Document

Array.prototype.inArray = function(str){
	for(var i in this){
		if(str == this[i]) return true;
	};
	return false;
}

/**
 * $A
 * @param {string} param
 */
 function $A(str) {
 	return str.split(' ');
 }

function c_checkbox(arr){
  alt_checkbox(arr,true);
}

function u_checkbox(arr){
  alt_checkbox(arr,false);
}

/**
 * alt_checkbox
 * @arr {string} prefix of the name of check boxes
 * @ac {string} action to be performed ( true/false) for ( check/ uncheck)
 */
function alt_checkbox(arr,ac){
  if(!document.getElementsByTagName('input')) return;
  var iarr = document.getElementsByTagName('input');
  for(var i=0; i < iarr.length; i++){
    if(iarr[i].type == 'checkbox' && iarr[i].name.match(arr))
    iarr[i].checked = ac;
  }
}

function focusFormElement(){
	var frm = document.getElementsByTagName('form');
	if(frm.length<=0) return;
	frm = frm[0];
	var els = frm.getElementsByTagName('input');
	for(var i=0;i<els.length;i++){
		if(!(els[i].type == 'hidden' || els[i].type == 'undefined' || els[i].style.display == 'none' ) ){
		 	els[i].focus();
			return;
		}
	}
	return false;
}

function addLoadEvent(func){
  var oldonload = window.onload;
  if(typeof oldonload != 'function'){
    window.onload = func;
  }else{
    window.onload = function(){
      oldonload();
      func();
    }
  }
}

function altTableRow(){
  if(!document.getElementsByTagName('table')) return;
  var tbl = document.getElementsByTagName('table');
  var odd = true;
  for(var i=0; i<tbl.length; i++){
    row = tbl[i].getElementsByTagName('TBODY')[0].getElementsByTagName('tr');
    for(var j=0; j<row.length; j++){
      if(odd){
        row[j].className='odd';
        odd = false;
      }else{
        row[j].className='even';
        odd = true;
      }
    }
  }
}

function altTableRowMouseOver(){
  if(!document.getElementsByTagName('table')) return;
  var tbl = document.getElementsByTagName('table');
  for(var i=0; i<tbl.length; i++){
  	var classArray = $A(tbl[i].className);
  	if(classArray.inArray('main') || classArray.inArray('data') ){
	    var row = tbl[i].getElementsByTagName('tr');
	    for(var j=0; j<row.length; j++){
		  var rowClass = $A(row[j].className);
		  if(rowClass.inArray('heading') || rowClass.inArray('ignore') ) continue;
	      current_row = row[j];
	      current_row.id = j;
	      current_row.onmouseover = function(){
	        if(this.highlight != 1)
	        this.className = 'odd';
	      }
	      current_row.onclick = function(){
	        if(this.highlight == 1){
	          this.className = 'odd';
	          this.highlight = 0;
	        }else{
	          this.className = 'highlight';
	          this.highlight = 1;
	        }
	      }
	      current_row.onmouseout = function(){
	        if(this.highlight != 1)
	        this.className = 'even';
	      }
	    }
	  }
  }
}

function altFieldSet(){
  if(!document.getElementsByTagName('fieldset')) return;
  var fset = document.getElementsByTagName('fieldset');
  for(var i=0; i<fset.length; i++){
    fset[i].onmouseover = function(){
      this.className = 'odd';
    }
    fset[i].onmouseout = function(){
      this.className = 'even';
    }
  }
}

function altVisibility(obj){
  if(typeof obj != 'object')
  obj = document.getElementById(obj);
  if(!obj) return false;
  obj.style.display = (obj.style.display == 'none')?'':'none';
  return false;
}

function getParentElement(obj,tag){
  if(tag == '') return false;
  tag = tag.toLowerCase();
  obj = (typeof obj != 'object')? document.getElementById('obj'):obj;
  if(!obj) return false;
  var node = '';
  while(node != tag){
    pNode = obj.parentNode;
    obj = pNode;
    node = pNode.nodeName;
    node = node.toLowerCase();
    //if(!confirm(node)) break;
  }
  return pNode;
}

/**
 *
 * @access public
 * @return void
 **/
function getMousePosition(e){
	var e = e || window.event;
	this.src = (e.srcElement)?e.srcElement:e.target; // event source element
	this.x = (e.clientX)?e.clientX:e.pageX;
	this.y = (e.clientX)?e.clientY:e.pageY;
	return this;
}
/* function getChildElement(obj,tag){
  obj = (typeof obj != 'object')? document.getElementById('obj'):obj;
  if(!obj) return false;
}
 */

 /**
  *
  * @access public
  * @return object of id
  **/
 function $(){
    if(arguments.length <= 0)
        return false;
    else if(arguments.length == 1){
        return (typeof arguments[0] == 'object')? arguments[0] : document.getElementById(arguments[0]);
    }else{
        var obj = [];
        for(var i=0;id=arguments[i];i++){
            obj.push((typeof id == 'object')? id : document.getElementById(id));
        }
        return obj;
    }
}

Function.prototype.bind = function(obj){
	var method = this;
	return function (e){
		method.call(obj,e||window.event)
	}
}

 //~ addLoadEvent(altTableRowMouseOver);
 //~ addLoadEvent(focusFormElement);

//~ addLoadEvent(altFieldSet);
//~ addLoadEvent(altTableRow);


  function step(obj){
    for(i in obj){
      if(!confirm(i +' :: '+ obj[i])){
        return false;
      }
    }
  }

  function styleQuote(){
    var el = document.getElementsByTagName('blockquote');
    for(var i=0;i<el.length;i++){
      var bqt = el[i];
      /*
      bqt.style.background = 'url(images/q_open.gif) no-repeat top left';
      bqt.style.margin = '0px';
      bqt.style.border = '1px solid red';
      bqt.style.padding = "0px 2px 2px 30px";
      var im = document.createElement('img');
      im.src = 'images/q_close.gif';
      im.style.margin = '0px';
      im.style.padding = '0px';
      im.style.border = '1px solid red';
      bqt.appendChild(im);
      */
      
      var html = bqt.innerHTML;
      html = "<font size='5'>&ldquo;</font>" + html;
      html = html + "<font size='5'>&rdquo;</font>";
      bqt.innerHTML = html;
    }
    
    return false;
  }

function setClass(){
  var ip = document.getElementsByTagName('input');
  for(var i in ip){
    switch(ip[i].type){
      case 'submit':
        ip[i].className = 'bt_submit';
        break;
      case 'reset':
        ip[i].className = 'bt_reset';
        break;
      case 'button':
        ip[i].className = 'bt_bt';
        break;
      default:
        break;
    }
  }
  return false;
}

function getOffset(obj){
	var offset = new Object;
	offset.x = 0;
	offset.y = 0;
  var srcObj = obj;
	if(obj.offsetParent)
			while(1) 
			{
				offset.x += obj.offsetLeft;
				offset.y += obj.offsetTop;
				if(!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
	else if(obj.x){
			offset.x += obj.x;
			offset.y += obj.y;
	}
	offset.width = srcObj.offsetWidth | srcObj.pixelWidth;
	offset.height = srcObj.offsetHeight | srcObj.pixelHeight;
	return offset;
}

function windowSize(){
  //~ Getting width here
  this.width = 0;
  if (window.innerWidth) this.width = window.innerWidth - 18;
  else if (document.documentElement && document.documentElement.clientWidth)
    this.width = document.documentElement.clientWidth;
  else if (document.body && document.body.clientWidth)
    this.width = document.body.clientWidth;

  //~ Getting height here
  this.height = 0;
  if (window.innerHeight) this.height = window.innerHeight - 18;
  else if (document.documentElement && document.documentElement.clientHeight)
    this.height = document.documentElement.clientHeight;
  else if (document.body && document.body.clientHeight)
    this.height = document.body.clientHeight;

  //~ Getting horizontal offset
  this.scrollx = 0;
  if (typeof window.pageXOffset == "number") this.scrollx = window.pageXOffset;
  else if (document.documentElement && document.documentElement.scrollLeft)
    this.scrollx = document.documentElement.scrollLeft;
  else if (document.body && document.body.scrollLeft)
    this.scrollx = document.body.scrollLeft;
  else if (window.scrollX) this.scrollx = window.scrollX;

  //~ Getting verticle offset
  this.scrollY = 0;
  if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
  else if (document.documentElement && document.documentElement.scrollTop)
    this.scrollY = document.documentElement.scrollTop;
  else if (document.body && document.body.scrollTop)
    this.scrollY = document.body.scrollTop;
  else if (window.scrollY) this.scrolly = window.scrollY;
	
	this.scrollX = this.scrollX || 0;
	this.scrollY = this.scrollY || 0;
	//~ getting window total height width
	this.sHeight = document.documentElement.clientHeight || document.body.scrollHeight;
	this.sWidth = document.documentElement.clientWidth || document.body.scrollWidth;
	
	this.width = parseInt(this.width);
	this.height = parseInt(this.height);
	this.scrollX = parseInt(this.scrollX);
	this.scrollY = parseInt(this.scrollY);
	this.sHeight = parseInt(this.sHeight);
	this.sWidth = parseInt(this.sWidth);
	
	//~ getting screen center co-ordinates 
	this.cx = parseInt((this.width)/2 + this.scrollX);
	this.cy = parseInt((this.height)/2 + this.scrollY);
  return this;
}
//~ addLoadEvent(setClass);
