$(document).ready(function() {
	$('.rounded').corners("10px");
	$('.roundedbottom').corners("10px bottom");
	$('.roundedtop').corners("10px top");
	$('#messagebar').hide();
	setTimeout('$(".messagerow").slideUp()',5000);
});

function sendContact(){
	if(checkForm()){
		document.getElementById('bttsend').disabled=true;
		document.getElementById('bttsend').value='Sending...';
		data='f=sendContact&f1='+document.getElementById('f1').value+'&f2='+document.getElementById('f2').value+'&f3='+document.getElementById('f3').value+'&f4='+document.getElementById('f4').value;
					
		// alert(data);
		// data.replace("&","&amp;");
		// data.replace("[AMP]","&");
		// alert(data);
		// return false;
				
		elm=document.getElementById('frmcontact');
		$.ajax({
			type: "POST",
			url: "ajax.php",
			cache: false,
			data: data,
			success: function(html){
				elm.innerHTML=html;
			}
		});
	}
}

//GENERIC FUNCTIONS
function addslashes (str) {
    return (str+'').replace(/([\\"'+])/g, "\\$1").replace(/\u0000/g, "\\0");
}
function selectOptionByValue(elmid,val){
	elm=document.getElementById(elmid);
	elmoptions=elm.options;
	elmlen=elmoptions.length;
	for(i=0;i<elmlen;i++){
		if(elm.options[i].value==val){
			elm.options[i].selected=true;
		}
	}
}
function toggleTableRow(elmid){
	elm=document.getElementById(elmid);
	if(elm.style.visibility=='hidden'){
		elm.style.display='';
		elm.style.visibility='visible';
	}
	else{
		elm.style.display='none';
		elm.style.visibility='hidden';
	}
}

//FORM VALIDATION
function highlightOn(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#FBE5EA";
	f.style.color="#686868";
}
function highlightOff(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#FFFFFF";
	f.style.color="#686868";
}
function checkPassword(f1,f2){
	pswd1=document.getElementById(f1).value;
	pswd2=document.getElementById(f2).value;
			
	if(pswd1.length>0&&pswd1==pswd2){
		highlightOff(f1);
		highlightOff(f2);
		return true;
	}
	else{
		highlightOn(f1);
		highlightOn(f2);
		return false;
	}
}
function checkForm(){
	err=0;
	
	fieldstocheck=new Array();
	fieldstocheck=['f1','f2'];
	for(var i=0;i<fieldstocheck.length;i++){
		fieldid=fieldstocheck[i];
		field=document.getElementById(fieldid);
		if(field.value.length==0){err++;highlightOn(fieldid);}else{highlightOff(fieldid);}
	}
	
	// test email against a regular expression
	email=document.getElementById('f2').value;
	var emailrule = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if(!emailrule.test(email)){
		err++;
		highlightOn('f2');	
	}
	else{
		highlightOff('f2');	
	}
	
	if(err==0){
		return true;
	}
	else{
		alert("Please complete the hightlighted fields with valid entries");
		return false;
	}
}

//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);
}