var corex = {

	/**
	  * Internal CORE-X JavaScript Library, a set of few usefull functions ;-) 
	  *************************************************************************************/
	INFO : "COREX jsLibrary v.1.01-26/08/2008 (c)2007 dendy@dendy.sk",
	
	/**
	  * browser =type variables .. they are initialised automaticaly during loading this file, by "corex.browserTypeCheck()" function
	  */
	win : 0,
	mac : 0,
	lnx : 0,
	ice : 0,
	ie  : 0,
	ie4 : 0,
	ie5 : 0,
	ie6 : 0,
	op5 : 0,
	op6 : 0,
	op7 : 0,
	ns4 : 0,
	ns6 : 0,
	ns7 : 0,
	mz7 : 0,
	kde : 0,  
	saf : 0,
	
	/**
	  * mouse cursor absolute position. it's loaded by corex.getMouseXY() function, durin initialisation
	  * of page this function is put into document.onmousemove= .. so if you overwrite this handler
	  * with you own function, these two valies will be no more available, or value here will be not corresponding
	  * with actual mouse cursor poistion
	  */
	mouseX : 0,
	mouseY : 0,
	
	/** get fckeditoc content - this function reads selected fckeditor data. useable
	  *                         when submitting some form containing fckeditor through
	  *						    xajax (you need to read fckedtiro content with this function,
	  *                         save it to some hidden input box for exampe, and then subit
	  */
	getFCKcontent : function (editor_name) {
	
	    var oEditor = FCKeditorAPI.GetInstance(editor_name) ;
	    if (oEditor.EditorDocument.body.innerHTML) { return oEditor.GetXHTML(); }
	    else return '';
	    
	},
	
	/**
	  * function for submiting form if enter is pressed over selected inputbox
	  * simply put into seleted elemnt: onClick="corex.submitIfEnter(this,event)" and you have it ;)
	  */
	submitIfEnter : function (myfield,e) {
	
		var keycode;
		
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
	
		if (keycode == 13) {
		   myfield.form.submit();
		   return false;
		} else {
		   return true;
		}
		
	},
	
	/**
	  * reads some html selectobx (with given id) and returns the value of selected idem
	  */
	getSelectedValueText : function (inputBoxId) {
		
		var tmp    = document.getElementById(inputBoxId);
		var retVal = '';
		for (i=0;i<tmp.length;i++) {
			if (tmp[i].value==tmp.value) retVal = tmp[i].text; 
		}
		
		return(retVal);
		
	},
	
	/**
	  * checks if a value exists in an array
	  */ 
	inArray : function (value,haystack) {
		
	  for(var i=0;i<haystack.length;i++) {
	    if (haystack[i]==value) return true;
	  }
	  return false;
	  
	},

	/**
	  * searches the array for a given value and returns the corresponding key if successful
	  * if not successfull (value not found), will return NULL
	  */
	arraySearch : function (value,haystack)  {
		
	  for(var i=0;i<a.length;i++) {
	    if (haystack[i]==value) return i;
	  }
	  return null;
	  
	},
	
	/**
	  * this function will check string in 'email' variable if it's valid e-mail format 
	  */
	checkMail : function (email) {
		 
		 var regExpEmail=/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}/;

		 if(!regExpEmail.exec(email)) return(false); else return(true);
		 
	},
	
	/**
	  * this is for replacing some substring (all occurences) with other string
	  */
	replaceSubstring : function (haystack, value, replacement) {
		
	   var output = haystack;
	   if (value == "") {
	      return haystack;
	   }
	   if (replacement.indexOf(value) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
	      while (output.indexOf(value) != -1) {
	         var toTheLeft = output.substring(0, output.indexOf(value));
	         var toTheRight = output.substring(output.indexOf(value)+value.length, output.length);
	         output = toTheLeft + replacement + toTheRight;
	      }
	   } else { 
	   	  var midStrings = new Array("~", "`", "_", "^", "#");
	      var midStringLen = 1;
	      var midString = "";
	      while (midString == "") {
	         for (var i=0; i < midStrings.length; i++) {
	            var outputMidString = "";
	            for (var j=0; j < midStringLen; j++) { outputMidString += midStrings[i]; }
	            if (value.indexOf(outputMidString) == -1) {
	               midString = outputMidString;
	               i = midStrings.length + 1;
	            }
	         }
	      } 
	      while (output.indexOf(value) != -1) {
	         var toTheLeft = output.substring(0, output.indexOf(value));
	         var toTheRight = output.substring(output.indexOf(value)+value.length, output.length);
	         output = toTheLeft + midString + toTheRight;
	      }
	      while (output.indexOf(midString) != -1) {
	         var toTheLeft = output.substring(0, output.indexOf(midString));
	         var toTheRight = output.substring(output.indexOf(midString)+midString.length, output.length);
	         output = toTheLeft + replacement + toTheRight;
	      }
	   } 
	   return output; 
	   
	},
	
	/**
	  * wrapper for window.open() function, no special functionality here ;-) useable
	  * in some speciffic cases when direct calling window.open() isn't good 
	  * (for exampel in <a href=".."> tag)
	  */
	windowOpen : function (url,windowName,params) {
		
		window.open(url,windowName,params);
		
	},

	/**
	  * used by CONTENTS plugin for submiting content-inline form
	  */
	contentsMailerSubmit : function (form_id,mandatory_fields,mandatory_fields__error_message,bad_email__error_message) {
		
		var mF           = mandatory_fields.split(','); 
		var allFilled    = 1;
		var emailInvalid = 0;
		
		for(i=0;i<mF.length;i++) {
		
			var tmp = '_' + mF[i];
			if (tmp.indexOf('email')>0) {
				if (document.forms[form_id][mF[i]]!=undefined) {

					var regExpEmail=/^.+@.+\..+$/;
					
					var o = document.forms[form_id][mF[i]];
					if(!regExpEmail.exec(o.value)) {
						emailInvalid = 1;			 	
					}
					
				}
			}
			
			if (document.forms[form_id][mF[i]]==undefined) alert('ERROR :: contentsMailerSubmit() :: Send variable not found : ' + mF[i]);
			if (document.forms[form_id][mF[i]].value=='')  allFilled = 0;
			
		}
		
		if (mandatory_fields__error_message=='') mandatory_fields__error_message = "ERROR: SOME MANDATORY FIELDS EMPTY !";
		if (bad_email__error_message=='')        bad_email__error_message        = "ERROR: INCORRECT E-MAIL COLUMN FORMAT !";
		
		if      (allFilled!=1)    alert(mandatory_fields__error_message);
		else if (emailInvalid==1) alert(bad_email__error_message);
		else                      {
			document.getElementById(form_id).action = '';
			document.getElementById(form_id).submit();
		}
		
	},
	
	browserTypeCheck : function () {
		
		var exclude=1;
		var agt=navigator.userAgent.toLowerCase();
		var win=0;var mac=0;var lin=1;
		if(agt.indexOf('win')!=-1){win=1;lin=0;}
		if(agt.indexOf('mac')!=-1){mac=1;lin=0;}
		var lnx=0;if(lin){lnx=1;}
		var ice=0;
		var ie=0;var ie4=0;var ie5=0;var ie6=0;var com=0;var dcm;
		var op5=0;var op6=0;var op7=0;
		var ns4=0;var ns6=0;var ns7=0;var mz7=0;var kde=0;var saf=0;
		if(typeof navigator.vendor!="undefined" && navigator.vendor=="KDE"){
			var thisKDE=agt;
			var splitKDE=thisKDE.split("konqueror/");
			var aKDE=splitKDE[1].split("; ");
			var KDEn=parseFloat(aKDE[0]);
			if(KDEn>=2.2){
				kde=1;
				ns6=1;
				exclude=0;
				}
			}
		else if(agt.indexOf('webtv')!=-1){exclude=1;}
		else if(typeof window.opera!="undefined"){
			exclude=0;
			if(/opera[\/ ][5]/.test(agt)){op5=1;}
			if(/opera[\/ ][6]/.test(agt)){op6=1;}
			if(/opera[\/ ][7-9]/.test(agt)){op7=1;}
			}
		else if(typeof document.all!="undefined"&&!kde){
			exclude=0;
			ie=1;
			if(typeof document.getElementById!="undefined"){
				ie5=1;
				if(agt.indexOf("msie 6")!=-1){
					ie6=1;
					dcm=document.compatMode;
					if(dcm!="BackCompat"){com=1;}
					}
				}
			else{ie4=1;}
			}
		else if(typeof document.getElementById!="undefined"){
			exclude=0;
			if(agt.indexOf("netscape/6")!=-1||agt.indexOf("netscape6")!=-1){ns6=1;}
			else if(agt.indexOf("netscape/7")!=-1||agt.indexOf("netscape7")!=-1){ns6=1;ns7=1;}
			else if(agt.indexOf("gecko")!=-1){ns6=1;mz7=1;}
			if(agt.indexOf("safari")!=-1 || (typeof document.childNodes!="undefined" && typeof document.all=="undefined" && typeof navigator.taintEnabled=="undefined")){mz7=0;ns6=1;saf=1;}
			}
		else if((agt.indexOf('mozilla')!=-1)&&(parseInt(navigator.appVersion)>=4)){
			exclude=0;
			ns4=1;
			if(typeof navigator.mimeTypes['*']=="undefined"){
				exclude=1;
				ns4=0;
				}
			}
		if(agt.indexOf('escape')!=-1){exclude=1;ns4=0;}
		if(typeof navigator.__ice_version!="undefined"){exclude=1;ie4=0;}	
		
		corex.win = win;
		corex.mac = mac;
		corex.lnx = lnx;
		corex.ice = ice;
		corex.ie  = ie;
		corex.ie4 = ie4;
		corex.ie5 = ie5;
		corex.ie6 = ie6;
		corex.op5 = op5;
		corex.op6 = op6;
		corex.op7 = op7;
		corex.ns4 = ns4;
		corex.ns6 = ns6;
		corex.ns7 = ns7;
		corex.mz7 = mz7;
		corex.kde = kde;
		corex.saf = saf;
		
	},

	/**
	  * get element (Elem - id of html element) absolute height (pixels)
	  */
	getElementHeight : function (Elem) {
		if (corex.ns4) {
			var elem = getObjNN4(document, Elem);
			return elem.clip.height;
		} else {
			if(document.getElementById) {
				var elem = document.getElementById(Elem);
			} else if (document.all){
				var elem = document.all[Elem];
			}
			if (corex.op5) { 
				xPos = elem.style.pixelHeight;
			} else {
				xPos = elem.offsetHeight;
			}
			return xPos;
		} 
	},
	
	/**
	  * get element (Elem - id of html element) absolute width (pixels)
	  */
	getElementWidth : function (Elem) {
		if (corex.ns4) {
			var elem = getObjNN4(document, Elem);
			return elem.clip.width;
		} else {
			if(document.getElementById) {
				var elem = document.getElementById(Elem);
			} else if (document.all){
				var elem = document.all[Elem];
			}
			if (corex.op5) {
				xPos = elem.style.pixelWidth;
			} else {
				xPos = elem.offsetWidth;
			}
			return xPos;
		}
	},
	
	/**
	  * returns absolute left/top position of element in browser
	  */
	getElementPos : function (Elem) {
		
		var curleft = 0;
		var curtop  = 0;
		var obj     = document.getElementById(Elem);
		
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
		
	},
	
	getMouseXY : function (e) {
		
		var mouseX = 0;
		var mouseY = 0;
		
		if(corex.ie){
			
			var x = 0
			if(typeof(window.pageYOffset)=='number') {
				x = window.pageXOffset;
			} else if(document.body && (document.body.scrollLeft || document.body.scrollTop )) {
				x = document.body.scrollLeft;
			} else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop )) {
				x = document.documentElement.scrollLeft;
			}
			
			var y = 0;
			if (typeof(window.pageYOffset)=='number') {                                       
				y = window.pageYOffset;
			} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				y = document.body.scrollTop;
			} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))  {
				y = document.documentElement.scrollTop;
			}		
			
			corex.mouseX = x + event.clientX;
			corex.mouseY = y + event.clientY;
		
		} else {
			
			corex.mouseX = window.pageXOffset + e.clientX;
			corex.mouseY = window.pageYOffset + e.clientY;
			
		}
		
	}
	
	
	
}

/**
  * this function is called from opened filemanager or linkmanager... theyinserts 
  * through this function filename into input box with selected ID.. do not change anything on this function !
  */
function SetUrl(value,width,height,filename,targetInputID) { document.getElementById(targetInputID).value = value;  }

// set corex browser-type variables
corex.browserTypeCheck();
document.onmousemove=corex.getMouseXY;
