// JavaScript Document

// Validating Login Form
// Checking Variables are: Username (f_username) and Password (f_password)
function login() { 
   var cL = document.forms[0];
   
   if(IsEmpty(cL.f_username)) {
		alert("Username cannot be empty!");
		SetFocusSelect(cL.f_username);
		return;
   }

   if(IsEmpty(cL.f_password)) {
		alert("Password cannot be empty!");
		SetFocusSelect(cL.f_password);
		return;
   }
	cL.submit();
}
// end login form validation


// Script for validating news letter signup form
// Checking Variables are: Username (f_news_name) and Password (f_news_email)
function newsLetter() { 
   var nF = document.forms[0];

   if(IsEmpty(nF.f_news_name)) {
		alert("Name cannot be empty!");
		SetFocusSelect(nF.f_news_name);
		return;
   }

	if(!validateEmail(nF.f_news_email.value)) {
		alert("Please enter a valid email address.");
		nF.f_news_email.select();
		return;
	}
	nF.submit();
}
// end news letter signup validation

// Script for validating Quick Contact form
function quickContact() {
   var qC = document.forms[0];

   if (qC.f_enq_subject.value == 0){
       alert("Please select a subject of Enquiry!");
       qC.f_enq_subject.focus();
       return;
   }

   if(IsEmpty(qC.f_name)) {
		alert("Please enter your name!");
		SetFocusSelect(qC.f_name);
		return;
   }

	if(!validateEmail(qC.f_email.value)) {
		alert("Please enter a valid email address.");
		qC.f_email.select();
		return;
	}

   if(IsEmpty(qC.f_inquiry)) {
       alert("Please enter your comments/inqueries!");
		SetFocusSelect(qC.f_inquiry);
		return;
   }
	qC.submit();
}
// end Quick Contact Form validation

// Script for validating Contact Us form
function checkContactForm(){
   var cntForm = document.forms[0];

   if(IsEmpty(cntForm.f_name)) {
       alert("Please enter your name!");
		SetFocusSelect(cntForm.f_name);
		return false;
   }

   if(IsEmpty(cntForm.f_position)) {
       alert("Please enter your Position!");
		SetFocusSelect(cntForm.f_position);
		return false;
   }

   if(IsEmpty(cntForm.f_co_name)) {
       alert("Please enter your Company Name!");
		SetFocusSelect(cntForm.f_co_name);
		return false;
   }

   if(IsEmpty(cntForm.f_contact_no)) {
       alert("Please enter your Contact Telephone Number!");
		SetFocusSelect(cntForm.f_contact_no);
		return false;
   }

	if(!validateEmail(cntForm.f_email.value)) {
		alert("Please enter a valid email address.");
		SetFocusSelect(cntForm.f_email);
		return false;
	}

	if (cntForm.f_enq_subject.value == 0){
       alert("Please select a subject of Enquiry!");
       cntForm.f_enq_subject.focus();
       return false;
   }

   if(IsEmpty(cntForm.f_enquiry)) {
	   var confirmMsg = confirm("You've not typed any Enquiries/Comments!\n\nClick OK to Send the form without typing Enquiries/Comments\nClick CANCEL to enter your Enquiries/Comments");
	   if(confirmMsg == false) {
		   SetFocusSelect(cntForm.f_enquiry);
		   return false;
	   }
   }

   return true;
}
// End Contact us form validation

// Script for validating Sales Enquiry form
function checkSalesEnquiryForm() {
   var salesForm = document.forms[0];

   if(IsEmpty(salesForm.f_name)) {
       alert("Please enter your name!");
		SetFocusSelect(salesForm.f_name);
		return false;
   }

   if(IsEmpty(salesForm.f_position)) {
       alert("Please enter your Position!");
		SetFocusSelect(salesForm.f_position);
		return false;
   }

   if(IsEmpty(salesForm.f_companyName)) {
       alert("Please enter your Company Name!");
		SetFocusSelect(salesForm.f_companyName);
		return false;
   }

   if(IsEmpty(salesForm.f_contactNumber)) {
       alert("Please enter your Contact Telephone Number!");
		SetFocusSelect(salesForm.f_contactNumber);
		return false;
   }

	if(!validateEmail(salesForm.f_email.value)) {
		alert("Please enter a valid email address.");
		SetFocusSelect(salesForm.f_email);
		return false;
	}

	if( salesForm.f_interested_solutions.value == 0 ) {
		alert("Please Select: Interested Solutions");
		salesForm.f_interested_solutions.focus();
//		SetFocusSelect(salesForm.f_interested_solutions);
		return false;
	}

	return true;
}
// End Sales Enquiry form validation

// the Request a demo form validation in the ACMS Quick Tour
function checkRequestDemo() {
   var requestDemoForm = document.forms[0];

   if(IsEmpty(requestDemoForm.f_name)) {
		alert("Please enter your name!");
		SetFocusSelect(requestDemoForm.f_name);
		return false;
   }

	if(!validateEmail(requestDemoForm.f_email.value)) {
		alert("Please enter a valid email address.");
		requestDemoForm.f_email.select();
		return false;
	}

   if(IsEmpty(requestDemoForm.f_tel)) {
       alert("Please enter Telephone Number.");
		SetFocusSelect(requestDemoForm.f_tel);
		return false;
   }

	return true;

}
// End Request a Demo form validation.

// Client Testimonial form validation
function  checkTestimonialForm() {
   var testimonialForm = document.forms[0];

   if(IsEmpty(testimonialForm.f_name)) {
       alert("Please enter your name!");
		SetFocusSelect(testimonialForm.f_name);
		return false;
   }

   if(IsEmpty(testimonialForm.f_position)) {
       alert("Please enter your Position!");
		SetFocusSelect(testimonialForm.f_position);
		return false;
   }

   if(IsEmpty(testimonialForm.f_company)) {
       alert("Please enter your Company Name.!");
		SetFocusSelect(testimonialForm.f_company);
		return false;
   }

   if(IsEmpty(testimonialForm.f_telephone)) {
       alert("Please enter your Phone No.!");
		SetFocusSelect(testimonialForm.f_telephone);
		return false;
   }
   
	if(!validateEmail(testimonialForm.f_email.value)) {
		alert("Please enter a valid email address.");
		testimonialForm.f_email.select();
		return false;
	}

   if(IsEmpty(testimonialForm.f_testimonial)) {
       alert("Please Put your Testimonial!");
		SetFocusSelect(testimonialForm.f_testimonial);
		return false;
   }
   return true;
}
// end Client Testimonial form validation

// Approval form validation
function  checkApprovalForm() {
   var approvalForm = document.forms[0];

   if(IsEmpty(approvalForm.f_name)) {
       alert("Please enter your name!");
		SetFocusSelect(approvalForm.f_name);
		return false;
   }

   if(IsEmpty(approvalForm.f_position)) {
       alert("Please enter your Position!");
		SetFocusSelect(approvalForm.f_position);
		return false;
   }

	if(!validateEmail(approvalForm.f_email.value)) {
		alert("Please enter a valid email address.");
		approvalForm.f_email.select();
		return false;
	}

   if(IsEmpty(approvalForm.f_state)) {
       alert("Please enter State!");
		SetFocusSelect(approvalForm.f_state);
		return false;
   }

   if(IsEmpty(approvalForm.f_country)) {
       alert("Please enter Country!");
		SetFocusSelect(approvalForm.f_country);
		return false;
   }

   if (!checkCheckBox( 'approvalForm', 'f_approvaltype[]', '' )) {
	   alert('Check one of the Approval Types!');
	   return false;
   }
   if (approvalForm.f_accept.checked == false){
	   alert('You have to Accept the Authority!');
	   return false;

   }
   return true;
}

// end Client Approval form validation



// FOLLOWING ARE THE FUNCTIONS USED FOR VALIDATING ABOVE WRITTEN FUNCTIONS

// E-mail Validation
function validateEmail(pEmail) {
   var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
   var returnval=emailfilter.test(pEmail);
   if (returnval==false){
      return false;
   } else {
      return true;
   }
}

// Input Text Validation
function TrimString(szString) {
      var i = 0;
      var j = 0;

      for (i=0; i<parseInt(szString.length); i++) {
            if(szString.charAt(i) != " ") {
                  for (j=parseInt(szString.length) - 1; j > i; j--) {
                        if (szString.charAt(j) != " ") {
                              break;
                        }
                  }
                  break;
            }
      }

      if (i > j)
            i = j;

      if (szString.length > 0 && szString.charAt(j) != " ")
            j++;

      return szString.substring(i, j);
}

// Input Text Validation
function TrimField(szFieldName) {
   var szNewStr="";
   if (szFieldName.value == "")
      return;

   if (szFieldName.value.length > 0) {
      szNewStr = TrimString(szFieldName.value);
      szFieldName.value = szNewStr;
   }
}

// Input Text validation
function IsEmpty(szFieldName) {
   var i;
   var ch;

   if (szFieldName.value == "")	
   		return true;

   TrimField(szFieldName);
   if (parseInt(szFieldName.value.length) == 0)
         return true;

   for (i=0; i<parseInt(szFieldName.value.length); i++) {
      ch = szFieldName.value.charAt(i);
      if (ch != ' ' && ch != '\t')
         return false;
   }
   return true;
}

// To focus and select the text box when it is empty
function SetFocusSelect(szFieldName) {
   szFieldName.focus();
   szFieldName.select();
   return true;
}

function checkCheckBox( curForm, curVar, curMsg ) {
	var elts = document.forms[curForm].elements[curVar];
	var elts_cnt = elts.length;
//	var ifChecked = false;
	if( typeof(elts_cnt) == 'undefined' ) {
		var ifChecked = true;
	}
	for (var i = 0; i < elts_cnt; i++) {
		if( elts[i].checked ) {
			var ifChecked = true;
		}
	}

	if( ifChecked == true ) {
		return true;
	} else {
		return false;
	}
}
