// JavaScript Document
// Autor: Alex Platt
// Project WebPlat4m


	function addListener(element, event, listener, bubble) {
	  if(element.addEventListener) {
		if(typeof(bubble) == "undefined") bubble = false;
		element.addEventListener(event, listener, bubble);
	  } else if(this.attachEvent) {
		element.attachEvent("on" + event, listener);
	  }
	}
	
	function cancelEvent( e ) {
		    if (e && e.preventDefault) e.preventDefault();
			if (window.event != null) window.event.returnValue = false;
			return false;
	}
	
	function tooltipOn( nam, caption) {
		

		   var coord = findPos(document.getElementById(nam));		   
		   toolTip = document.getElementById('platt4m_validator_tooltip');
		   toolTip.style.left = coord[0] + "px";
		   toolTip.style.top = (coord[1] + getElementHeight(nam)) + "px";
		   
		   
	
		   
		   addListener(document.getElementById(nam), "change", function(e) {
				toolTip = document.getElementById('platt4m_validator_tooltip');
				document.getElementById('platt4m_validator_tooltip_text').innerHTML = "&nbsp;";
				toolTip.style.display = "none";
		   });
		   
		   addListener(document.getElementById(nam), "keydown", function(e) {
				toolTip = document.getElementById('platt4m_validator_tooltip');
				document.getElementById('platt4m_validator_tooltip_text').innerHTML = "&nbsp;";
				toolTip.style.display = "none";
		   });
		   
		   addListener(document.getElementById(nam), "click", function(e) {
				toolTip = document.getElementById('platt4m_validator_tooltip');
				document.getElementById('platt4m_validator_tooltip_text').innerHTML = "&nbsp;";
				toolTip.style.display = "none";
		   });
		   
		   var fix = "absolute";
		   var pNode = document.getElementById(nam).parentNode;
		   do {
			    if ((pNode != null) && (pNode.parentNode != null) && (pNode.style != null) ) {
					if (pNode.style.position == "fixed") {
						fix = "fixed";
					}
				}
		   }
		   while (pNode = pNode.parentNode ) ;
		   
		   var dontShow = false;
		   var browser = navigator.appVersion;
		   if (browser.indexOf('MSIE ') > -1) {
		   		var num = browser.substr(browser.indexOf('MSIE ') + 5,browser.length);
				num = num.substr(0,num.indexOf(';'));
				if (eval(num) >= 8) {
					 if (fix == "fixed") {
					 	dontShow = true;
					 }
				}
		   }
		   
		   if (!dontShow) {
		   		document.getElementById('platt4m_validator_tooltip_text').innerHTML = "<img src='http://www.plattpou.com/platt4m/imgs/spacer.gif' border='0' width='100%' height='1'/><font style='font-family:Arial, Helvetica, sans-serif; font-size:12px;'>"  + caption + "</font>";
		   		toolTip.style.display = "inline";
	 		    toolTip = document.getElementById('platt4m_validator_tooltip');
			    toolTip.style.position = fix;
		   
		   }
		   else {
		   		alert(caption);
		   }
		   
		   
		   //alert(document.getElementById(nam).parentNode.style.position);
		   document.getElementById(nam).focus();
	}
	
	function validateForm( e, senderForm ) {
	        addListener(senderForm,"reset", function(e) {
				 toolTip = document.getElementById('platt4m_validator_tooltip');
				 toolTip.style.display = "none";
			});
	
			function htmlize(str){
					str = str.replace(/\&/g,"&amp;");
					str = str.replace(/\</g,"&lt;");
					str = str.replace(/\>/g,"&gt;");
					str = str.replace(/\"/g,"&quot;");
					str = str.replace(/\n/g,"<br/>\n");
					return str;
			}
	
			function validate( fieldsArr ) {
					
				for (x=0; x< fieldsArr.length; x++) {
					var nam = fieldsArr[x].name;
					var type = fieldsArr[x].type;
					if ((nam != "") && (nam != null)) {
						var tag = fieldsArr[x].alt;
						var val = fieldsArr[x].value;
						
						if ((val != null) && (val != "")) {
											
							 //for comments value  --put something here--
							 if (val.length > 2) {
									if ((val.substring(0,2) == "--") && (val.substring(val.length - 2,val.length) == "--")) {
										val = "";				
									}
									
							 }
						}
						
						if (nam.substring(0,1) == "_") {  // _ for not empty
						   if ((val == null) || (val=="")) {
							   if (type != "hidden") {
							   		tooltipOn( nam, "Cannot be empty");	
							   		return (true);   
							   }
						   }
						}
						
						if (nam.substring(0,1) == "~") {  // ~ must be a number
						   if ((val == null) || (val=="") || (isNumeric(val)==false) ) {		
						       if (type != "hidden") {
							   		tooltipOn( nam, "Must be a number");
							   		return (true);
							   }
						   }
						}
						
					} //nam not empty
				}
				return false;
			}
	
			var inputs = senderForm.getElementsByTagName("input"); 
			var textareas = senderForm.getElementsByTagName("textarea"); 
			var selects = senderForm.getElementsByTagName("select"); 
			
			
			
			var abort = validate(inputs) || validate(textareas) || validate(selects);
			
			validator_submit_aborted = abort;
			
			if (abort == true) {
				if (e && e.preventDefault) e.preventDefault();
				if (window.event != null) window.event.returnValue = false;
				return false;
			}
	}
	
	var validator_submit_aborted = false;
	
	function validate( frmID ) {
			addListener(document.forms[frmID],"submit", function(e) { 
				 e= window.event || e;
				 validateForm( e, document.getElementById(frmID) ); 
			 } );
	}		 

    function isNumeric(sText)
	{
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	
	 
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 }
		  }
	   return IsNumber;
	   
	}


   
   function findPos(obj) {
	var curleft = curtop = 0;
    if (obj.offsetParent) {
         do {
			curleft += obj.offsetLeft;
			
			curtop += obj.offsetTop;
			
         } while (obj = obj.offsetParent);
	}
    return [curleft,curtop];
   }
   
   function getElementHeight(Elem) {
	
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		
		xPos = elem.offsetHeight;
		
		return xPos;
	 
   }
   
   function hola(){ alert('hola'); }
   
   
   function setComment( obj, txt ) {
	
		if ((obj.value == "") || (obj.value == txt)) {
			obj.value = txt;
			obj.style.color = "#CCCCCC";
		}
		
		obj.onclick = function () {
			if (this.value == txt) obj.value = "";
			obj.style.color = "#000000";
		}
		
		obj.onblur = function () {
			if (this.value == "") {
				obj.value = txt;
				obj.style.color = "#CCCCCC";
			}
		}
		
		obj.onfocus = function() {
			if (this.value == txt) obj.value = "";
			obj.style.color = "#000000";
		}
	
   }
	
   
   function setIgnoreComments( objName , commentText ) {	 
		document.getElementById(objName).onclick = function() {
			 if (this.value == commentText) this.value = "";
		}
		document.getElementById(objName).onfocus = function() {
			 if (this.value == commentText) this.value = "";
		}
		document.getElementById(objName).onblur = function() {
			 if (this.value == "") this.value = commentText;
		}	        
   }

   function setMutualExclusiveChks( chk1 , chk2 ) {
		var ochk1 = document.getElementById(chk1);
		var ochk2 = document.getElementById(chk2);
	
		addListener(ochk2, "click", function(e) {
			if (ochk2.checked) {
			   ochk1.checked = false;
			}
		});
		
		addListener(ochk1, "click", function(e) {
			if (ochk1.checked) {
			   ochk2.checked = false;
			}
		});
	}
	
	function preload( img ) {
		document.write("<img src='" + img + "' style='display:none;'/>"); 
	}



    document.write("<div id='platt4m_validator_tooltip' style='position:absolute; display:none; color:#000000;z-index:3000;'><table border='0' style='font-size:12px;' background='http://www.plattpou.com/platt4m/imgs/tooltip.png' width='200' height='40'><tr valign='middle'><td valign='middle' align='center' id='platt4m_validator_tooltip_text' style='padding-top:5px;'>&nbsp;</td></tr></table></div>");