
function LTrim(str) { 
 for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
 return str.substring(k,str.length);
}

function RTrim(str) {
 for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
 return str.substring(0,j+1);
}

function Trim(str) {
 return LTrim(RTrim(str));
}


function nameSelect()
{
 if (isBlank("" + document.buyer.co_name.value)) {
    document.buyer.co_name.value="Company Name"
	document.buyer.co_name.focus()
	document.buyer.co_name.select()
 }
}
	
function isBlank(s) {
	var len=s.length
	var i
	for (i=0;i<len;++i) {
		if (s.charAt(i) != " ") return false
	}
	return true
}

function validate(fieldName,fieldValue) {
	if (isBlank(fieldValue)) {
		alert(fieldName + " cannot be left blank.")
		return false
	}
	return true
}

function validateEmail(fieldName, m) {
	var s = Trim(m)
	var len = s.length
	var i, count
	count = 0
	for (i=0;i<len;++i) {
		if (s.charAt(i) == '@')  ++count
		if (s.charAt(i) == " ")  {
			alert(fieldName + " is not a valid email address.")
			return false
		}
	}
	if (count != 1) {
		alert(fieldName + " is not a valid email address.") 
		return false
	}
	return true
}



function validateNum(fieldName,m) {
	var s = Trim(m)
	var len = s.length
	var i
	for (i=0;i<len;++i) {
		 switch (s.charAt(i)) {
			  case "0":
			     break
			  case "1":
			     break
		      case "2":
			     break
			  case "3":
			      break
			  case "4":
			      break
			  case "5":
			      break
			  case "6":
			      break
			  case "7":
			      break
			  case "8":
			      break
			  case "9":
			      break
			  default:
			      alert(fieldName + " must only contain numbers 0-9 and have no spaces between numbers.")
			      return false
		    }
	 }
	 return true
 }



function validateCompanyName() {
   validate("The company name field",document.buyer.co_name.value)
}



function limitselection() {

var maxFreeItems=4
var n=document.forms[0].elements['industry[]'].length
var num = 0

for (i=0;i<n;i++) {
     if (document.forms[0].elements['industry[]'][i].selected) {
	     num = num + 1
		}	 
}

if (num > maxFreeItems) {
 
   alert('Remarks! You can only choose '+maxFreeItems+' industries.')

     //alert('Remarks! You can only choose '+maxFreeItems+' industries.')
	 	 
	for (i=0;i<n;i++) {
        document.forms[0].elements['industry[]'][i].selected=false
	    num = 0
	 }
	
   }
          
}

  
function validateForm() {
	if (!validate("The company name field", document.buyer.co_name.value)) {
	   document.buyer.co_name.focus()
	   return false
	  
	}
	
	if (!validate("The company address field", document.buyer.co_address.value)) {
	   document.buyer.co_address.focus()
	   return false
	  
	}
	if (!validate("The company email field", document.buyer.co_email.value)) {
	   document.buyer.co_email.focus()
	   return false
	  
	}
	
	if (!validateEmail("The company email field", document.buyer.co_email.value)) {
	   document.buyer.co_email.focus()
	   return false
   }
	
	if (!validate("The contact first name field", document.buyer.contact_first.value)) {
	   document.buyer.contact_first.focus()
	   return false
	  
	}
	
	if (!validate("The contact family name field", document.buyer.contact_family.value)) {
	   document.buyer.contact_family.focus()
	   return false
	  
	}
	
	if (!validate("The contact email address field", document.buyer.contact_email.value)) {
	   document.buyer.contact_email.focus()
	   return false
	  
	}
	
	if (!validateEmail("The contact email field", document.buyer.contact_email.value)) {
	   document.buyer.contact_email.focus()
	   return false
   }
   	
	
	if (!validate("The telephone country code field", document.buyer.tel_country.value)) {
	   document.buyer.tel_country.focus()
	   return false
	  
	}
	
	if (!validateNum("The telephone country code field", document.buyer.tel_country.value)) {
	   document.buyer.tel_country.focus()
       return false
   }
	
	if (!validate("The local telephone number field", document.buyer.tel_local.value)) {
	   document.buyer.tel_local.focus()
	   return false
	  
	}
	
	if (!validateNum("The local telephone numbers field", document.buyer.tel_local.value)) {
	   document.buyer.tel_local.focus()
       return false
   }
	
	if (!validateNum("The fax country code field", document.buyer.fax_country.value)) {
	   document.supplier.fax_country.focus()
       return false
   }
	
	if (!validateNum("The local fax numbers field", document.buyer.fax_local.value)) {
	   document.buyer.fax_local.focus()
       return false
   }
   
     
    if (!validateNum("The year of establishment field", document.buyer.year_of_establish.value)) {
	   document.buyer.year_of_establish.focus()
       return false
   }
	
	
	if (!validate("The login ID field", document.buyer.loginID.value)) {
	   document.buyer.loginID.focus()
	   return false
	  
	}
	
	if (!validateEmail("The login ID field", document.buyer.loginID.value)) {
	   document.buyer.loginID.focus()
	   return false
   }
	
	if (!validate("The password field", document.buyer.password1.value)) {
	   document.buyer.password1.focus()
	   return false
	}
	
   if (!validate("The confirm password field", document.buyer.password2.value)) {
	   document.buyer.password2.focus()
	   return false
	}
	
	
	if (document.forms[0].elements['industry[]'].value == "" || document.forms[0].elements['industry[]'].value == " " ) {
	 
	  alert("The industry field cannot be left blank")	
	 // document.forms[0].elements['industry[]'].focus()
	  return false
     }
	
	//if (!validate("The industry field", document.forms[0].elements['industry[]'].value)) {
	 // document.forms[0].elements['industry[]'].focus()
	 // return false
//	}
	
   if (document.buyer.password1.value != document.buyer.password2.value) {
	   alert("The two passwords just typed do not match.") 
	   document.buyer.password1.focus()
	   document.buyer.password1.select()
	   return false
   }

}

 function validateForm2() {
	if (!validateEmail("The email field", document.supplier_email.email.value)) {
	   document.supplier_email.email.focus()
	   return false
   }
}
  
function validateForm3() {
	if (!validateEmail("The email field", document.edit_information.email.value)) {
	   document.edit_information.email.focus()
	   return false
   }
}

  
function validateForm4() {
	if (!validateEmail("The current login ID", document.change_login.login.value)) {
	   document.change_login.login.focus()
	   return false
   }
   
   if (!validateEmail("The new login ID", document.change_login.new_login.value)) {
	   document.change_login.new_login.focus()
	   return false
   }
}

function validateForm5() {
	   
   if (!validateEmail("Your email", document.email_response.sendemail.value)) {
	   document.email_response.sendemail.focus()
	   return false
   }
}

//make a pop-up window function

