/*
 * Validate text area maxlength
 */
function imposeMaxLength(Object) {

	var mlength=Object.getAttribute? parseInt(Object.getAttribute("maxlength")) : ""

	document.form_feedback.text_counter.value = mlength - Object.value.length

	if (Object.getAttribute && Object.value.length > mlength) {
		Object.value = Object.value.substring(0,mlength)
		document.form_feedback.text_counter.value = 0
	}
//	return (Object.value.length <= MaxLen);
}

/*
 * validate fields inside feedback form in contact.php
 */
function ValidateFeedbackForm() {

	var radio_salutation=document.form_feedback.radio_salutation
	var isRadio_salutation_selected = false
	var text_name=document.form_feedback.text_name
	var emailID=document.form_feedback.text_email
	var textarea_message=document.form_feedback.textarea_message
	var isError = false

	//validate salutation
	for (var i=0; i < radio_salutation.length; i++) {
		if (radio_salutation[i].checked) {
			isRadio_salutation_selected=true
		} 
	}
	if ( (isRadio_salutation_selected == false) )	{
		document.getElementById('error_salutation').innerHTML = "Salutation is required"
		isError = true
	} else {
		document.getElementById('error_salutation').innerHTML = ""
	}
	
	//validate name field

	if ( len(trim(text_name.value)) == 0 ) {
		document.getElementById('error_name').innerHTML = "Name is required"
		isError = true
	} else {
		document.getElementById('error_name').innerHTML = ""
	}
	
	//validate email
	if ( len(trim(emailID.value)) == 0 ) {
		document.getElementById('error_email').innerHTML = "Email is required"
		isError = true
	} else if ( !(emailID.value==null) && (echeck(emailID.value)==false) ) {
		document.getElementById('error_email').innerHTML = "Invalid email"
		isError = true
	} else {
		document.getElementById('error_email').innerHTML = ""
	}

	//validate message field
	if ( len(trim(textarea_message.value)) == 0 ) {
		document.getElementById('error_message').innerHTML = "Message is required"
		isError = true
	} else {
		document.getElementById('error_message').innerHTML = ""
	}

	if (isError) {
		return false
	}

	document.form_feedback.submit_button.value="Processing.."
	document.form_feedback.submit_button.disabled=true

	return true
}

/*
 * validate fields inside course registration form in courseregister.php
 */
function validateCourseRegistrationForm() {

	var radio_salutation = document.form_courseregistration.Salutation
	var text_firstname = document.form_courseregistration.FirstName
	var text_lastname = document.form_courseregistration.LastName
	var text_companyname = document.form_courseregistration.CompanyName
	var text_addressstreet1 = document.form_courseregistration.AddressStreet1
	var text_postalcode = document.form_courseregistration.PostalCode
	var text_email = document.form_courseregistration.Email
	var text_confirmemail = document.form_courseregistration.ConfirmEmail
	var text_contact = document.form_courseregistration.Contact
	var select_country = document.form_courseregistration.Country
	var select_course = document.form_courseregistration.Course
	var text_securitycode = document.form_courseregistration.SecurityCode
	isRadio_payment_selected = false
	isError = false
	var isRadio_salutation_selected = false

	//validate salutation
	for (var i=0; i < radio_salutation.length; i++) {
		if (radio_salutation[i].checked) {
			isRadio_salutation_selected=true
		} 
	}
	if ( (isRadio_salutation_selected == false) )	{
		document.getElementById('error_salutation').innerHTML = "Salutation is required"
		isError = true
	} else {
		document.getElementById('error_salutation').innerHTML = ""
	}

	//validate first name
	if ( len(trim(text_firstname.value)) == 0 ) {
		document.getElementById('error_firstname').innerHTML = "First name is required"
		isError=true
	} else {
		document.getElementById('error_firstname').innerHTML = ""
	}

	//validate last name
	if ( len(trim(text_lastname.value)) == 0 ) {
		document.getElementById('error_lastname').innerHTML = "Last name is required"
		isError=true
	} else {
		document.getElementById('error_lastname').innerHTML = ""
	}

	//validate company's name
	if ( len(trim(text_companyname.value)) == 0 ) {
		document.getElementById('error_companyname').innerHTML = "Company name is required"
		isError=true
	} else {
		document.getElementById('error_companyname').innerHTML = ""
	}

	//validate address street 1
	if ( len(trim(text_addressstreet1.value)) == 0 ) {
		document.getElementById('error_addressstreet').innerHTML = "Address is required"
		isError=true
	} else {
		document.getElementById('error_addressstreet').innerHTML = ""
	}

	//validate postal code
	if ( len(trim(text_postalcode.value)) == 0 ) {
		document.getElementById('error_postalcode').innerHTML = "Postal Code is required"
		isError=true
	} else {
		document.getElementById('error_postalcode').innerHTML = ""
	}

	//validate country field
	if ( (select_country.value == null) || (select_country.value == "") ) {
		document.getElementById('error_country').innerHTML = "Country is required"
		isError=true
	} else {
		document.getElementById('error_country').innerHTML = ""
	}

	//validate email
	if ( len(trim(text_email.value)) == 0 ) {
		document.getElementById('error_email').innerHTML = "Email is required"
		isError=true
	} else if (echeck(text_email.value)==false) {
		document.getElementById('error_email').innerHTML = "Invalid Email"
		isError=true
	} else {
		document.getElementById('error_email').innerHTML = ""
	}

	//validate confirm email
	if ( echeck(text_email.value) 
		&& ((text_confirmemail.value == null) || (text_confirmemail.value == "")) ) {
		document.getElementById('error_email').innerHTML = "Re-enter email"
		isError=true
	}
	if ( echeck(text_email.value) 
		&& !((text_confirmemail.value == null) || (text_confirmemail.value == ""))
		&& !(text_email.value == text_confirmemail.value) ) {

		document.getElementById('error_email').innerHTML = "Email addresses do not match"
		isError=true
	}

	//validate contact number
	if ( len(trim(text_contact.value)) == 0 ) {
		document.getElementById('error_contact').innerHTML = "Contact number is required"
		isError=true
	} else {
		document.getElementById('error_contact').innerHTML = ""
	}

	//validate course field
	if ( (select_course.value == null) || (select_course.value == "") ) {
		document.getElementById('error_course').innerHTML = "Select a course"
		isError=true
	} else {
		document.getElementById('error_course').innerHTML = ""
	}

	//validate payment options
//	var radio_payment = document.form_courseregistration.Payment
/*	for (var i=0; i < radio_payment.length; i++) {
		if (radio_payment[i].checked) {
			isRadio_payment_selected=true
		} 
	}
	if ( (isRadio_payment_selected == false) )	{
		document.getElementById('error_payment').innerHTML = "Select a payment method"
		isError = true
	} else {
		document.getElementById('error_payment').innerHTML = ""
	}
*/
	//validate security code
	if ( len(trim(text_securitycode.value)) == 0 ) {
		document.getElementById('error_securitycode').innerHTML = "Please enter the security codes"
		isError=true
	} else {
		document.getElementById('error_securitycode').innerHTML = ""
	}



	if (isError) {
		return false
	}

	document.form_courseregistration.Button_Submit.value="Please wait..."
	document.form_courseregistration.Button_Submit.disabled=true

	return true
}
/*
function validatejobrole() {
	var select_jobrole = document.form_courseregistration.JobRole
	if ( (select_jobrole.value == "Other") ) {
		document.form_courseregistration.JobRoleOther.disabled = false
	} else {
		document.form_courseregistration.JobRoleOther.disabled = true
		document.form_courseregistration.JobRoleOther.value= ""
	}
}

function validatecompanyindustry() {
	var select_companyindustry = document.form_courseregistration.CompanyIndustry
	if ( (select_companyindustry.value == "Other") ) {
		document.form_courseregistration.CompanyIndustryOther.disabled = false
	} else {
		document.form_courseregistration.CompanyIndustryOther.disabled = true
		document.form_courseregistration.CompanyIndustryOther.value= ""
	}
}
*/
//********************helper functions******************

/*
 * $str is a string that may contain valid email address
 * return true if email is valid
 */
function echeck(str) {

	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
}


/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
PURPOSE: Remove leading blanks from our string.
IN: str - the string we want to LTrim
*/
function lTrim(str) {
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
PURPOSE: Remove trailing blanks from our string.
IN: str - the string we want to RTrim
*/
function rTrim(str) {
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
   {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
function trim(str) {
   return rTrim(lTrim(str));
}

function len(str) {
	return str.length;
}