// JavaScript Document
//var previouscolor='';
//function focusElement()
//{
//previouscolor=this.style.borderColor; this.style.borderColor='#FF0000';
//}
//function blurElement() { this.style.borderColor=previouscolor; }
//Selecting the first input box available

var deactivatedSubmits=Array();

function egAddEvent(obj,event,handler)
	{
	if (obj.addEventListener) obj.addEventListener(event,handler,false);
	else if (obj.attachEvent) obj.attachEvent('on'+event,handler);
	else if (obj.setAttribute) obj.setAttribute('on'+event,handler);
	else obj['on'+event]=handler;
	}

function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') window.onload = func;
  else{
      window.onload = function()
      	{
      	if (oldonload) oldonload();
	    func();
    	}
  	  }
}	

function focusOne()
{
var j=0; var flag=true;
var buttonSubmit=true;
while((j<document.forms.length))
	{
	var i=0;
	while ((i<document.forms[j].length))
		{		
		obj=document.forms[j].elements[i];
		if (obj.getAttribute('noFocus')!='yes')
			{
			if(obj.tagName!='SELECT') obj.onfocus=showTooltip;
			else obj.onmousedown=showTooltip;
			obj.onblur=hideTooltip;
			if (obj.type!='hidden')
				if (obj.id!='ignore')
					if(flag)
						if((obj.tagName=='INPUT')|(obj.tagName=='SELECT'))
							if(obj.type!='submit')
								if(obj.type!='button')
									if (obj.disabled==false)
										if (obj.style.visibility!='hidden')
											if (obj.style.display!='none')
												if(obj.focus)
													{
													obj.focus();
													if(obj.select) obj.select();
													flag=false;
													}
			if (obj.type=='file')
				obj.onchange=onChangeCheckAllowed;
			}
		i++;
		}
	egAddEvent(document.forms[j],'submit',processSubmit);
	j++;	
	}
}
//addLoadEvent();
focusOne();

function processSubmit(event)
	{
	el=(event.srcElement?event.srcElement:this);
	if (checkFields(el)===false) return false;
	}

function deactivateSubmit(element)
	{
	if (element.parentNode!=undefined)
		{
		var rand_id=Math.random();
		var working=document.createElement('div');
		working.id=rand_id;
//		working.setAttribute('style','font-weight:bold;');
		working.innerHTML="<blink><font size='1'>submitting...</font></blink>";
		element.parentNode.insertBefore(working,element);
		element.style.display='none';
		deactivatedSubmits[rand_id]=element;
		}
	}

function reactivateSubmit()
	{
	for (var i in deactivatedSubmits)
		{
		deactivatedSubmits[i].style.display='inline';
		document.getElementById(i).parentNode.removeChild(document.getElementById(i));
		}
	deactivatedSubmits=Array();
	}

function onChangeCheckAllowed()
	{
	var notAllowed=checkAllowed(this)
	if (notAllowed)
		alert(notAllowed);
	}

function checkAllowed(element)
	{
	var allowed=element.getAttribute('allow');
	if (allowed)
		{	
		if (element.value!='')
			{
			file=element.value;
			if (file.indexOf('.')!=-1)
				{
				var extension=file.substr(file.lastIndexOf('.')+1);
				var is_array=false;
				if (allowed.indexOf(',')!=-1)
					{
					is_array=true;
					allowed_array=allowed.split(',');
					}
				if (!is_array)
					{
					if (allowed.toUpperCase()!=extension.toUpperCase())
						return "You have uploaded a "+extension.toUpperCase()+" file but you are only allowed to upload "+allowed.toUpperCase()+" files!";
					}
				else
					{
					var is_valid=false;
					for (var i in allowed_array)
						if (allowed_array[i].toUpperCase()==extension.toUpperCase())
							is_valid=true;
					if (!is_valid)
						return "You have uploaded a "+extension.toUpperCase()+" file but you are only allowed to upload "+allowed.toUpperCase()+" files!";
					}
				}
			else
				return "We could not determine the type of your file. Please choose another one!";
			}
		}
	return false;
	}
	
function checkFields(element)
{
var flag=true;
var i=0;
var submiters=Array();
while ((i<element.length))
	{	
	obj=element.elements[i];
	if (obj.getAttribute('isRequired')=='yes')
		if(obj.value=='')
			{
			requiredName=obj.getAttribute('requiredName').toLowerCase();
			requiredName=requiredName.charAt(0).toUpperCase()+requiredName.substr(1);
			alert(requiredName+' is missing !');
			try{ obj.focus(); } catch(er) { }
			return false;
			}
	if (obj.type=='file')
		{
		var notAllowed=checkAllowed(obj);
		if (notAllowed)
			{
			alert(notAllowed);
			return false;
			}
		}
	if (obj.type=='submit')
		if (obj.getAttribute('noFocus')!='yes')
			submiters[i]=obj;
	i++;
	}
if (element.getAttribute)
	{
	if (element.getAttribute('showLoading')) var showLoading=(element.getAttribute('showLoading')=='false'?false:true);
	else var showLoading=true;
	}
else
	{
	if (element.showLoading) var showLoading=(element.showLoading=='false'?false:true);
	else var showLoading=true;
	}
if (showLoading) for (var j in submiters) deactivateSubmit(submiters[j]);
}

function showTooltip()
{
this.style.backgroundColor = '#fffdd5';
this.style.color ='#000000';
if(this.name) if(typeof(tooltips)!='undefined')
	{
	var flag=true, i=0;
	while((i<tooltips.length)&&(flag))
		if(this.name==tooltips[i]) flag=false;
		else i+=2;
	if(!flag)
		{
		var coors = findPos(this);
		var x = document.getElementById('Tooltip');
		var elemWidth=parseInt(this.offsetWidth);
		var additional=parseInt(this.getAttribute('additional'));
		if (!isNaN(additional))
			elemWidth=elemWidth+additional;
		x.style.top = coors[1] + 'px';
		x.style.left = coors[0] +elemWidth+10+'px';
		x.innerHTML=tooltips[i+1];x.style.visibility='visible';
		}
	}

}

function showTips(Obj,text)
{
var x = document.getElementById('Tooltip');

if(x){
	var coors = findPos(Obj);
	var elemWidth=parseInt(Obj.offsetWidth);
	var additional=parseInt(Obj.getAttribute('additional'));
	if (!isNaN(additional))	elemWidth=elemWidth+additional;

	x.style.top = coors[1] + 'px';
	x.style.left = coors[0] +elemWidth+10+'px';
	x.innerHTML=text;
	x.style.visibility='visible';
	}
}

function hideTips() 
	{
	var x = document.getElementById('Tooltip');
	if(x) x.style.visibility='hidden';
	}

function hideTooltip() 
	{
	var x = document.getElementById('Tooltip');
	if(x) x.style.visibility='hidden';
	this.style.backgroundColor = '';	
	this.style.color='';

	validateForm.verifyField(this);
	}

function findPos(InputTag)
	{
	var curleft = curtop = 0;
		curleft = InputTag.offsetLeft
		curtop = InputTag.offsetTop
		while (InputTag = InputTag.offsetParent) {
			curleft += InputTag.offsetLeft
			curtop += InputTag.offsetTop
		}
	return [curleft,curtop];
	}


	
function doBlink()
{
var blink = document.getElementsByTagName('BLINK');
for (var i=0; i < blink.length; i++)
  blink[i].style.visibility = blink[i].style.visibility == '' ? 'hidden' : '' 
}
setInterval('doBlink()',1000);

function q(name){return confirm('Are you sure you want to delete ' + name + ' ?');}

function p(name){return confirm('Are you sure you want to ' + name + ' ?');}


function getElementWidth(Elem) {
	if (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 (op5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	}
}

function capsDetect(Obj,e) 
{
 if(!e) e = window.event;
 if(!e) return; 
 var theKey = 0;
 if( e.which ) { theKey = e.which; } 
 else if( e.keyCode ) { theKey = e.keyCode; } 
 else if( e.charCode ) { theKey = e.charCode } 
 var theShift = false;
 if( e.shiftKey ) theShift = e.shiftKey;
 else if( e.modifiers ) 
 	{ 
    if( e.modifiers & 4 ) theShift = true;
	}
 
 if( theKey > 64 && theKey < 91 && !theShift ) showTips(Obj,'Caps Lock is ON');
 else if( theKey > 96 && theKey < 123 && theShift ) showTips(Obj,'Caps Lock is ON');
 else hideTips();
}
function preventMoreClicks(e)
{
    if (!e)
      e = window.event;

    if (e.cancelBubble)
      e.cancelBubble = true;
    else
      e.stopPropagation();
}

/* PHP-JS functions */
function sround(number,decimals)
	{
	number=String(number);
	decimals = typeof(decimals) != 'undefined' ? decimals : 2;
	if(number.indexOf('.')!=-1){p=number.split('.');number=p[0]+'.'+p[1].substr(0,decimals);}
	return(number);
	}	

function stripBad(string)
	{
    for (var i=0, output='', valid="eE+/*-0123456789.()"; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1) output += string.charAt(i)
    return output;
	}

function strtolower(str) {return (str+'').toLowerCase();}

function ucfirst(str)
	{
	str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
	}


/* Validate Form Object */
var validateForm = {
	verifyField : function(Obj) 
		{
		var validateFields = {"tbSecurityCodeBox":"code","tbEmailBox":"email"};
		if(typeof Obj.className != 'undefined')
			{
			for(crt_class in validateFields)
				if(hasClass(Obj,crt_class)) validateForm.checkForm(Obj,validateFields[crt_class]);
			}
		},
				
	checkForm : function(Obj,type)
		{
		if(typeof Obj.value == 'undefined') return false;
		var current_value=Obj.value;
		
		var currentRow=document.getElementById('live_field_'+type);
		var errorObj=document.getElementById('live_field_err_'+type);
		
		if(currentRow)if(errorObj)
			{
			var cclass='tbTableRow';var test=false;
			var tmpVal="validate"+ucfirst(type);
			errorObj.style.display='none';
			
			if(typeof tmpVal != 'undefined') test = validateForm[tmpVal](current_value);
			
			if(test)if(test!='OK')
				{
				cclass='tbTableError';
				errorObj.innerHTML=test;
				errorObj.style.display='block';
				}
			currentRow.className=cclass;	
			}
		},
		
	validateEmail : function(elementValue)
		{  
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if(elementValue.length==0) return "Email missing !";  
		else if(!emailPattern.test(elementValue)) return "Invalid Email !"; 
		else return "OK";
		},
		
	validateZip : function(elementValue)
		{
		var valid = "0123456789-";
		var hyphencount = 0;
	
		if (elementValue.length!=5 && elementValue.length!=10) return "Please enter a 5 digit or 5 digit+4 zip code"
			
		for (var i=0; i < elementValue.length; i++)
			{
			temp = "" + elementValue.substring(i, i+1);
			if (temp == "-") hyphencount++;
		
			if (valid.indexOf(temp) == "-1") return "Invalid characters in zip code";
			
			if ((hyphencount > 1) || ((elementValue.length==10) && ""+elementValue.charAt(5)!="-"))
				{
				return "The hyphen character is missplaced (Correct format: 'xxxxx-yyyy')";
			   	}
			}
		return "OK";
		},
		
	validateCode : function(elementValue)
		{  
		if(elementValue.length==0) return "Code missing !";
		else if(elementValue.length<6) return "Code must have 6 characters !";
		else return "OK";
		}
	};
	
function isset()
{
var a = arguments,l = a.length,i = 0,undef;
if (l === 0) return false;
while (i !== l){if (a[i] === undef || a[i] === null) return false;i++;}
return true;
}
	
	
/*CSS functions*/
function hasClass(ele,cls) {return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));}
function addClass(ele,cls) {if (!this.hasClass(ele,cls)) ele.className += " "+cls;}

function removeClass(ele,cls)
{
if (hasClass(ele,cls))
	{
	var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');	
	ele.className=ele.className.replace(reg,' ');
	}
}
