function isRequired(str,requiredfields)
{
	var x;
	
	if (!requiredfields)
		{
		x = str.charAt(0);
		if (x >= 'A' && x < 'a') 
			{
			return true;
			}
		else
			{
			return false;
			}
		}
	else
		{
		// Is the field contained in the list ...
		if (requiredfields.value.indexOf(str) != -1)
			{
			return true;
			}
		else
			{
			return false;
			}		
		}
}

function check_form(frm)
{
	var i;
	var ok = true;
	var return_value;
	

	//dump(frm);

	for (i=0; i < frm.length; i++)
		{
		x = frm[i];

		if (ok)
			{
			if (x.type != 'hidden' && isRequired(x.name, frm.requiredfields))
				{
				
				switch (x.type)
					{
					case 'text':
					case 'textarea':
					case 'password':
						{
						if (x.name == 'Email')
							{
							ok = isEmail(x.value);
							
							if (ok == false)
								{
								display_alert(x);
								}
							}
						else
							{
							ok = ForceEntry(x, format_name(x.name));
							}														
						}
						break;
											
					
					case 'select-one':
						{
						if (x.selectedIndex == 0)
							{
							ok = false;
							display_alert(x);
							}
						else
							{
							ok = true;
							}
						}
						break;
					
					case 'radio':
						{
						var radio;
						var btn;
						var debug;
						
						ok = false;
						radio = eval('frm.'+x.name);

						for (btn=0;btn<radio.length;btn++)
							{
							if (radio[btn].checked == true)
								{
								ok = true;
								break;
								}
							}

						if (ok == false)
							{
							display_alert(x);
							}
						}
						break;											
					

					case 'checkbox':
					case 'select-multiple':
					default:
						break;
					} //End Switch
				} // End If hidden
			}
		else
			{
			break;
			}
		} // End loop
			
		
	if (ok)
		{
		for(i=0; i < frm.length; i++)
			{
			if (frm[i].type == 'text' || frm[i].type == 'textarea')
				{
				makeSafe(frm[i]);
				}
			}
		}
			
	return ok;
}

function display_alert(z)
{
	if (z.name == 'Email')
		{
		alert("Please enter email address in the format of me@anywhere.com");
		}
	else
		{
		alert("Please select " + format_name(z.name));
		}
		
	z.focus();
	z.select;
}

function format_name(name)
{
	var new_array;
   	var i;
   	var new_str;
   
   	new_array = name.split("_");
	new_str = "";
   
	  
   	for(i=0; i<new_array.length; i++)
   		{
		new_str += new_array[i].charAt(0).toUpperCase() + new_array[i].substr(1);
		new_str = new_str + " ";
		}
	
   	return new_str;
}

function jumpMenu(targ,selObj,restore)
	{ 
	var location = selObj.options[selObj.selectedIndex].value;
	
  	eval(targ+".location='"+location+"'");
  	if (restore) selObj.selectedIndex=0;
	}
	

	
function preselect_form(frm)
{
	var query = window.location.search.substring(1).split("&");
	var key, val, pos;
	var re = /\+/g;
	
	for(i=0; i<query.length-1; i++)
		{
		pos = query[i].indexOf('=');
		key = query[i].substring(0, pos);

		// Check if the key value pairs are empty .. is the last character a "="
		if (pos != query[i].length-1)
			{
			val = query[i].substring(pos+1).replace(re,' ');
			val = val.replace(/%2B/, '+');
			}
		else
			{
			val ='';	
			}

		for (j=0; j < frm.length; j++)
			{
			if ((frm[j].type=='select-one') && (frm[j].name == key))
				{
				for (k=0; k < frm[j].options.length; k++)
					{
					if (frm[j].options[k].value	== val)
						{
						frm[j].options[k].selected = true;
						break;
						}
						
					}
				}
			}
		}
		
}

// Added 22-08-06 for Yacht Search Changes
function change_yacht_type(frm)
{
	var i = frm.yacht_type.selectedIndex;
	var loc = frm.location[frm.location.selectedIndex].value;

	switch (i)
		{
		// Yacht Type - Sailing Yachts
		case 2:
			{
			search_form_default_locations(frm);
	
			if (frm.location.disabled)
				{
				frm.location.disabled = false;
				}
	
			if (frm.daily_rate.disabled)
				{
				frm.daily_rate.disabled = false;
				}

			frm.daily_rate.options[0].text = "Daily Rate (All)";
			frm.daily_rate.options[0].value = "";
			frm.daily_rate.options[1].text = "Up to £1000";
			frm.daily_rate.options[1].value = "0-1000";
			frm.daily_rate.options[2].text = "£1000 - £2000";
			frm.daily_rate.options[2].value = "1000-2000";
			frm.daily_rate.options[3].text = "£2000+";
			frm.daily_rate.options[3].value = "2000+";
			}
			break;
			
		
		// Yacht Type - Cocktail Cruises
		case 3:
		case 4:
			{
			search_form_default_locations(frm);
			frm.location.selectedIndex = 0;

			//if (!frm.location.disabled)
			//	{
			//	frm.location.disabled = true;
			//	}
	
	
			frm.daily_rate.selectedIndex = 0;
			if (!frm.daily_rate.disabled)
				{
				frm.daily_rate.disabled = true;
				}
			}
			break;
		
		
		// Yacht Type - Bareboat
		case 5:
			{
			search_form_default_locations(frm);

			if (frm.location.disabled)
				{
				frm.location.disabled = false;
				}
	
			if (frm.daily_rate.disabled)
				{
				frm.daily_rate.disabled = false;
				}

			frm.daily_rate.options[0].text = "Daily Rate (All)";
			frm.daily_rate.options[0].value = "";
			frm.daily_rate.options[1].text = "Up to £200";
			frm.daily_rate.options[1].value = "0-200";
			frm.daily_rate.options[2].text = "£200 - £400";
			frm.daily_rate.options[2].value = "200-400";
			frm.daily_rate.options[3].text = "£400+";
			frm.daily_rate.options[3].value = "400+";
			}
			break;

	
		default:
			{
			search_form_default_locations(frm);

			if (frm.location.disabled)
				{
				frm.location.disabled = false;
				}
	
			if (frm.daily_rate.disabled)
				{
				frm.daily_rate.disabled = false;
				}
				
			frm.daily_rate.options[0].text = "Daily Rate (All)";
			frm.daily_rate.options[0].value = "";
			frm.daily_rate.options[1].text = "Up to £2000";
			frm.daily_rate.options[1].value = "0-2000";
			frm.daily_rate.options[2].text = "£2000 - £5000";
			frm.daily_rate.options[2].value = "1900-5000";
			frm.daily_rate.options[3].text = "£5000+";
			frm.daily_rate.options[3].value = "4000+";
			}
			break;
			
		}
}

// Added 22-08-06 for Yacht Search Changes
function search_form_default_locations(frm)
{
	var loc = frm.location.options[frm.location.selectedIndex].value;

	frm.location.options[0] = new Option("South of France", "South of France");
	frm.location.options[1] = new Option("Caribbean", "Caribbean");
	//frm.location.options[2] = new Option("Croatia", "Croatia"); 
	frm.location.options[2] = new Option("Italy", "Italy"); 
	frm.location.options[3] = new Option("All Regions", ""); 	
	
	for (k=0; k < frm.location.options.length; k++)
		{
		if (frm.location.options[k].value == loc)
			{
			frm.location.options[k].selected = true;
			break;
			}
		}	
}


function CountWords (this_field) 
{
	if (show_word_count == null) 
		{
		show_word_count = true;
		}

	if (show_char_count == null) 
		{
		show_char_count = false;
		}

	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;

	if (fullStr.length <2) 
		{
		word_count = 0;
		}

	return word_count;
}


function results_order_by(x)
{
	if (x.selectedIndex != 0)
		{
		var full_url = window.location.href;
		var url = full_url.substring(0, full_url.indexOf('?'));
		var querystring = full_url.substring(full_url.indexOf('?')+1);
		var query = querystring.split('&'); 
		var new_querystring = '';
		var i;

		for(i=0; i<query.length-1; i++)
			{
			if (query[i].indexOf('order_by') == -1)
				{
				new_querystring += query[i] + '&';
				}
			}
		
		new_querystring += 'order_by=' + x.options[x.selectedIndex].value + '&' + query[query.length-1];
		//alert(new_querystring);
		window.location.href = url + '?' + new_querystring;
		}
}


function open_image_window(imageref, titlestr, height, width)
{
	height = height + 80;
	width = width + 50;
	window.open('/yachts/popup.php?image='+imageref+'&title='+titlestr+'&height='+height+'&width='+width,'image','width='+width+',height='+height+',toolbar=no, location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no, resizable=no'); 
	return false;
}

function check_email_validation(email, confirmation)
{
	var ok;
	
	if (email.value == confirmation.value)
		{
		ok = true;
		}
	else
		{
		ok = false;
		alert("The Confirmation Email Address does not match as the Email Address entered, please re-confirm email address.");
		confirmation.value = "";
		confirmation.focus();
		}
		
	return ok;
}

function check_ban_list(email, confirmation)
{
	var banlist = "rivieramotoryachts";
	var ok;
	
	if (email.value.indexOf(banlist) == -1)
		{
		ok = true;
		}
	else
		{
		ok = false;
		alert("The Email Address contains a string that we do not allow as it appears to be spam. Please enter another email address.");
		email.value = "";
		confirmation.value = "";
		email.focus();
		}
	
	return ok;
}



function check_contact_form(frm)
{
	var ok;
	
	ok = check_form(frm);
	
	if (ok)
		{
		ok = check_email_validation(frm.Email, frm.Email_Confirmation);
		}
		
	if (ok)
		{
		ok = check_ban_list(frm.Email, frm.Email_Confirmation);	
		}
		
	return ok;	
}



function entsub(myform) 
{
  if (window.event && window.event.keyCode == 13)
  	{
	ok = check_form(myform);
	if (ok)
		{
		myform.submit();
		}
	else
		{
		return false;			
		}
	}
 else
 	{
    return true;
	}
}