$(document).ready(function() {
	$('.rounded').corners("10px");
	$('.roundedbottom').corners("10px bottom");
	$('.roundedtop').corners("10px top");
	$('#messagebar').hide();
	setTimeout('$(".messagerow").slideUp()',5000);
	$('#frmComment').hide();

	//LEFTCOL ACTIONS
			$('.lnkov').hide();
			
			$('.lnkup').mouseenter(function(event){
				upid=event.target.id;
				uparr=upid.split('-');
				ovid='#'+uparr[0]+'-'+uparr[1]+'-ov';
				$(this).slideUp(400);
				$(ovid).slideDown(400);
			});
			
			$('.lnkov').mouseleave(function(event){
				ovid=event.target.id;
				ovarr=ovid.split('-');
				upid='#'+ovarr[0]+'-'+ovarr[1]+'-up';
				$(this).slideUp(400);
				$(upid).slideDown(400);
			});
	
	//FLEET ACTIONS
			$('#royal2').hide();
			$('#royal3').hide();
			$('#lnkroyal2').click(function(){
				$('#royal2').slideToggle();
				$('html, body').animate({ 
					scrollTop: $('#lnkroyal2').offset().top 
				}, 500);
				return false;
			});
			$('#lnkroyal3').click(function(){
				$('#royal3').slideToggle();
				$('html, body').animate({ 
					scrollTop: $('#lnkroyal3').offset().top 
				}, 500);
				return false;
			});
	
	//CONTACT FORM ACTIONS
			//hide all contact form day fields and loader animation
			$('div.subrow').slideUp(0);
			$('#loader').hide();
			
			//show first day
			$('#day1').slideDown(0);
			
			//select number of days
			$('#f5').change(function(){
				for(i=1;i<11;i++){
					dayid='#day'+i;
					if(i<=document.getElementById('f5').value){
						$(dayid).slideDown();
					}
					else{
						$(dayid).slideUp();
					}
				}
			});
			
			//submit button
			$('#bttSubmit').click(function(){
				
				//validate
				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);}
				}
				
				if(err==0){
					//disable submit button
					document.getElementById('bttSubmit').disabled=true;
					document.getElementById('bttSubmit').value='Sending...';
					$('#firstrow').slideUp();
					$('#secondrow').slideUp();
					$('#thirdrow').slideUp();
					$('#loader').show();
				
					//concatenate data string
					data='';
					for(i=1;i<10;i++){
						fid='f'+i;
						data=data+fid+'='+document.getElementById(fid).value+'&';
					}
					for(i=1;i<11;i++){
						dateid='d'+i+'-a';
						halfdayamid='d'+i+'-ba';
						halfdaypmid='d'+i+'-bp';
						fulldayid='d'+i+'-bf';
						boatid='d'+i+'-c';
						data=data+dateid+'='+document.getElementById(dateid).value+'&';
						data=data+halfdayamid+'='+document.getElementById(halfdayamid).checked+'&';
						data=data+halfdaypmid+'='+document.getElementById(halfdaypmid).checked+'&';
						data=data+fulldayid+'='+document.getElementById(fulldayid).checked+'&';
						data=data+boatid+'='+document.getElementById(boatid).value+'&';
					}
					data=data+'f6='+document.getElementById('f6').value;
					
					// alert(data);
					// data.replace("&","&amp;");
					// data.replace("[AMP]","&");
					// alert(data);
					
					elm=document.getElementById('frmContact');
					$.ajax({
						type: "POST",
						url: "contact_dosend.php",
						cache: false,
						data: data,
						success: function(html){
							elm.innerHTML=html;
						}
					});
				}
				else{
					alert("Please complete the hightlighted fields with valid entries");
				}
			});
			
	//TESTIMONIALS FORM ACTIONS
			//submit button
			$('#bttSubmitComment').click(function(){
				
				//validate
				err=0;
			
				fieldstocheck=new Array();
				fieldstocheck=['f1','f2','f3'];
				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);}
				}
				
				if(err==0){
					//disable submit button
					document.getElementById('bttSubmitComment').disabled=true;
					document.getElementById('bttCancelComment').disabled=true;
					document.getElementById('bttSubmitComment').value='Sending...';
					$('#loader').show();
				
					//concatenate data string
					data='f1='+document.getElementById('f1').value+'&';
					data=data+'f2='+document.getElementById('f2').value+'&';
					data=data+'f3='+document.getElementById('f3').value+'&';
					
					challengeField = $("input#recaptcha_challenge_field").val();
					responseField = $("input#recaptcha_response_field").val();
					data=data+"recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField;
					
					// alert(data);
					// return false;
					
					elm=document.getElementById('frmComment');
					$.ajax({
						type: "POST",
						url: "comments_dosend.php",
						cache: false,
						data: data,
						success: function(html){
							elm.innerHTML='<p id="tempmessage">Thank you.<br /><br />Your testimonial has been sent and will be displayed on the website soon.</p>';
							$('#frmComment').animate({opacity: 1.0}, 10000);
							$('#frmComment').slideUp(1000);
						}
					});
				}
				else{
					alert("Please complete the hightlighted fields with valid entries");
				}
			});
});

function getDatePicker(elmid){
	elmid='#'+elmid;
	$(elmid).DatePicker({
		format:'d/m/Y',
		date: $(elmid).val(),
		current: $(elmid).val(),
		starts: 1,
		position: 'bottom',
		onBeforeShow: function(){
			$(elmid).DatePickerSetDate($(elmid).val(), true);
		},
		onChange: function(formated, dates){
			$(elmid).val(formated);
			$(elmid).DatePickerHide();
		}
	});
	$(elmid).DatePickerShow();
}

function showCommentForm(){
	$('#frmComment').slideDown();
	$('html, body').animate({ 
		scrollTop: $('h2').offset().top 
	}, 500);
}

function hideCommentForm(){
	$('#frmComment').slideUp();
	$('html, body').animate({ 
		scrollTop: $('body').offset().top 
	}, 500);
}

//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="#1C6483";
	f.style.color="#FFFFFF";
}
function highlightOff(elm){
	f=document.getElementById(elm);
	f.style.backgroundColor="#0B0E15";
	f.style.color="#767676";
}
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);
}