// directory-related functions

function showContact(obj)
{
	// gets the selected option and fill the div with content
	var countryid	= Country(obj.value)[0] ;
	var countryname	= '' ;//'<span class="countryname">' + Country(obj.value)[1] ; + '</span>'
	var details		= document.getElementById('contactdetails') ;
	if(document.getElementById(countryid))
		{
			details.innerHTML = countryname + document.getElementById(countryid).innerHTML ;
			details.style.display = 'none';
			details.style.display = 'block' ;
		}
		else
		{
			details.innerHTML = '';
			details.style.display = 'none';
		}
}

function contactDetails(obj)
{
	var pArea = obj.value[0]
	creerRequete() ;
	var url = '/lib/httprequest.asp?mode=contactdetails&area='+pArea; 
	requete.open('GET', url, true);
	
	requete.onreadystatechange = function()
		{
			if(requete.readyState == 4)
			{ 
				if(requete.status == 200) InjectResponseIn('contactdetails',requete)
			}
		};
	requete.send(null);	
	
}



function Country(string)
{
	return 	string.split('-');
}

function AddInput(txtType, txtName, txtId, txtValue, obj)
{

	var newInput= document.createElement("INPUT");
		newInput.value	=txtValue;
		newInput.name	= txtName;
		newInput.id		= txtId;
		newInput.type	= txtType;
	obj.appendChild(newInput); 
} 




/* useful functions  */
function verif(email) {
	var arobase = email.indexOf("@")
	var point = email.lastIndexOf(".")
	if((arobase < 3)||(point + 2 > email.length)
	   ||(point < arobase+3)) return false
	return true
	}
	
 function testform(country,email) {
	if(country.value=="0") {
	   alert("Please select a country!")
	   country.focus(); return false }
	if(!verif(email.value)) {
	   alert("Email not valid!"); email.value = ""
	   email.focus(); return false }
	 // I add the current recipient to the sendmail
	var recipient = trim(document.getElementById('recipient').innerHTML);
	AddInput('hidden','recipient','recipient',recipient,document.contact) 
	
	return true
	}

 function testshopform(email, customerid) {
	if(customerid.value=="") {
	   alert("Please enter your customer ID!")
	   customerid.focus(); return false }
	if(!verif(email.value)) {
	   alert("Email not valid!"); email.value = ""
	   email.focus(); return false }
	return true;
	}


 function testform2(country,email,name,company,language) {
	
	if(!verif(email.value)) {
	   alert("Email not valid!"); email.value = ""
	   email.focus(); return false }
	if(name.value=="") {
		alert("Name is mandatory")
		name.focus(); return false }
	if(language.value=="0") {
		alert("Please select your language")
		language.focus(); return false }
	if(company.value=="") {
		alert("Company is mandatory")
		company.focus(); return false }
	if(country.value=="0") {
		alert("Please select a country")
		country.focus(); return false }	
	
	   
	   
	 // I add the current recipient to the sendmail
	var recipient = trim(document.getElementById('recipient').innerHTML);
	AddInput('hidden','recipient','recipient',recipient,document.contact) 
	return true
	}


function trim(aString)
{
	var regExpBeginning = /^\s+/;
	var regExpEnd       = /\s+$/;
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}


// Ajax functions
function InjectResponseIn(ref,requete)
{
	document.getElementById(ref).innerHTML = requete.responseText;
	document.getElementById(ref).style.display = 'block';
}
