// JavaScript Document
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var opened;
var executeOnceCloseFloatPanelScript;	// The script only execute once on close the float panel

function showPopup(url, w, h){
	if (opened ){
		opened.close();
	}
	var canResize = ( arguments.length > 3 ) ? arguments[3] : 1;
	var canScroll = ( arguments.length > 4 ) ? arguments[4] : 0;
	
	var y = ( window.screen.availHeight - h ) / 2;
	var x = ( window.screen.availWidth - w ) / 2;
	
	var config = 'top='+y+',left='+x+',height='+h+',width='+w+',toolbar=0,location=0,statusbar=0,menubar=0,scrollbars='+canScroll+',resizable='+ canResize;
	opened = window.open(url, '_popup', config);
	opened.focus();
}

function trim(input){
	var output = input.replace(/^\s+|\s+$/g, '');
	return output;
}

/*====================================================================
 * To remove a item from a array.
 * retrun is found the item in the array.
 *====================================================================*/
function arrayRemoveItem( theArray, toRemoveItem ){
	var found = false;
	for ( var i=0; i< theArray.length; i++ ){
		if ( theArray[i] == toRemoveItem )
			found = true;
		if ( found && (i+1 < theArray.length) )
			theArray[i] = theArray[i+1];
	}
	if ( found )
		theArray.length -= 1;
	return found;
}

/*====================================================================
 * Set the Object property, and return the object
 *====================================================================*/
function setObjProperty( objId, property, value ){
	var obj = document.getElementById( objId );
	if ( obj ){
		eval('if (obj.'+property+') obj.' + property +" = value;" );
		return obj;
	}
	return null
}

/*====================================================================
 * Check TextField
 *====================================================================*/
function checkTextFieldNotEmpty( textFldId ){
	var obj = document.getElementById( textFldId );
	if ( (obj == null) || (obj.value.length == 0) ){
		if ( (arguments.length > 1) && (arguments[1].length > 0) )
			alert( arguments[1] );
		if (obj != null) obj.focus();
		return false;
	}
	return true;
}

function checkEmailFormat( fieldId ){
	var obj = document.getElementById( fieldId );
	if ( (obj == null) || (obj.value.match(/^[a-zA-Z0-9\-\._]+@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]+$/) == null) ){
		if ( (arguments.length > 1) && (arguments[1].length > 0) )
			alert( arguments[1] );
		if (obj != null) obj.focus();
		return false;
	}
	return true;
}

function checkPhoneNoFormat( fieldId ){
	try{
		var obj = document.getElementById( fieldId );
	}catch( ex ){
		return false;
	}
	if ( (obj == null) || obj.value.match(/^((\([0-9]+\)){0,1}(\+[0-9]+){0,1}){0,1}[0-9 \-]+$/) == null ){
		if ( (arguments.length > 1) && (arguments[1].length > 0) )
			alert( arguments[1] );
		obj.focus();
		return false;
	}
	return true;
}

/*====================================================================
 * Format a number
 *====================================================================*/
function number_format( number, decimals, dec_point, thousands_sep ) {
	var n = number, prec = decimals;
	
	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	
	var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
	var dec = (typeof dec_point == "undefined") ? '.' : dec_point;
	
	var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
	
	var abs = Math.abs(n).toFixed(prec);
	var _, i;
	
	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;
		_[0] = s.slice(0,i + (n < 0)) +
		_[0].slice(i).replace(/(\d{3})/g, sep+'$1');
		s = _.join(dec);
	} else {
		s = s.replace('.', dec);
	}
	
	return s;
}

/*====================================================================
 * Get the bounds
 *====================================================================*/
function getBounds( obj ){
	var t = obj.offsetTop;
	var l = obj.offsetLeft;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	var preObj = null;

	while ( obj=obj.offsetParent ){
		if ( obj.tagName.toUpperCase() == 'TABLE' ){
			var cIndex = preObj.cellIndex;
			for ( var i=0 ; i<obj.rows.length ; i++ ){
				if ( obj.rows[i][cIndex] == preObj ){
					for ( var j=0; j<cIndex; j++){
						l +=  obj.rows[i][j].offsetLeft;
					}					
					break;
				}
			}
		}
		t += obj.offsetTop;
		l += obj.offsetLeft;
		
		if ( obj.tagName.toUpperCase() == 'TD' ){
			preObj = obj;
		}
	}

	return {top:t, left:l, width:w, height:h}
}

/*====================================================================
 * Check is key exists in array
 *====================================================================*/
function arrayKeyExists( array, key ){
	for ( k in array ){
		if ( key == k ) return true;
	}
	return false;
}

/*====================================================================
 * Set the selected value of the select list
 *====================================================================*/
function setSelectedValue( compId, vals ){
	var obj = document.getElementById( compId );
	if ( obj ) {
		var sel = vals.split(';');
		for( var i=0; i<obj.options.length; i++){
			obj.options[i].selected = false;
			for ( var j=0; j<sel.length; j++ ){
				if ( obj.options[i].value == sel[j] )
					obj.options[i].selected = true;
			}
		}
	}
}

/*====================================================================
 * Set the selected value of the radio button group
 *====================================================================*/
function setRadioValue( compName, val ){
	var objs = document.getElementsByName( compName );
	if ( objs != null ){
		for ( var i=0; i<objs.length; i++ ){
			objs[i].checked = ( objs[i].value == val );
		}
	}
}

/*====================================================================
 * Show Floating Panel
 *====================================================================*/
function showFloatPanel( content ){
	var ieFix = document.getElementById('iefix_panel');
	if ( ieFix == null ){
		ieFix = document.createElement('div');
		ieFix.setAttribute('id', 'iefix_panel');
		document.body.appendChild( ieFix );
		
		ieFix.style.backgroundColor ='#000000';
		ieFix.style.opacity = 0.1;
		ieFix.style.filter = "alpha(opacity=10);";
		ieFix.style.position = 'absolute';
		ieFix.style.zIndex = 20;
		ieFix.style.top = 0;
		ieFix.style.left = 0;
	}
	
	var shadow = document.getElementById('float_panel_shadow');
	if ( shadow == null ){
		shadow = document.createElement('div');
		shadow.setAttribute('id', 'float_panel_shadow');
		document.body.appendChild( shadow );
		
		shadow.style.backgroundColor ='#000000';
		shadow.style.opacity = 0.5;
		shadow.style.filter = "alpha(opacity=50);";
		shadow.style.position = 'absolute';
		shadow.style.zIndex = 21;
		shadow.style.top = 0;
		shadow.style.left = 0;
		shadow.onclick = closeFloatPanel;
	}
	var scrMaxH = ( document.body.scrollHeight > document.body.clientHeight ) ? document.body.scrollHeight : document.body.clientHeight;

	shadow.style.height = scrMaxH + 'px';
	shadow.style.width = document.body.offsetWidth+'px';
	shadow.style.display = 'block';
	
	var contentObj = document.getElementById('float_panel_content');
	if ( contentObj == null ){
		contentObj = document.createElement('div');
		contentObj.setAttribute('id', 'float_panel_content');
		document.body.appendChild( contentObj );
		contentObj.style.position = 'absolute';
		contentObj.style.backgroundColor ='#FFFFFF';
		contentObj.style.zIndex = 22;
		contentObj.style.top = 0;
		contentObj.style.left = 0;
	}
	contentObj.innerHTML = content;
	contentObj.style.display = 'block';

	resetPanelPosition();
}

/*====================================================================
 * Close Floating Panel
 *====================================================================*/
function closeFloatPanel(){
	var shadow = document.getElementById('float_panel_shadow');
	if ( shadow ){
		shadow.style.height = '0px';
		shadow.style.display = 'none';
	}
	var ieFix = document.getElementById('iefix_panel');
	if ( ieFix ){
		ieFix.style.height = '0px';
		ieFix.style.display = 'none';
	}
	var contentObj = document.getElementById('float_panel_content');
	if ( contentObj ){
		contentObj.style.display = 'none';
	}
	
	eval( executeOnceCloseFloatPanelScript );
	executeOnceCloseFloatPanelScript = '';
}

/*====================================================================
 * Reset Floating Panel position
 *====================================================================*/
function resetPanelPosition(){
	var contentObj = document.getElementById('float_panel_content');
	if ( contentObj && (contentObj.style.display == 'block')){
		var top = ( contentObj.offsetHeight < document.body.clientHeight ) ? ((document.body.clientHeight - contentObj.offsetHeight) / 2) : 0;
		var left = ( contentObj.offsetWidth < document.body.clientWidth ) ? ((document.body.clientWidth - contentObj.offsetWidth) / 2) : 0; 
		contentObj.style.top = document.body.scrollTop + top;
		contentObj.style.left = document.body.scrollLeft + left;
	}
}


// Attach the event
if ( window.addEventListener ){	//W3C standard
	window.addEventListener('load', resetPanelPosition, false);
	window.addEventListener('scroll', resetPanelPosition, false);
} else if ( window.attachEvent ){	// Miscrosoft IE only
	window.attachEvent('onload', resetPanelPosition);
	window.attachEvent('onscroll', resetPanelPosition);
}
