is_ie=(document.recalc)?1:0
if( document.getElementById )
  $ = function( id ){ return document.getElementById( id ); };
else if( document.all )
  $ = function( id ) { return document.all[id]; };
else
  $ = function(){ return undefined; };  
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = ((document.all && !document.getElementsByTagName) || (strTagName == "*" && document.all))? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements[arrReturnElements.length]=oElement;
        }   
    }
    return (arrReturnElements)
}
function ucwords(str){ 
	str=str.toLowerCase()
	return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
}
function createDomElement(type,atts) {

	var e=document.createElement(type)
	for (var  i in atts) {
		if (is_ie) {
		if (i=='style') { var p=atts[i].split(';');
			for (i=0;p[i];i++) {
				kv=p[i].split(':');
				e.style.setAttribute(kv[0],kv[1])
				}
			continue;
			}
		if (i=='class')  i='class';
		if (i=='for') i= 'htmlFor';
		}
		e.setAttribute(i,atts[i]);
		}
	return e
	}
	
function formToString(f) {
	var str='';
	for(var i=0;e=f.elements[i];i++) { 
		if (!e.name) continue;
		if (e.type=='checkbox') { str+=(e.checked)?"&"+e.name+'='+e.value:'';}
		else if (e.type=='select'){ str+="&"+e.name+'='+e.options[e.selectedIndex].value;}
		else {str+="&"+e.name+'='+escape(e.value);}
		}
	return str;
}



 function contactServer(sURL,str,callback) { 
	xc.connect(sURL, 'GET', str,callback);
	}

function format(text) {
	for (var i=1;i<arguments.length;i++) {
		text=text.replace('%'+i,arguments[i])
	}
	return text
}

String.prototype.trim=function() {return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"); }

function findTop(e) {
	var top=0;
	while (e.offsetParent && e.style.position!='relative') {
		top+=e.offsetTop;
		e=e.offsetParent;
		}
	
	return top;
	}
function findLeft(e) {
	var left=0;
	while (e.offsetParent && e.style.position!='relative') {
		
		left+=e.offsetLeft;
		e=e.offsetParent;
		}

	return left;
	}
	
var msgTimer;
var curElm;

function hideMessage(elm) {
	if (window['hideCalendarControl'])hideCalendarControl()	
	curElm.focus();
	if (curElm.select) curElm.select();
	curElm='';
	$('validationMessage').style.display='none'
}	
function showMessage(elm,msg) {

	if (curElm) return;
	if (msgTimer) {clearTimeout(msgTimer)}
	if (!$('validationMessage')) {
		var validationMessage= createDomElement("div",{id:'validationMessage',style:'z-index:10000;position:absolute;font-size:12px;color:#FF0000;display:none;background:#FFF;padding:1px 4px;border: solid 1px #AAAAAA;'}); 			
		document.body.appendChild(validationMessage)
		}

	var vm=$('validationMessage')
	vm.innerHTML=msg
	var x=findLeft(elm)+'px';
	var y=findTop(elm)+'px'
	vm.style.left=x
	vm.style.top=y;
	vm.style.display='';
	curElm=elm
	msgTimer=setTimeout('hideMessage()','1000')	;

}
function toggleDisplay(e) { 
	$(e).style.display=($(e).style.display=='none')?'':'none';
	}
function findValue(f,elm){
	if (elm.length>1 && !elm.type ) {
		var n=elm[0].name
		for(var i=0;f.elements[i];i++) { 
			
			if (f.elements[i].name==n && f.elements[i].checked) {
				return f.elements[i].value	
			}
		}
	return false;
	}
	switch (elm.type){
		case 'text':
		case 'hidden':
		case 'password':
		return elm.value;
		case 'checkbox': 
		return elm.checked
		case 'select-one':
		return elm.options[elm.selectedIndex].value
	}
}
function findFormIndex(f,e){
	for (i=0;f.elements[i];i++) { if (f.elements[i]==e) { return i}}
}

function validateField(elm,elName,f) {
	if (!window.fieldValidation[elName]) return
	var field=fieldValidation[elName];
	elm=(elm.name)?elm:elm[0]; 
			elm.value=elm.value.trim()
			var val=findValue(f,elm)
			if (field[0].match(/t/)) {
				if (!val) {
					showMessage(elm,field[1]);return false;
				}
			}
			if (field[0].match(/s/)) { 
				if (val>-1 && val!=false){return true} 
				}
			if (field[0].match(/r/)) {
		  		if (!val) { 
		 		
					showMessage(elm,"The "+field[1]+" field is required");
						;	;
		      		  	return false
		       		}
				}
			if (field[0].match(/w/)) {
				elm.value=ucwords(elm.value)
			}	
			if (field[0].match(/(\[[^\]]+\])/)) {
				m=RegExp.$1;
				re=new RegExp(m,'i');
				chars=elm.value.split('');
				for (c in chars) { 
					if (!re.test(chars[c])) {
							showMessage(elm,"There are illegal characters in the "+field[1]+" field");
							return false;	
							}
					}
				}
			if (field[0].match(/n/)) {   
		  		 if (elm.value.match(/[^\d]/g)) {
		      	 		;	
					    showMessage(elm,"only numbers are allowed in the "+field[1]+" field")
		       			return false
		       			}
				}
	if (field[0].match(/N/)) {   
		  		 if (elm.value.match(/[^\d\/\.-]/g)) {
		      	 		;	
					    showMessage(elm,"only numbers and forward slashes(/)  are allowed in the "+field[1]+" field")
		       			return false
		       			}
				}
       		if (field[0].match(/l/)) {
				if (elm.value.match(/\d/)) {    	
					;
					showMessage(elm,"numbers are not allowed in the "+field[1]+" field")
		        			return false
		       			}
				}
			if (field[0].match(/(\d+)-(\d+)/ )) {
				if (elm.value.length<RegExp.$1||elm.value.length>RegExp.$2) {
					;		
					showMessage(elm, "the "+field[1]+" field requires between "+RegExp.$1+" and "+RegExp.$2+" characters.")
		       		 	return false
		       		 	}
	      		  	}
			if (field[0].match(/=([^=]+)=/)) {
				var f2=f.elements[findFormIndex(f,elm)+parseInt(RegExp.$1)];
				if (val!=f2.value) {
					;		
					showMessage(elm, "field "+field[1]+" must equal field "+fieldValidation[f2.name][1]);
		       		 	return false
				}
			}
			if (field[0].match(/e/)) {
				if (!elm.value.match(/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][-0-9a-zA-Z\.]+)\.([a-zA-Z]{2,3}$)/)) {
					;		
					showMessage(elm,"Invalid email address");
					return false
					}
				}
			if (field[0].match(/cc/)) {				
				j=0;total=0;
				var CC_Type='';
				if(elm.form.CC_Type) {
					CC_Type=findValue(f,f.CC_Type)
				}
				
				var ccv=elm.value.replace(/[\-a-z\/ ]/gi,'');
				
				var first2=parseInt(ccv.substr(0,2));
				if (!(first2==34 || first2==37 || (first2>50 && first2<56) || ccv.substr(0,1)==4)){
					showMessage(elm,"Only Visa, Mastercard, and American Express excepted.");
					return false;
				}
					if (((first2==34 || first2==37) && (ccv.length!=15 ||(CC_Type && CC_Type!='amex'))) || (ccv.substr(0,1)==4 && ((ccv.length!=13 && ccv.length!=16) ||(CC_Type && CC_Type!='visa'))) || ((first2>50 && first2<56) && (ccv.length!=16 ||(CC_Type && CC_Type!='mc')))) {
						showMessage(elm,"Malformatted Credit Card Number");
						return false;
					}
				for(i=ccv.length-1;i>-1;i--) {
					num=parseInt(ccv.substr(i,1));
					if (j) { num*=2;if (num>9){total+=1;num-=10}}
					total+=num;
					j=(j)?0:1;
					}
					
				if (total%10) {
					showMessage(elm,"Invalid Credit Card Number");
					return false
					}	
				else { elm.value=ccv}	
				}
				
return true;				
				
				
}
function validate(f) { 
	var v;	
	if (!window.fieldValidation) {return true;}
	  for (e in fieldValidation) {		
			if (!f.elements[e]) {continue}
			if (!validateField(f.elements[e],e,f)) {return false;}					
   		}

return true
}
