/*
 addOnloadEvent(myFunctionName);
Or to pass arguments
 addOnloadEvent(function(){ myFunctionName('myArgument') });
*/

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else 
      window.onload = fnc;
  }
}

function Currency(sSymbol, vValue) {
   aDigits = vValue.toFixed(2).split(".");
   aDigits[0] = aDigits[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1,").split("").reverse().join("");
   return sSymbol + aDigits.join(".");
}

/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) 
{
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function checkEnter(e) { //e is event object passed from function invocation
	var characterCode;

	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		return false;
	} else {
		return true;
	}
}

//configure status message not to show
var statusmsg=""
function hidestatus(){
	window.status=statusmsg
	return true
}

function isDocument(formElement, message) {
	var OK = new Array ("csv","doc","pdf","ppt","txt","xls");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);
	_isDocument = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isDocument = true; // one of the file extensions found
		} 
	}

	if (!_isDocument) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isDocument;
}

function isImage(formElement, message) {
	var OK = new Array ("gif","jpg","jpeg","png","wbmp");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);

	_isImage = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isImage = true; // one of the file extensions found
		} 
	}

	if (!_isImage) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isImage;
}

function isImageVal(formElement) {
	var OK = new Array ("gif","jpg","jpeg","png","wbmp");  // if you change this element, please change the config.php

	if (formElement == '') {
		return true;
	}

	var ext = getExt(formElement);

	_isImage = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isImage = true; // one of the file extensions found
		} 
	}

	return _isImage;
}

function isVideo(formElement, message) {
	var OK = new Array ("3gp","asf","asx","avi","mov","mp4","mpg","qt","rm","swf","wmv");  // if you change this element, please change the config.php

	trimElement = trim(formElement.value);
	if (trimElement == '') {
		return true;
	}

	var ext = getExt(trimElement);
	_isDocument = false;

	for (i=0; i<OK.length; i++) {
		if (OK[i] == ext) {
			_isDocument = true; // one of the file extensions found
		} 
	}

	if (!_isDocument) { 
		if (message!='') alert(message);
		formElement.focus();
	}

	return _isDocument;
}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isDecimalKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 46) // Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isCommaDecimalKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 44 || charCode == 46) // Coma or Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isDecPercentKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 37 || charCode == 46) // % or Decimal Period
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isQuantityKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 43 || charCode == 45) // + or - key
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function isPostalKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode

	if (charCode == 45) // - key
		return true;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function getExt(formElement) {
	var dot_pos = formElement.lastIndexOf(".");
	if(dot_pos == -1)
		return "";
	return formElement.substr(dot_pos+1).toLowerCase();
}

function getURL() {
	// Get URL
	if (location.href.indexOf("https://")!= -1) 
		var prefix = "https://"; 
	else 
		var prefix = "http://"; 

	if (location.href.indexOf('//') !=-1) {
		firstpos = location.href.indexOf('//')+2;
		var tmpHref = location.href.substring(firstpos);
		if (tmpHref.indexOf('/') !=-1) {
			lastpos = tmpHref.indexOf('/');
			var strHref = prefix + tmpHref.substring(0, lastpos);
		} else {
			var strHref = location.href;
		}
	} else {
		if (location.href.indexOf('/') !=-1) {
			lastpos = location.href.indexOf('/');
			var strHref = prefix + location.href.substring(0, lastpos);
		} else {
			var strHref = prefix + location.href;
		}
	}

	return strHref;
}

function clearText(field) {
    if (field.defaultValue == field.value) {
		field.value = '';
	} else if (field.value == '') {
		field.value = field.defaultValue;
		field.select();
	}
}

function makeFieldUppercase(field, len) {
	if(field.value.length == len) {
		field.value = field.value.toUpperCase();
	}
}

function toggleBox(szDivID, iState) { // 1 visible, 0 hidden
	var obj = document.layers ? document.layers[szDivID] : document.getElementById ?  document.getElementById(szDivID).style : document.all[szDivID].style;
	obj.display = document.layers ? (iState ? "show" : "hide") : (iState ? "block" : "none");
}

function popupWin(url,width,height,left,top) {
  window.open(url,"_blank","location=0,status=0,scrollbars=1,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return pad_with_zeros(result, dec);
}

function pad_with_zeros(rounded_value, decimal_places) {
	// Convert the number to a string
	var value_string = rounded_value.toString();
	// Locate the decimal point
	var decimal_location = value_string.indexOf(".");
	// Is there a decimal point?
	if (decimal_location == -1) {
		// If no, then all decimal places will be padded with 0s
		decimal_part_length = 0;
		// If decimal_places is greater than zero, tack on a decimal point
		value_string += decimal_places > 0 ? "." : "";
	} else {
		// If yes, then only the extra decimal places will be padded with 0s
		decimal_part_length = value_string.length - decimal_location - 1;
	}
	// Calculate the number of decimal places that need to be padded with 0s
	var pad_total = decimal_places - decimal_part_length;

	if (pad_total > 0) {
		// Pad the string with 0s
		for (var counter = 1; counter <= pad_total; counter++) 
			value_string += "0";
	}

	return value_string;
}

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url) {
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

