/**
 * Library of javascript functions
 *
 * @author	Olivier Berrewaerts <olivier@bureau347.com>
 * @version 1.1
 */

// detect browser type
var dom = (document.getElementById) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;

// set the image
function setImage(imgname, imgurl) {
	if (document.images[imgname]) {
		document.images[imgname].src = imgurl;
	}
}

// submit the form
function submitForm(theformname, theaction) {
	if (document.forms[theformname]) {
		document.forms[theformname].act.value = theaction;
		document.forms[theformname].submit();
	}
}

// submit the form with a new value
function submitFormField(theformname, theaction, thefield, thevalue) {
	if (document.forms[theformname]) {
		document.forms[theformname].elements[thefield].value = thevalue;
		document.forms[theformname].act.value = theaction;
		document.forms[theformname].submit();
	}
}

// open the link in a new window
// return false if the popup is shown, true otherwise
function openWindow(oLink, name, width, height, params) {
	var href = '';
	if (oLink.getAttribute) url = oLink.getAttribute('href');
	if (href=='') href = oLink.href;
	var all_params = '';
	all_params += 'width=' + width + ',height=' + height;
	if (params && params!='') all_params += ','+params;
	// all is ready, open the popup and focus on it
	var oPopup = window.open(href, (name && name!='' ? name : 'popup'), all_params);
	if (oPopup) oPopup.focus();
	return (oPopup?false:true);
}

// open the link in the opener window
function showLinkInOpener(href) {
	if (window.opener) {
		window.opener.document.location.href = href;
		window.opener.focus();
	} else {
		// opener don't exist, open link in new window
		window.open(href, 'popup');
	}
}

// show an element
function show(id) {
	if (ns4) {
		if (document.layers[id]) {
			document.layers[id].visibility = "show";
			document.layers[id].display = "block";
		}
	} else if (ie4) {
		if (document.all[id]) {
			document.all[id].style.visibility = "visible";
			document.all[id].style.display = "block";
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.visibility = "visible";
			document.getElementById(id).style.display = "block";
		}
	}
}

// hide an element
function hide(id) {
	if (ns4) {
		if (document.layers[id]) {
			document.layers[id].visibility = "hide";
			document.layers[id].display = "none";
		}
	} else if (ie4) {
		if (document.all[id]) {
			document.all[id].style.visibility = "hidden";
			document.all[id].style.display = "none";
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			document.getElementById(id).style.visibility = "hidden";
			document.getElementById(id).style.display = "none";
		}
	}
}

// hide multiple div, beginning with 'pre' as prefix and a number
function hideMulti(pre, start, end) {
	var s, i;
	for (i=start; i<=end; i++) {
		s = pre+''+i;
		hide(s);
	}
}

// show a div if it is visible, hide it if it is hidden
function toggle(id) {
	if (ns4) {
		if (document.layers[id]) {
			if (document.layers[id].visibility == "hide" || document.layers[id].display == "none")
				show(id);
			else
				hide(id);
		}
	} else if (ie4) {
		if (document.all[id]) {
			if (document.all[id].style.visibility == "hidden" || document.all[id].style.display == "none")
				show(id);
			else
				hide(id);
		}
	} else if (dom) {
		if (document.getElementById(id)) {
			if (document.getElementById(id).style.visibility == "hidden" || document.getElementById(id).style.display == "none")
				show(id);
			else
				hide(id);
		}
	}
}


// trim the left and right spaces of a string
function trim(str) {
	return str.replace(/^\s+|\s+$/, '');
};

// returns true if the string is empty
function isEmpty(str) {
	return (str == null) || (str.length == 0);
}

// returns true if the string is a valid email
function isEmail(str) {
	if (isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

// build the dropdown with the region list
// uses a global variables "regionList"
function buildRegionList(list_country, list_region) {
	if (countriesList && regionList && list_country.selectedIndex > -1) {
		var country_code = list_country.options[list_country.selectedIndex].value;
		if(country_code == '') {
			list_region.options[0].text = '';
			list_region.options.length = 1;
			return;
		}
		// set the region label
		list_region.options[0].text = countriesList[country_code].region_label;
		// remove all elements of the list except the first one
		list_region.options.length = 1;
		// add new elements to the list
		if (regionList[country_code]) {
			var i = 1;
			for (var el in regionList[country_code]) {
				list_region.options[i++] = new Option(regionList[country_code][el],el);
			}
		}
	}
}

// build the dropdown with the region list
// uses a global variables "zipcodeList"
function buildZipcodeList(list_country, list_zipcode) {
	if (countriesList && zipcodeList && list_country.selectedIndex > -1) {
		var country_code = list_country.options[list_country.selectedIndex].value;
		if(country_code == '') {
			list_zipcode.options[0].text = '';
			list_zipcode.options.length = 1;
			return;
		}
		// set the zipcode label
		list_zipcode.options[0].text = countriesList[country_code].zipcode_label;
		// remove all elements of the list except the first one
		list_zipcode.options.length = 1;
		// add new elements to the list
		if (zipcodeList[country_code]) {
			var i = 1;
			for (var el in zipcodeList[country_code]) {
				list_zipcode.options[i++] = new Option(zipcodeList[country_code][el][1],el);
			}
		}
	}
}

// build the dropdown with the region list
// uses a global variables "zipcodeList"
function buildRegionZipcodeList(list_country, list_region, list_zipcode) {
	if (zipcodeList && list_country.selectedIndex > -1) {
		var country_code = list_country.options[list_country.selectedIndex].value;
		if (list_region.selectedIndex > -1)
			var region_id = list_region.options[list_region.selectedIndex].value;
		else
			var region_id = '';
		// remove all elements of the list except the first one
		list_zipcode.options.length = 1;
		// add new elements to the list
		if (zipcodeList[country_code]) {
			var i = 1;
			for (var el in zipcodeList[country_code]) {
				if (region_id == '' || zipcodeList[country_code][el][0] == region_id)
					list_zipcode.options[i++] = new Option(zipcodeList[country_code][el][1],el);
			}
		}
	}
}

// hide a div after x seconds, return the timer
function hideAfter(id, sec) {
	return setTimeout("hideAfter_real('"+id+"')", sec*1000);
}
// hide a div after x seconds
function cancelHideAfter(timer) {
	if (timer != null) {
		clearTimeout(timer);
		timer = null;
	}
}
// hide a div after x seconds
function hideAfter_real(id) {
	hide(id);
}

// show the virtual visit applet
function showApplet(p, l, f) {
	document.write('<APPLET archive="'+p+'java/PurePlayer.jar" code="PurePlayer" id="vr" name="vr" width="660" height="370">\n');
	document.write('  <param name="gui" value="'+p+'java/gui-'+l+'.xml">\n');
	document.write('  <param name="panorama" value="'+f+'">\n');
	document.write('  <param name="quality" value="200">\n');
	document.write('  <param name="optimizememory" value="true">\n');
	document.write('</APPLET>');
}

// toogle the price ranges selects
function togglePriceRanges(typeSelected, typeUnselected)
{
	$(typeSelected + '_price_ranges').parentNode.style.display = 'block';
	$(typeUnselected + '_price_ranges').selectedIndex = 0;
	$(typeUnselected + '_price_ranges').parentNode.style.display = 'none';
}

// easy get by id
function $(id)
{
	return document.getElementById(id);
}