/**
* Javascript Library
* Copyright eChiron 2005
* All Rights Reserved
*
* Functions:
*
* getElement( elementId )
* getKey( eEvent )
*
*
*
*
* checkCarRegister()
*/



var browser = navigator.userAgent;
var key = 0;

var _money_separator = new String('.').charCodeAt(0);       // Separator for the money format
var _percentage_separator = new String('.').charCodeAt(0);  // Separator for the percentage

/**
 * Gets specified element by id
 */
function getElement( elementId ){
	var i, _obj;
	
	if( document.getElementById )
		_obj = document.getElementById( elementId );
	
	if( _obj == null )
		_obj = document[elementId];
	
	if( _obj == null && document.all )
		_obj = document.all[elementId];
	
	for( i = 0; !_obj && i < document.forms.length; i++ ) 
    	_obj = document.forms[i][elementId];
  
	return _obj;
}

/*
 * Gets key for the specified event
 */
function getKey( eEvent ){
	var _key;
	
	if( browser.indexOf("MSIE") != -1 || browser.indexOf("Opera") != -1 ){
		_key = eEvent.keyCode;
	}			
	else{
		_key = eEvent.charCode;
	}

	return _key;
}

/* Checks whether the value of the inputed element contains as a maximum 2 numbers after the separator */
function checkMoney( event ){

	var _key = getKey( event );
	
	if ( _key != 0 && (_key != _money_separator) && ((_key < 48) || (_key > 57))) 
		return false;
	
	
	// Checking uniqueness of the separator
	var elem = (event.target) ? event.target : event.srcElement;
	if( elem != null && _key == _money_separator ){
		if( elem.value.length == 0 || elem.value.indexOf(String.fromCharCode(_money_separator)) >= 0 )
			return false;
	}
	
	// Checking number of the digits behind the separator
	if( elem != null && _key != _money_separator && _key != 0 && elem.value.indexOf(String.fromCharCode(_money_separator)) != -1 ){
		//if( elem.value.indexOf(String.fromCharCode(_money_separator)) + 2 < elem.value.length )
		//	return false;
	}
	
	// Checking leading '0's
	
	return true;
}

/* Checks whether the value of the inputed element contains a number */
function checkNumber( event ){
	
	var _key = getKey( event );
	
	if ( _key != 0 && ((_key < 48) || (_key > 57))) 
		return false;
	
	return true;
}

/* Checks whether the value of the inputed element contains a portuguese post code */
function checkPostCode( event ){
	
	var _key = getKey( event );
	
	if( _key != 0 && _key != 45 && ((_key < 48) || (_key > 57)) ) 
		return false;
		
	// Checking uniqueness of the post code separator
	var elem = (event.target) ? event.target : event.srcElement;
	if( elem != null && _key == 45 && elem.value.length != 4 ){
		return false;
	}
	
	return true;
}


/* Checks whether the value of the inputed element contains a portuguese post code */
function checkCarRegister( event ){
	
	var _key = getKey( event );
	
	if ( _key != 0 && _key != 45 && ((_key < 48) || (_key > 57 && _key < 65 ) || (_key > 90 && _key < 97) || (_key > 127) ) ) 
		return false;
		
	// Checking uniqueness of the post code separator
	var elem = (event.target) ? event.target : event.srcElement;
	if( elem != null && _key == 45 && (elem.value.length != 2 && elem.value.length != 5) ){
		return false;
	}
	
	return true;
}

/* Checks whether the value of the inputed element contains a date */
function checkDate( event ){
	
	var _key = getKey( event );
	
	if( _key != 0 && _key != 45 && ((_key < 48) || (_key > 57)) ) 
		return false;
		
	// 
	var elem = (event.target) ? event.target : event.srcElement;
	if( elem != null && _key == 45 && (elem.value.length != 2 && elem.value.length != 5) ){
		return false;
	}
	
	return true;
}

// Gets number formatted for use as a percentage with a custom separator
function getCustomPercentage( amount, customFormat ){
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + customFormat + '00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

// Gets number formatted for use as a percentage .00
function getPercentage( amount ){
    return getCustomPercentage( amount, String.fromCharCode(_percentage_separator) );
}


// Converts value to represent the percentage and to have a ',' as a separator
function convertToRepresentation( oldFormatValue ){
	//return getPercentage( oldFormatValue ).toString().replace('.',',');
	oldFormatValue += "";
	if (oldFormatValue.indexOf(',') != -1)
		return getPercentage( oldFormatValue.replace(',','.' ) );
	else
		return getPercentage( oldFormatValue );
}

// Converts value to be used in the calculations (substitutes , for .)
function convertToCalculation( oldFormatValue ){
//	return oldFormatValue.replace(',','.');
	if (oldFormatValue.indexOf(',') != -1)
		return oldFormatValue.replace(',','.' );
	else
		return oldFormatValue;
}

// Checks where the browser is the Internet Explorer
function isMSIE(){
	return browser.indexOf("MSIE") != -1 && window.ActiveXObject && browser.indexOf("Opera") == -1;
}

// Checks where the browser is the Opera Browser
function isOpera(){
	return browser.indexOf("Opera") >= 0;
}

// Gets AJAX Object
function getHTTPObject(){
	var xmlhttp;
	
	if( window.XMLHttpRequest ){
        try{
			xmlhttp = new XMLHttpRequest();
		}
		catch( e ){ 
			alert(e); 
		}
    
    } // branch for IE/Windows ActiveX version
    else if( window.ActiveXObject ){
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	return xmlhttp;
}
