/**
*	CosmoAJAX build 001
*	Copyright Cosmopoint Sdn. Bhd.
*	Author: Syed Muhammad @ 1337
*	Description: Javascript library helper for advAJAX 1.1
*/
/*Global variables*/
var indicator_img_loading = Element('img');
indicator_img_loading.src = 'mozilla_blu.gif';
indicator_img_loading.width = 16;
indicator_img_loading.height = 16;
/*User interaction functions*/
function showIndicator(type,msg) {
	$('indicator').innerHTML = '';
	$('indicator').style.display = 'block';
	switch(type) {
		case 'Loading' : $('indicator').appendChild(indicator_img_loading);break;
	}
	$('indicator').appendChild(Text(msg));
}
function hideIndicator() {
	$('indicator').style.display = 'none';
}
/*Overused functions in HTML DOM*/
function $(id) {
	return document.getElementById(id);
}
function $F(id) {
	return $(id).value;
}
function $A(name) {
	return document.getElementsByName(name);
}
/* Usage: instead of var tr = document.createElement('tr'); use var tr = Element('tr');*/
function Element(element) {
	return document.createElement(element);
}
/* Usage: instead of var text = document.createTextNode('test'); use var text = Text('test');*/
function Text(text) {
	return document.createTextNode(text);
}
/*Overused functions in XML DOM + advAJAX */
function $Fxml(xml,tag,rec) {
	return xml.getElementsByTagName(tag).item(rec).firstChild.data;
}
function CosmoTable(tableid) {
	/* Members declaration */
	this.table = Element('table');
	this.table.appendChild(Element('tbody'));
	this.table.cellPadding = 5;
	this.table.cellSpacing = 0;
	this.table.border = 0;
	this.selectColor = "#FFE6BF";
	this.selectText = "#000000";
	this.firstColor = "#F2F2F2";
	this.firstText = "#000000";
	this.secondColor = "#FFFFFF";
	this.secondText = "#000000";
	this.table.id = tableid;
	this.unit = 'px';
	this.aligns = new Array();
	this.widths = new Array();
	this.paddingLeft = '2px';
	this.paddingRight = '2px';
	this.rowHeight = '';
	this.vAlign = 'top';
	/* Functions definition */
	this.stripe = stripe;
	this.setId = setId;
	this.appendRow = appendRow;
	this.getTable = getTable;
	this.clearTable = clearTable;
	this.setWidth = setWidth;
	this.setAlign = setAlign;
	this.setRowHeight = setRowHeight;
	this.setValign = setValign;
	this.getColumnHeaders = getColumnHeaders;
	/*Functions declarations*/
	function setValign(valign) {
		this.vAlign = valign;
	}
	function setAlign(aligns) {
		for(var i=0;i<aligns.length;i++) {
			switch(aligns[i]) {
				case 'c' : this.aligns[i] = "center";break;
				case 'l' : this.aligns[i] = "left";break;
				case 'r' : this.aligns[i] = "right";break;
			}
		}
	}
	function setRowHeight(rh) {
		this.rowHeight = rh;
	}
	function getColumnHeaders(name) {
		var columns = $A(name);
		var widths = new Array();
		for(var i=0;i<columns.length;i++) {
			widths[i] = columns[i].style.width;
			widths[i] = widths[i].replace('px','');
		}
		this.widths = widths;
	}
	function clearTable() {
		var noofrows = this.table.tBodies[0].rows.length;
		for(var i=0;i<noofrows;i++) {
			this.table.tBodies[0].deleteRow(0);
		}
	}
	function setWidth(widths) {
		this.widths = widths
	}
	function getTable() {
		return this.table;
	}
	
	function setId(id) {
		this.table.id = id;
	}
	function stripe() {
		for(var i=0;i<this.table.tBodies[0].rows.length;i++) {
			if(i%2==0) {
				this.table.tBodies[0].rows[i].style.backgroundColor = this.firstColor;
				this.table.tBodies[0].rows[i].style.color = this.firstText;
			}
			else {
				this.table.tBodies[0].rows[i].style.backgroundColor = this.secondColor;
				this.table.tBodies[0].rows[i].style.color = this.secondText;
			}
		}
	}
	function appendRow(td,funcname,funcparams) {
		var tr = this.table.tBodies[0].insertRow(this.table.tBodies[0].rows.length);
		if(this.rowHeight!='') {
			tr.style.height = this.rowHeight+'px';
		}
		tr.vAlign = this.vAlign;
		for(var i=0;i<td.length;i++) {
			if(this.aligns.length!=0) {
				td[i].style.textAlign = this.aligns[i];
			}
		}
		if(this.widths.length!=0) {
			for(var i=0;i<td.length;i++) {
				if(!(this.unit=='px')) {
					td[i].width = this.widths[i];
				}
				else {
					td[i].style.width = (this.widths[i]-4)+this.unit;
				}
				td[i].style.paddingRight = this.paddingRight;
				td[i].style.paddingLeft = this.paddingLeft;
				tr.appendChild(td[i]);
			}
		}
		else {
			for(var i=0;i<td.length;i++) {
				tr.appendChild(td[i]);
			}
		}
		
		this.stripe();
		tr.value = "firstText="+this.firstText+"&secondText="+this.secondText+"&selectText="+this.selectText+"&selectColor="+this.selectColor+"&firstColor="+this.firstColor+"&secondColor="+this.secondColor+"&id="+this.table.id;
		tr.onclick = 	function(e) {
							var values = convertToArray(this.value);
							/* Return the color into their respective color */
							for(var i=0;i<$(values['id']).tBodies[0].rows.length;i++) {
								if(i%2==0) {
									$(values['id']).tBodies[0].rows[i].style.backgroundColor = values['firstColor'];
									$(values['id']).tBodies[0].rows[i].style.color = values['firstText'];
								}
								else {
									$(values['id']).tBodies[0].rows[i].style.backgroundColor = values['secondColor'];
									$(values['id']).tBodies[0].rows[i].style.color = values['secondText'];
								}
							}
							this.style.backgroundColor = values['selectColor'];
							this.style.color = values['selectText'];
							if(!(funcname==null||funcname=='')) {
								if(!(funcparams==null||funcparams=='')) {
									funcname(funcparams);
								}
								else {
									funcname();
								}
							}
						};
	}
}
/*format accepted: firstvalue=firstvalue&secondvalue=secondvalue*/
function convertToArray(values) {
	var temp = values.split('&');
	var array = new Array();
	for(var i=0;i<temp.length;i++) {
		var temp2 = temp[i].split('=');
		array[temp2[0]] = temp2[1];
	}
	return array;
}
/*Client side form validation functions*/

var whitespace = " \t\n\r";

function isWhitespace(s) {
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

function forceNumber(objField, FieldName) {
	var strField = new String(objField.value);
	if (isWhitespace(strField)) return true;
	var i = 0;
	for (i = 0; i < strField.length; i++) {
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9') {
			alert(FieldName + " must be a valid numeric entry.  Please do not use commas or dollar signs or any non-numeric symbols.");
			objField.focus();
			return false;
		}
	}
	return true;
}

function forceMoney(objField,FieldName) {
	var strField = new String(objField.value);
	if (isWhitespace(strField))
		return true;
	var i = 0;
	for (i = 0; i < strField.length; i++) {
		if ((strField.charAt(i) < '0' || strField.charAt(i) > '9') && ((strField.charAt(i) != '.') || (strField.split('.').length > 2) || (strField.split('.')[1].length > 2))) {
			alert(FieldName + " must be a valid money numeric entry.  Please do not use commas or riggit/dollar signs.");
			$(objField.id).focus();
			return false;
		}
	}
	return true;
}
/*Old Functions. Kept here for compatibility purposes*/
function strpad(val){
	return (!isNaN(val) && val.toString().length==1)?"0"+val:val;
}
function $xml(xmlobj,tagname,no) {
	return xmlobj.getElementsByTagName(tagname).item(no).firstChild.data;
}
function showStatus(message) {
	$("status").style.visibility = "visible";
	$("status").innerHTML = '<img src="mozilla_blu.gif" /> '+message;
}

function hideStatus() {
	$("status").innerHTML = "";
	$("status").style.visibility = "hidden";
}

var popup_detailsWindow=null;
function popup_details(mypage,myname,w,h,pos,infocus){
	if (pos == 'random') {
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	else {
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
	}
	settings='width='+ w + ', height='+ h + ', top=' + TopPosition + ', left=' + LeftPosition + ', scrollbars=no, location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=no';
	popup_detailsWindow=window.open('',myname,settings);
	if(infocus=='front') {
		popup_detailsWindow.focus();
		popup_detailsWindow.location=mypage;
	}
	if(infocus=='back') {
		popup_detailsWindow.blur();
		popup_detailsWindow.location=mypage;
		popup_detailsWindow.blur();
	}
}