/**
 * Send JS
 * @author V. Perez
 * @copyright Copyright (c) 2008
 * @version 1.0
 */
 
 /**
 * This will return a object from a id in all browsers
 * @param {String} contains the id name
 * @return {Object}
 */
 function globalId(p_sId){
 	//Object
 	var oNewObject = null;
 	
	if(document.getElementById){
		oNewObject = document.getElementById(p_sId);
	}else if(document.all){
		oNewObject = document.all[p_sId];
	}else if (document.layers){
		oNewObject = document.layers[p_sId];
	}
	return oNewObject;
 }
 
 /**
  * This function will get the real x coordinaat of a object
  * @param {Object} p_oObject
  */
 function xLocation(p_oObject){
	var iXLocation = 0;
	if(p_oObject.offsetParent){
		while(true){
			iXLocation += p_oObject.offsetLeft;
			if(!p_oObject.offsetParent){
				break;
			}
			p_oObject = p_oObject.offsetParent;
		}
	}else if(p_oObject.x){
		iXLocation += p_oObject.x;
	}		
	return iXLocation;
 }

 /**
  * This function will get the real y coordinaat of a object
  * @param {Object} p_oObject
  */
 function yLocation(p_oObject){
	var iYLocation = 0;
	if(p_oObject.offsetParent){
		while(true){
			iYLocation += p_oObject.offsetTop;
			if(!p_oObject.offsetParent){
				break;
			}
			p_oObject = p_oObject.offsetParent;
		}
	}else if(p_oObject.y){
		iYLocation += p_oObject.y;
	}		
	return iYLocation;
 }
 
 /**
  * This will check of a value is empty
  * @example string empty = "" or ''
  * @example number empty = 0
  * @example boolean empty = false
  * @example function returns always false;
  * @example undefined is always empty
  * 
  * Supported objects
  * @example Array empty = Array();
  * @example HTMLSelectElement empty = if value = 0 or ''
  * @example HTMLInputElement empty = if value = '';
  * @example HTMLTextAreaELement empty = if value = '';
  * 
  * @param {Mixed} p_vValue
  * @return {Boolean} true if value is empty else, true
  */
 function empty(p_vValue){
 	var sDataType = typeof p_vValue;
	var bReturn = false;
	/**
	 * This function will check of a array is empty
	 */
	ArrayCheck = function(){
		if(p_vValue.length == 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * This function will check of a html Select items value is empty or 0
	 */
	HTMLSelectElementCheck = function(){
		var sCheckValue = p_vValue.options[p_vValue.selectedIndex].value;
		if(sCheckValue == 0 || sCheckValue == ''){
			return true;
		}else{
			return false;		
		}		
	}
	/**
	 * This function whill check of a html input or textarea values empty is
	 */
	HTMLInputElementCheck = function(){
		var sCheckValue =  p_vValue.value;
		if(sCheckValue == ''){
			return true;
		}else{
			return false;		
		}	
	}
	
	/**
	 * Start search for data type
	 */
 	switch(sDataType){
		case 'number':{
			if(p_vValue == 0){
				bReturn = true;
			}
		}
		break;
		
		case 'string':{
			if(p_vValue == '' || p_vValue == null){
				bReturn = true;
			}
		}
		break;
		
		case 'boolean':{
			if(!p_vValue){
				bReturn = true;
			}
		}
		break;
		
		case 'object':{
			try{
				//Array
				if (p_vValue instanceof Array) {
					bReturn = this.ArrayCheck();
				}
			}catch(sError){}
			
			try{
				switch(p_vValue.type){
					case 'select-one':{
						bReturn = this.HTMLSelectElementCheck();
					}
					break;
					
					case 'text':{
						bReturn = this.HTMLInputElementCheck();
					}
					break;
					
					case 'textarea':{
						bReturn = this.HTMLInputElementCheck();
					}
					break;
					
					default:{
						alert(p_vValue.type);
					}
				}				
			}catch(sError){}
			
			if(p_vValue == null){
				bReturn = true;
			}
			
		}
		break;
		
		case 'undefined':{
			bReturn = true;
		}
		break;
		
		default:{
			alert(sDataType);
		}
	}
	return bReturn;
 }

 /**
  * Cross-browser indexOf
  * @param {Object} p_oObject
  */
 if (!Array.prototype.indexOf){
	Array.prototype.indexOf = function(p_oObject){
	    var iTotal = this.length;
	    var iFrom = Number(arguments[1]) || 0;
		var iReturnValue = -1;
		
	    if (iFrom < 0) {
			iFrom = Math.ceil(iFrom);
		}else{
	        iFrom = Math.floor(iFrom);
		}
		
	    if (iFrom < 0) {
			iFrom += iTotal;
		}
	
	    for (; iFrom < iTotal; iFrom++) {
			if (iFrom in this && this[iFrom] === p_oObject) {
				iReturnValue = iFrom;
			}
		}
	    return iReturnValue;
  	}
 }
 
 number_format = function(p_iNumber, p_iDecimals){
 	var sReturn = '';
 	if(/\.+/.test(p_iNumber)){
		p_iNumber = p_iNumber.toString();
		var aSplit = p_iNumber.split('.');
		sReturn = aSplit[0] + '.';
		sReturn += aSplit[1].substring(0, 2);
		
	}else{
		sReturn = p_iNumber + '.';
		for(i=0; i < p_iDecimals; i++){
			sReturn += '0'; 	
		} 
	}
	return sReturn;
 }
 
 function imgToBigger(p_iImage){
	var oImg = new Image();
	var oLoadImg = new Image();
 	oLoadImg.src = '/images/loading.gif';
 	oLoadImg.className = 'preload';
	var iEndXLocation = (document.body.clientWidth - 200) / 2;
	var iEndYLocation = (document.body.offsetHeight - 100) / 2;
	var oLoadingText = document.createTextNode('Bezig met laden...');
	var oImgDiv = document.createElement('div');
	var oPopup = new Popup();
	oPopup.show();
	
	
	oImg.src = p_iImage;
	oImg.onclick = function(){
		oPopup.close();
	}	
	
	preload = function(){
		if(oImg.complete){
			try{
				oImgDiv.removeChild(oLoadImg);
				oImgDiv.removeChild(oLoadingText);
				oImgDiv.style.background = '';		
				show();
			}catch(e){
				alert(e);
			}
		}else{
			setTimeout('preload()', 1000);
		}
	}
	
	show = function(){
		iEndXLocation = (document.body.clientWidth - oImg.width) / 2;
		iEndYLocation = (document.body.offsetHeight - oImg.height) / 2;
		oImgDiv.style.top = iEndYLocation + 'px';
		oImgDiv.style.left = iEndXLocation + 'px'
		oImgDiv.appendChild(oImg);
	}
	
	oImgDiv.className = 'tipToBigger';
	oImgDiv.style.top = iEndYLocation + 'px';
	oImgDiv.style.left = iEndXLocation + 'px';
	oImgDiv.style.width = '200px';
	oImgDiv.style.height = '100px';
	//IE check
	if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)){
		oImgDiv.style.position = 'fixed';
	}
	
	//start popup
	preload();
	oPopup.oBlock.onclick = function(){
		oPopup.close();
	}	 
	
	oImgDiv.style.zIndex = '1000';	
	oPopup.oPopup.appendChild(oImgDiv);	
	oImgDiv.appendChild(oLoadImg);
	oImgDiv.appendChild(oLoadingText);
 }

