$(document).ready(function() {
	$('.rounded').corners("10px");
	$('.roundedbottom').corners("10px bottom");
	$('.roundedtop').corners("10px top");
	$('.rounded-bottomleft').corners("10px bottom-left");
	$('.rounded-bottomright').corners("10px bottom-right");
	$('#messagebar').hide();
	$('.resultssection').hide();
	setTimeout('$(".messagerow").slideUp()',5000);

	$('a.newsitem').fadeTo(0,0.6);
	$('a.newsitem').hover(
		function () {
			$(this).fadeTo(200,1)
		}, 
		function () {
			$(this).fadeTo(300,0.6)
		}
	);
	
	$('#thumbs a').click(
		function(){
			imagepath=$(this).attr("href");
			imgtext=$(this).attr("title");
			imgtextarr=imgtext.split(' | ');
			title=imgtextarr[0];
			caption=imgtextarr[1];
			imgcode='<img src="'+imagepath+'" />';
			caption="<div id=\"caption\"><b>"+title+"</b><p>"+caption+"</p></div>";
			fullcode=caption+imgcode;
			$('#viewer').html(fullcode); 
			return false;
		}
	);
	
	$("#frmContact #bttSubmitContact").click(function(){
		err=0;
		tofill=''
		if($('#frmContact #f1').attr('value')==''){ err++;tofill=tofill+'\n- name'; }
		if($('#frmContact #f2').attr('value')==''){ err++;tofill=tofill+'\n- email'; }
		if($('#frmContact #f3').attr('value')==''){ err++;tofill=tofill+'\n- message'; }
		
		if(err>0){
			alert('Please complete the obligatory fields: '+tofill);
			return false;
		}
	
		actionstr=$('#frmContact').attr('action');
		datastr='name='+$('#frmContact #f1').attr('value');
		datastr=datastr+'&email='+$('#frmContact #f2').attr('value');
		datastr=datastr+'&message='+$('#frmContact #f3').attr('value');
		
		$('#frmContact').html('<p class="statusmessage">Sending... please wait...</p>');
		
		
		$.ajax({
			type: "POST",
			url: actionstr,
			data: datastr,
			success: function(html){
				$('#frmContact p.statusmessage').html(html);
			}
		});	
	});
	
});

function loadGalleryThumbs(fid){
	
}

//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="#333333";
}
function highlightOff(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#FFFFFF";
	f.style.color="#333333";
}
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=['f2','f3','f12','f15','f18','f22','f24'];
	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);}
	}
	
	//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);
}
