
window.onload = myInit;

function myInit() {
	clarityInit();
}

function validatePhoneNumber(phoneNumber) {
	rePhoneNumber = new RegExp(/^[0-9\s\(\)\+]+$/);
	if (rePhoneNumber.test(phoneNumber)){
		return true;
	}
	return false;
}

function verifyContactForm() {

	if(typeof(document.contactForm.country_of_interest) == 'object'){
		if (document.contactForm.country_of_interest.value == '') {
			alert('Please enter your country of interest');
			document.contactForm.country_of_interest.focus();
			return false;
		}
	}

	if (document.contactForm.name.value == '') {
		alert('Please enter your name');
		document.contactForm.name.focus();
		return false;
	}

	if (document.contactForm.address.value == '' && document.contactForm.h_fromemail.value == '') {
		alert('Please enter either your address or email');
		document.contactForm.h_fromemail.focus();
		return false;
	}

	if (document.contactForm.address.value == '') {

		if (document.contactForm.h_fromemail.value == '') {
			alert('Please enter your email address');
			document.contactForm.h_fromemail.focus();
			return false;
		}

		if (document.contactForm.confirm_email.value == '') {
			alert('Please confirm your email address');
			document.contactForm.confirm_email.focus();
			return false;
		}

		if (document.contactForm.confirm_email.value != document.contactForm.h_fromemail.value) {
			alert('The email adresses you suplpied do not match');
			document.contactForm.confirm_email.focus();
			return false;
		}

	}

	if (document.contactForm.h_fromemail.value == '') {

		if (document.contactForm.address.value == '') {
			alert('Please enter your address');
			document.contactForm.address.focus();
			return false;
		}

	}

	if (document.contactForm.country.value == '') {
		alert('Please enter your country');
		document.contactForm.country.focus();
		return false;
	}

	if (!validatePhoneNumber(document.contactForm.phone.value)) {
		alert('Please enter a valid phone number');
		document.contactForm.phone.focus();
		return false;
	}

	if (document.contactForm.code.value == '') {
		alert('Please enter the verification code');
		document.contactForm.code.focus();
		return false;
	}

	return true;
}



function verifyWhereToStartForm() {

	if (document.form_wheretostart.name.value == '') {
		alert('Please enter your name');
		document.form_wheretostart.name.focus();
		return false;
	}

	if (document.form_wheretostart.h_fromemail.value == '') {
		alert('Please enter your email address');
		document.form_wheretostart.h_fromemail.focus();
		return false;
	}

	if (document.form_wheretostart.address.value == '') {
		alert('Please enter your address');
		document.form_wheretostart.address.focus();
		return false;
	}

	if (!validatePhoneNumber(document.form_wheretostart.phone.value)) {
		alert('Please enter a valid phone number');
		document.form_wheretostart.phone.focus();
		return false;
	}

	if (document.form_wheretostart.code.value == '') {
		alert('Please enter the verification code');
		document.form_wheretostart.code.focus();
		return false;
	}

	return true;
}


function checkEmail(x) {
 var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (filter.test(x)) { 
	 return true; 
	} else { 
		return false; 
	}
}
function verfiyTellafriendForm() {

	oForm = document.tellafriendForm;

	if (oForm.friendsEmail.value == '') {
		alert('Please enter your Friends Email Address.');
		oForm.friendsEmail.focus();
		return false;

	} else if (!checkEmail(oForm.friendsEmail.value)) {
		alert('Please enter a valid Friends Email Address.');
		oForm.friendsEmail.focus();
		return false;

	} else if (oForm.yourName.value == '') {
		alert('Please enter Your Name.');
		oForm.yourName.focus();
		return false;

	} else if (oForm.yourEmail.value == '') {
		alert('Please enter Your Email.');
		oForm.yourEmail.focus();
		return false;

	} else if (!checkEmail(oForm.yourEmail.value)) {
		alert('Please enter a valid  Email Address.');
		oForm.yourEmail.focus();
		return false;

	} else if (oForm.comments.value == '') {
		alert('Please enter Your Comments.');
		oForm.comments.focus();
		return false;

	} else {
		return true;
	}

}


function swapDevImage(newId) {
	document.getElementById('bigDevPic').src = '../uploads/development/dev_'+newId+'.jpg';
}

function popUpWindow(str_URL, str_winName, int_width, int_height) {
	newwindow = window.open(str_URL, str_winName, 'width='+int_width+',height='+int_height+',menubar=off,directories=off,toolbar=off');
	if (window.focus) {newwindow.focus()};
}

function printPage() {
  if (window.print)
    window.print()
  else
    alert("Sorry, your browser doesn't support this feature.");
}

// completely removes all the options for a <select>
function clearSelect(sSelectId) {
	document.getElementById(sSelectId).options.length = 0;
}

// adds a new <option> to a select box
function addSelectOption(sSelectId, sOptText, sOptValue){
	// save the reference to the select object for future use
	var oSelect = document.getElementById(sSelectId);
	// find out how many options there are currently
	// dont subtract the 1 as we need the next index number anyway
	var numOptions = oSelect.options.length;
	// add a new option to the end
	oSelect.options[numOptions] = new Option(sOptText, sOptValue);
}

function changedCountry(oForm) {
	var countryIndex = oForm.country_id.selectedIndex;
	var countryValue = oForm.country_id.options[countryIndex].value;

	var arr_id = eval('country_id_'+countryValue);
	var arr_name = eval('country_name_'+countryValue);

	// remove all the current options
	clearSelect('region_id');

	addSelectOption('region_id', "Now select a region", "");

	for(var i=0; i<= arr_id.length-1; i++){
		// add a new option
		addSelectOption('region_id', arr_name[i], arr_id[i]);
	}

	oForm.region_id.focus();
}

function validateSearchForm() {
	var err = false;

	var countryIndex = document.form_propSearch.country_id.selectedIndex;

	if(countryIndex == 0) {
		var fromPrice = document.form_propSearch.from_price.value;
		if(fromPrice <= 0){
			alert('Please select either a country or a from price.');
			err = true;
		}
	}

	if(err) {
		return false;
	}

	return true;
}


function addBookmark() { 
	if (window.sidebar) { 
		// Mozilla Firefox Bookmark 
		window.sidebar.addPanel(document.title, location.href,""); 
	} else if( window.external ) { 
		// IE Favorite 
		window.external.AddFavorite(location.href, document.title)
	} else if(window.opera && window.print) { 
		// Opera Hotlist 
		return true;
	}
}


function removeFromEnquiry(propId) {
	document.getElementById('wishlist_' + propId).innerHTML = '';
}
