var ydrCredentialsClient = new function () 
{
	this.validate = function(element)
	{
		var newElement;
		
		// country and postcode are the values used by the app - they have no special validations
		// these are overlaid by countryX and postCodeX which are selectList and special validated respectively
		// we flip user view between these dependent on if we are in the UK but always maintain the value in country/postcode
		if (element == null || element.id == "countryX")
		{
			if (element) 
				newElement = element
			else
				newElement = document.getElementById("countryX");
			
			if (newElement != null && ! ydrClientScripts.isHidden(newElement))
			{ //stop();
				if (newElement.selectedIndex<0)
					newElement.selectedIndex=0;
				 
				var selectedCountry = ydrBrowser.html.elementValue(newElement.options[newElement.selectedIndex]).replace(/\./g,"").replace(/U K/,"UK");
				// note the two UK values are not in the drop down list!
				if ("|England|Scotland|Wales|Northern Ireland|UK|United Kingdom|".indexOf(selectedCountry)<0)
				{
					newElement.style.visibility="hidden";
					with (document.getElementById("country"))
					{
						style.visibility = "inherit";
						if (selectedCountry == "<Other>")
							value=""
						else
							value = selectedCountry;
						focus();
					};
					
					document.getElementById("postcodeX").style.visibility = "hidden";
					document.getElementById("postcode").style.visibility = "inherit";
				}
				else
				{
					ydrClientScripts.elementValueUpdate(document.getElementById("country")
						, ydrBrowser.html.elementValue(newElement.options[newElement.selectedIndex]));
					
					newElement = document.getElementById("postcodeX");
				
					if (newElement && newElement.style.visibility != "hidden")
						ydrClientScripts.elementValueUpdate(document.getElementById("postcode"), ydrBrowser.html.elementValue(newElement));
				};
			};					
		};
		
		if (element != null && element.id == "country" && ! ydrClientScripts.isHidden(element))
		{
			var i;
			var selectedCountry=ydrBrowser.html.elementValue(element).trim().toLowerCase().replace(/\.| /g,"");
			
			if (selectedCountry == "uk" || selectedCountry == "unitedkingdom")
				selectedCountry="england";
				
			if ("|england|scotland|wales|northernireland|".indexOf(selectedCountry)>=0)
			{
				newElement = document.getElementById("countryX");
				
				for (i=0; i<newElement.options.length-1; i++)
				{
					with (newElement.options[i])
					{
						if (value.toLowerCase() ==  selectedCountry)
						{
							element.style.visibility="hidden";
							
							selected=true;
							newElement.style.visibility = "inherit";
												
							document.getElementById("postcodeX").style.visibility = "inherit";
							document.getElementById("postcode").style.visibility = "hidden";
							ydrClientScripts.elementValueUpdate(document.getElementById("postcodeX")
								, ydrBrowser.html.elementValue(document.getElementById("postcode")));
							break;
						};				 
					};			
				};		
			};
		};
				
		if (element != null && element.id=="address"  && ! ydrClientScripts.isHidden(element) )
			ydrClientScripts.elementValueUpdate(element, ydrBrowser.html.elementValue(element).replace(/\n\s+/g,"\n").replace(/\n\n/g,"\n").replace(/\t/g,""));
	
		var response="";
		if (document.getElementById("email"))
		{
			try 
			{
				if (ydrBrowser.html.elementValue(document.getElementById("email")) == ydrBrowser.html.elementValue(document.getElementById("noAutoFillEmailOld")))
					response="<p class='warning'><span class='object'>"
						+ ydrPopup.firstPeerTD(document.getElementById("email")).innerHTML 
						+"</span> new eMail should differ from old eMail</p>";
			} catch (e) {};
		};
		
		if (document.getElementById("TELEPHONE"))
		{
			try 
			{
				if (! ydrClientScripts.isHidden(document.getElementById("TELEPHONE")))
				{
					with (document.getElementById("TELEPHONE"))
					{		
						var newMessage;
						var telephone=value.toLowerCase().replace(/[^+0-9 x]/g," ").replace(/\s+/g," ").trim(); // only allow + digits spaces and x for extension
						
						// remove spurious X
						var entries;
						entries = telephone.split(/x/);
						if (entries.length>2)
							telephone = entries[0]+" x"+entries[1];
						
						//  ensure country code OK
						if (telephone.substr(0,2) != "+ " || telephone == "")
						{
							if (telephone.charAt(0) == "+")
								telephone = telephone.replace(/\+/,"+ ")
							else
							{
								if (telephone.substr(0,2) == "00")
									telephone = telephone.replace(/00/,"+ 44 ")
									
								else if (telephone.substr(0,1) == "0")
									telephone = telephone.replace(/0/,"+ 44 ") // in future look it up from REGION
									
								else
									newMessage = "I did not detect the country/trunk part of the telephone number.  Please use <span class='object'>+</span> followed by country code, city/area code and number, optionally followed by an <span class='object'>x</span> and an extension number</p>";
								
								if (newMessage == null)
									newMessage = "Your phone number has been reformatted, please check the country code.  If it is correct then please try again.";
								
								response +="<p class='info'><span class='object'>"
									+ ydrPopup.firstPeerTD(document.getElementById("TELEPHONE")).innerHTML
									+"</span> " + newMessage + "</p>";
							}
						};
						
						// remove spurious extra + and x
						entries = telephone.split(/\+/);
						if (entries.length>1)
							telephone = "+ " +entries.join(" ");
						
						if (telephone.charAt(telephone.length-1) == "x")
							telephone = telephone.substr(0,telephone.length-1).trim();
						
						if (telephone.split(/x/)[0].length < 12)
							response +="<p class='error'><span class='object'>"
									+ ydrPopup.firstPeerTD(document.getElementById("TELEPHONE")).innerHTML
									+"</span> Your telephone number does not appear to be long enough for the international format required</p>";
						
						value=telephone;
					}
				};
			} catch (e) {};
		};
		return response;
	};
}

