/* Common JS functions */

// Navigation JS - Required for IE6
sfHover = function() {
	// Navigation
	var sfEls = document.getElementById("navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
	
	// Find a Retailer
	var sfEls = document.getElementById("retailer").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// Hide search value on focus
function hide_value(el)
{
	if(el.V) {
	if (el.value == el.V) {
	el.value = '';
	}
	} else {
	el.V = el.value;
	el.value = '';
	}
}

function popi(type, sel_val){
	
	loadXMLDoc();
	url = "../coverage_and_roaming_v4/phone.php?type="+type

	if (type == "model"){
		url += "&make="+sel_val;
	}

	if (xmlhttpObj!=null){
		xmlhttpObj.onreadystatechange=function(){
										if (xmlhttpObj.readyState == "Ready" || xmlhttpObj.readyState == "4"){
											document.getElementById(type+"_td").innerHTML = xmlhttpObj.responseText;
										}
										};
	
		xmlhttpObj.open("GET", url, true);
		xmlhttpObj.send(null);
	
	}else{
  		alert("Your browser does not support XMLHTTP.");
  	}
}
	
	
function validate_newsletter_form() {
	
					var error = false;
					var message = 'The following needs your correction: \n\n';
				
					if (document.getElementById('newsletter_email').value == "") {message += ('- Please enter your email address \n');	error = true}
					if(isEmail(document.getElementById('newsletter_email')) == false){ message+='- Please enter a correct email address \n'; error = true}
					if(error){
						alert(message);
						return false;
					}else{
						return true;
					}		
		
}

function validate_contact_us() {
	
					var error = false;
					var message = 'The following needs your correction: \n\n';
				
					if (document.getElementById('name').value == "") {message += ('- Please enter your name \n');	error = true}
					if (document.getElementById('email').value == "") {message += ('- Please enter your email address \n');	error = true}
					if(isEmail(document.getElementById('email')) == false){ message+='- Please enter a correct email address \n'; error = true}
					
					if (document.getElementById('telephone').value == "") {message += ('- Please enter your contact number \n');	error = true}
					if(IsNumeric(document.getElementById('telephone').value)==false){ message+='- Contact number is not numeric, use - as a seperator \n'; error = true}
					
					if (document.getElementById('industry').value == "") {message += ('- Please enter an industry \n');	error = true}
					if (document.getElementById('territory').value == "") {message += ('- Please enter a territory \n');	error = true}
					
					if (document.getElementById('pre_meeting_notes').value == "") {message += ('- Please enter some pre_meeting_notes \n');	error = true}

					if(error){
						alert(message);
						return false;
					}else{
						return true;
					}
}
				
function IsNumeric(strString)
				   //  check for valid numeric strings	
				   {
				   var strValidChars = "0123456789-";
				   var strChar;
				   var blnResult = true;
				
				   if (strString.length == 0) return false;
				
				   //  test strString consists of valid characters listed above
				   for (i = 0; i < strString.length && blnResult == true; i++)
					  {
					  strChar = strString.charAt(i);
					  if (strValidChars.indexOf(strChar) == -1)
						 {
						 blnResult = false;
						 }
					  }
				   return blnResult;
}
				
				
function isEmail(emailID) {
						
						if ((emailID.value==null)||(emailID.value=="")){
							return false
						}
						var str = emailID.value;
						var at="@"
						var dot="."
						var lat=str.indexOf(at)
						var lstr=str.length
						var ldot=str.indexOf(dot)
						if (str.indexOf(at)==-1){
						   return false
						}
				
						if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
						   return false
						}
				
						if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
							return false
						}
				
						 if (str.indexOf(at,(lat+1))!=-1){
							return false
						 }
				
						 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
							return false
						 }
				
						 if (str.indexOf(dot,(lat+2))==-1){
							return false
						 }
						
						 if (str.indexOf(" ")!=-1){
							return false
						 }
				
						 return true					
}
