/****************************************************************************
 *	Copyright (C) 2007 FunnyFoxGroup. All Rights Reserved.
 *	The following is Sample Code and is subject to all restrictions on
 *	such code as contained in the End User License Agreement accompanying
 *	this product.
 ****************************************************************************/
 
 
function stdzSlashes(dm) {
	var len = dm.elements.length;
	var i = 0;
	for(i = 0; i < len; i++) {
		if( dm.elements[i].value ) {
			dm.elements[i].value = dm.elements[i].value.replace(RegExp("\'{1}" , "g"), "\\\'");
		}
	}
	return true;
}


function addSlashes( sText ) {
	if( sText) {
		return sText.replace(RegExp("\'{1}" , "g"), "\\\'");
	} else
		return sText;
}


function isNaturalNumber( sText ) {
    var re = /^[\d]+$/;
	return re.test( sText );
}


function isIntNumber( sText ) {
    if( sText.toString( ) == '-0' ) return false;
	
	var re = /^\-?[\d]+$/;
	return re.test( sText );
	
}

 
function isFloatNumber( sText ) {
	if( sText.toString( ) == '-0' ) return false;
	
	var re = /^\-?[\d]+$/;
	if( re.test( sText ) ) return true;
	re = /^\-?[\d]+\.[\d]+$/;
	return re.test( sText );
} 


function isEmpty( sText ) {
	if( !sText ) return false;
    
	return true;
}


function isNumberFormat( sText, sFormat ) {
	var specCharPattern = /[\D]/;
	var specChar = sFormat.match( specCharPattern );
	var aNum = sFormat.split( specChar );
	
	var sCmd = "var re = /^";
	for( var iC = 0; iC < aNum.length; iC++ ) {
		if( iC != 0 )
			sCmd += "\\" + specChar; 
		sCmd += "[0-9]{" + aNum[iC] + "}";
	}
	sCmd += "$/;";
	eval( sCmd );
	return re.test( sText );
}


// validates that the entry is formatted as an email address
function isEmail( sText ) {
    var str = sText;
	if(str == "") {
        //alert("Verify the email address format.");
        return false;
    }
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        //alert("Verify the email address format.");
        return false;
    } else {
        return true;
    }
}


function isValidInput( ) {
	var sText = "";
	var bEmptyAllowed = true;
	var bSpaceAllowed = true;
	
	switch( arguments.length ) {
		case 1:
			sText = arguments[0];
			break;
		case 2:
			sText = arguments[0];
			bEmptyAllowed = arguments[1];
			break;
		case 3:
			sText = arguments[0];
			bEmptyAllowed = arguments[1];
			bSpaceAllowed = arguments[2];
			break;
	}
	
	if( bEmptyAllowed ) {
		if( bSpaceAllowed )
			var re = /^[a-zA-Z0-9_\-\s]*$/;
		else 
			var re = /^[a-zA-Z0-9_\-]*/;
	}
	else {
		if( bSpaceAllowed )
			var re = /^[a-zA-Z0-9_\-\s]+$/;
		else 
			var re = /^[a-zA-Z0-9_\-]+$/; 
	}
	
	return re.test( sText );
			
}


function printPage() { print(document); }


function getInput( p_sFormId, p_sInputName ) {
	var oForm = document.getElementById( p_sFormId );
	eval( "var vInputVal = oForm." + p_sInputName + ".value;" );
	return vInputVal;
}


function getCheckBox( p_sFormId, p_sCheckBoxName ) {
	var oForm = document.getElementById( p_sFormId );
	eval( "var aChkBox = oForm." + p_sCheckBoxName + ";" );
	if( aChkBox.checked ) return aChkBox.value;
	else return "";
}


function getRadio( p_sFormId, p_sRadioName ) {
	var oForm = document.getElementById( p_sFormId );
	
	var checkedVal;
	eval( "var aRadio = oForm." + p_sRadioName + ";" );
	if( aRadio.length ) {
		for( var iC = 0; iC < aRadio.length; iC++ ) {
			if( aRadio[iC].checked ) {
				checkedVal = aRadio[iC].value;
				break;
			}
		}
	} else {
		if( aRadio.checked ) {
			checkedVal = aRadio.value;
		}
	}
	
	return checkedVal;
}


/*
 *	Param:
 *		p_sGName: multiple select box name(not including '[]')
 *	Return:
 *		option array (option within the multiple select box)
 *		checked option number
 *		unchecked option number
 *		checked values array
 */
function getOptionGroup( p_sGName ) {
	var oSelect = document.getElementById( p_sGName + '[]' );
	var aInput = oSelect.getElementsByTagName( "option" );
	var aCheckOption = new Array( );
	var aCheckedValue = new Array( );
	var aTemp = p_sGName.split( "[]" );
	var sGName = aTemp[0];
	
	var iNum = 0;
	var iCheckedNum = 0;
	var iUncheckedNum = 0;
	for( var iC = 0; iC < aInput.length; iC++ ) {
		aCheckOption[iNum++] = aInput[iC];
		if( aInput[iC].selected == true ) {
			aCheckedValue[iCheckedNum] = aInput[iC].value;
			iCheckedNum ++;
		}
		else
			iUncheckedNum++;
	}
	
	return Array( aCheckOption, iCheckedNum, iUncheckedNum, aCheckedValue );
}


//-- Functions set controls checkbox groups interaction
/*
 *	Call on 'checkall' checkbox
 *	Param:
 *		p_sGName: checkbox group name(including '[]')
 */
function checkAll( p_sGName ) {
	var aTemp2 = p_sGName.split( "[]" );
	var sGName = aTemp2[0];
	var oCheckAll = document.getElementById( sGName + "All" );
	
	var aTemp = getCheckGroup( p_sGName );
	var aCheckbox = aTemp[0];
	var iCheckedNum = aTemp[1];
	
	for( var iC = 0; iC < aCheckbox.length; iC++ ) {
		aCheckbox[iC].checked = oCheckAll.checked;
	}
	
}


/*
 *	Call on each checkbox in group
 *	Param:
 *		p_sGName: checkbox group name(including '[]')
 *		p_vValue: checkbox value
 */
function checkOne( p_sGName, p_vValue ) {
	var aTemp2 = p_sGName.split( "[]" );
	var sGName = aTemp2[0];
	var oCheckAll = document.getElementById( sGName + "All" );
	
	var aTemp = getCheckGroup( p_sGName );
	var aCheckbox = aTemp[0];
	var iCheckedNum = aTemp[1];	
	if( iCheckedNum == aCheckbox.length )
		oCheckAll.checked = true;
	else
		oCheckAll.checked = false;
}


/*
 *	Param:
 *		p_sGName: checkbox group name(not including '[]')
 *	Return:
 *		checkbox array(checkboxes in the group)
 *		checked checkbox number
 *		unchecked checkbox number
 *		checked values array
 */
function getCheckGroup( p_sGName ) {
	var aInput = document.getElementsByTagName( "input" );
	var aCheckbox = new Array( );
	var aCheckedValue = new Array( );
	var aTemp = p_sGName.split( "[]" );
	var sGName = aTemp[0];
	
	var iNum = 0;
	var iCheckedNum = 0;
	var iUncheckedNum = 0;
	for( var iC = 0; iC < aInput.length; iC++ ) {
		if( aInput[iC].type == "checkbox" && aInput[iC].name == sGName + "[]" ) {
			aCheckbox[iNum++] = aInput[iC];
			if( aInput[iC].checked == true ) {
				aCheckedValue[iCheckedNum] = aInput[iC].value;
				iCheckedNum ++;
			}
			else
				iUncheckedNum++;
		}
	}
	
	return Array( aCheckbox, iCheckedNum, iUncheckedNum, aCheckedValue );
}
function setHomepage()
{		
 if (document.all)
    {
      document.body.style.behavior='url(#default#homepage)';
  	  document.body.setHomePage('http://www.ameco.vn');
    }
    else if (window.sidebar)
    {
    if(window.netscape)
		{
			try
		{  
			 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
		}  
			catch(e)  
			 {  
				alert("this action was aviod by your browser,if you want to enable,please enter about:config in your address line,and change the value of signed.applets.codebase_principal_support to true");  
			 }
		} 
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage','http://www.ameco.vn');
	 }

}
//-- End
