function getObject(id){
	return document.getElementById(id);
}


//FIELD HIGHLIGHT
function highlightOn(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#A0BCC9";
	f.style.color="#FFFFFF";
}
function highlightOff(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#FFFFFF";
	f.style.color="#035FBA";
}

//DISPLAY TOGGLE
function hideElement(id){
	elm.style.visibility='hidden';
	elm.style.display='none';
}
function showElement(id){
	elm.style.visibility='visible';
	elm.style.display='block';
}
function toggle(id){
	elm=getObject(id);
	
	if(elm.style.visibility=='visible'){
		hideElement(id);
	}
	else{
		showElement(id);
	}
}


//FORM VALIDATION
function checkPassword(){
	pswd1=document.getElementById('f2').value;
	pswd2=document.getElementById('f2b').value;
			
	if(pswd1.length>0&&pswd1==pswd2){
		highlightOff('f2');
		highlightOff('f2b');
		return true;
	}
	else{
		highlightOn('f2');
		highlightOn('f2b');
		return false;
	}
	
}
function checkForm(){
	err=0;
	username=document.getElementById('f1');
	fname=document.getElementById('f3');
	lname=document.getElementById('f4');
	email=document.getElementById('f5');
	
	if(username.value.length<6){err++;highlightOn('f1');}else{highlightOff('f1');}
	if(fname.value.length==0){err++;highlightOn('f3');}else{highlightOff('f3');}
	if(lname.value.length==0){err++;highlightOn('f4');}else{highlightOff('f4');}
	if(email.value.length==0){err++;highlightOn('f5');}else{highlightOff('f5');}
	
	//check for password confirmation
	if(!checkPassword())err++;
	
	// test email against a regular expression
	var emailrule = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if(!emailrule.test(email.value)){
		err++;
		highlightOn('f5');	
	}
	else{
		highlightOff('f5');	
	}
	
	if(err==0){
		document.getElementById("registrationform").submit();
	}
	else{
		alert("Please complete the hightlighted fields with valid entries");
	}
}

//TEXTAREA RESIZING
// Place [onload="cleanForm();"] on body tag
function countLines(strtocount, cols) {
    var hard_lines = 1;
    var last = 0;
    while ( true ) {
        last = strtocount.indexOf("\n", last+1);
        hard_lines ++;
        if ( last == -1 ) break;
    }
    var soft_lines = Math.round(strtocount.length / (cols-1));
    var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
    if ( hard ) soft_lines = hard_lines;
    return soft_lines;
}
function cleanForm() {
	for(i=0;i<document.forms.length;i++){
	    var the_form = document.forms[i];
	    for ( var x in the_form ) {
	        if ( ! the_form[x] ) continue;
	        if( typeof the_form[x].rows != "number" ) continue;
	        the_form[x].rows = countLines(the_form[x].value,the_form[x].cols) +1;
	    }
	}
    setTimeout("cleanForm();", 300);
}

//AJAX
// <script src="http://yui.yahooapis.com/2.2.2/build/yahoo/yahoo-min.js" type="text/javascript"></script>
// <script src="http://yui.yahooapis.com/2.2.2/build/connection/connection-min.js" type="text/javascript"></script> 
function tempname(variables){ 
	elementid='';
	contentarea=document.getElementById(elementid);
	
	contentarea.innerHTML='';
	
	var sUrl="ajax_page.php";
	sUrl=sUrl+"?id="+variable;
	sUrl=sUrl+"&sid="+Math.random();
	
	var callback = {
		success: function(o) {
			contentarea.innerHTML=o.responseText;
		},
		failure: function(o) {
			contentarea.innerHTML='error';
		}
	} 

	var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);

}