/*check browser type*/

function getBrowserName()
{
	/**
	* @returns A string which specifies which is the current
	* browser in which we are running.
	*
	* Currently-supported browser detection and codes:
	* * 'opera' -- Opera
	* * 'msie' -- Internet Explorer
	* * 'safari' -- Safari
	* * 'firefox' -- FireFox
	* * 'mozilla' -- Mozilla
	*
	* If we are unable to property identify the browser, we
	* return an empty string.
	*
	* @type String
	*/ 
	
	var browserName = "";
	
	var ua = navigator.userAgent.toLowerCase();
	if ( ua.indexOf( "opera" ) != -1 ) {
	browserName = "opera";
	} else if ( ua.indexOf( "msie" ) != -1 ) {
	browserName = "msie";
	} else if ( ua.indexOf( "safari" ) != -1 ) {
	browserName = "safari";
	} else if ( ua.indexOf( "mozilla" ) != -1 ) {
	if ( ua.indexOf( "firefox" ) != -1 ) {
	browserName = "firefox";
	} else {
	browserName = "mozilla";
	}
	}
	
	return browserName;	
}

/*show hide menu */

 function MM_showHideLayers(name, show)
 { //alert(name)
 	var element = document.getElementById(name);
 	if(element){
	 	if(show == "show") {
			element.style.display = "block";			
		}else{
			element.style.display = "none";
		}
 	}
 }
 
 function menuClose(name)
 {
 	var element = document.getElementById(name);
 	if(element)
 		element.style.display = "none";
 }
 
 /*used for show help module popup window */
 function help(from, dir, anchor)
 {
	//alert("me")
		window.open(dir + "help/help.php?from=" + from + "&anchor=" + anchor, "tinyWindow", 'width=680,height=580, scrollbars=yes')	
		return false;	
 }

/* used in invoice_add page to calculate the result and show on the page.*/
function calculateSum()
	{			
		if(!document.getElementById("os").innerHTML) document.getElementById("os").innerHTML = 0;
		if(!document.getElementById("thours").innerHTML) document.getElementById("thours").innerHTML = 0;
		if(!document.getElementById("kms").innerHTML) document.getElementById("kms").innerHTML = 0;
		if(!document.getElementById("backup").innerHTML) document.getElementById("backup").innerHTML = 0;
		if(!document.getElementById("download").innerHTML) document.getElementById("download").innerHTML = 0;
		if(!document.getElementById("wcd").innerHTML) document.getElementById("wcd").innerHTML = 0;
		if(!document.getElementById("tollcall").innerHTML) document.getElementById("tollcall").innerHTML = 0;
		
		result1 = parseInt(document.getElementById("os").innerHTML) * parseFloat(document.getElementById("os_rate").value);
		result2 = parseFloat(document.getElementById("thours").innerHTML) * parseFloat(document.getElementById("thours_rate").value);
		result3 = (parseFloat(document.getElementById("kms").innerHTML) - 25) * parseFloat(document.getElementById("kms_rate").value);			
		if(result3 < 0) result3 = 0;			
		result4 = parseInt(document.getElementById("backup").innerHTML) * parseFloat(document.getElementById("backup_rate").value);			
		result5 = parseInt(document.getElementById("download").innerHTML) * parseFloat(document.getElementById("download_rate").value);
		result6 = parseInt(document.getElementById("wcd").innerHTML) * parseFloat(document.getElementById("wcd_rate").value);
		result7 = parseFloat(document.getElementById("tollcall").innerHTML) * parseFloat(document.getElementById("tollcall_rate").value)
		
		document.getElementById("os_sum").innerHTML	= '$' + result1;
		document.getElementById("thours_sum").innerHTML = '$' + result2;
		document.getElementById("kms_sum").innerHTML = '$' + result3;
		document.getElementById("backup_sum").innerHTML = '$' + result4;
		document.getElementById("download_sum").innerHTML = '$' + result5;
		document.getElementById("wcd_sum").innerHTML = '$' + result6;
		document.getElementById("tollcall_sum").innerHTML = '$' + result7		
		document.getElementById("subtotal").innerHTML = '$' + (result1 + result2 + result3 + result4 + result5 + result6 + result7)		
	}		

	/*this function can add new row or column in the table, not be used at the moment. But quite usable in the future.*/
	function addRows()
	{
		//document.getElementById("me").style.display = "block";
		
		var theTable = document.getElementById("newTable");	
		var therow = document.createElement('tr')
		var cell = document.createElement('td')					
		cell.appendChild(document.createTextNode('Tbody cell text1'));		
		therow.appendChild(cell)
		var cell = document.createElement('td')					
		cell.appendChild(document.createTextNode('Tbody cell text2'));	
		therow.appendChild(cell)
		theTable.appendChild(therow);
	}
				
	function addColumes()
	{
		for( var x = 0; x < theTable.tHead.rows.length; x++ ) {
			var y = document.createElement('td');
			y.appendChild(document.createTextNode('Thead cell text'));
			theTable.tHead.rows[x].appendChild(y);
		}
	
		for( var z = 0; z < theTable.tBodies.length; z++ ) {
			for( var x = 0; x < theTable.tBodies[z].rows.length; x++ ) {
				
				var y = document.createElement('td');
				y.appendChild(document.createTextNode('Tbody cell text'));
				theTable.tBodies[z].rows[x].appendChild(y);
			}
		}
	
		for( var x = 0; x < theTable.tFoot.rows.length; x++ ) {
			var y = document.createElement('td');
			y.appendChild(document.createTextNode('Tfoot cell text'));
			theTable.tFoot.rows[x].appendChild(y);
		}
			
	}
	
	function check_input(inputString, type, field)
	{//alert(inputString)
		 inputString = trim(inputString);
		 if(inputString.length < 0){
			alert("please fill up the field: " + field)
			return false;
		 }else{			 		
			if(type == "address"){
				 var objReg = /^[\w|\s|\(|\)|,|.|\'|\"|&|-]+$/;				
			}else if(type == "phone"){
				var objReg = /^\+?([0-9]|\(|\)|-){6,15}$/;						
			}else if(type == "email"){						
				 var objReg = /^\w+([-.]\w+)*@(\w+[-.]?)+\.[a-zA-Z]{2,3}$/;				 			
			}else if(type == "username"){
				 var objReg = /^\w+([\w|\'|\"|&|-|.])+$/;											
			}else if(type == "number"){
				 var objReg = /(^-?\d\d*$)/;				
			}else if(type == "text"){		
				 var objReg = /^[\w|\s|\'|\"|&|-|.]+$/;				
			}else if(type == "note"){
				var objReg = /^(.)[^#][^<][^>]+$/;
			}else{
				var objReg = new RegExp();			
			}
			if(objReg.test(inputString)) return true;
			else {alert(field + " Field Input Error."); return false;}
		 }
	}
	
	//============================================================
	// trim() - Strip leading and trailing spaces
	// Argument: A string
	// Returns: The string with leading and trailing spaces trimmed off	
	//============================================================
	function trim (zsStr)
	{
	  // get xiStart = position of first non-space char in zsStr
	  for (var xiStart = 0 ; xiStart < zsStr.length ; xiStart++)
		if (zsStr.charAt(xiStart) != " ") break;		 
	
	  // if zsStr consists of all spaces, return ""
	  if (xiStart == zsStr.length)return "";		
	
	  // get xiEnd = position of last non-space char in zsStr
	  for (var xiEnd = zsStr.length-1 ; xiEnd >= 0 ; xiEnd--)
		if (zsStr.charAt(xiEnd) != " ")  break;		
	
	  // return desired substring
	  return zsStr.substring (xiStart, xiEnd+1);
	}
