// Script for validating Feed Back Form
function checkQuickQueryForm() { 
   var feedBackFrom = document.forms['quickQueryForm'];

   if(IsEmpty(feedBackFrom.f_name)) {
		alert("الرجاء أدخال الأسم!");
		SetFocusSelect(feedBackFrom.f_name);
		return false;
   }

	if(!validateEmail(feedBackFrom.f_email.value)) {
		alert("الرجاء أدخال البريد الألكتروني");
		feedBackFrom.f_email.select();
		return false;
	}

   if(IsEmpty(feedBackFrom.f_information)) {
		alert(" الرجاء أدخال الأقتراحات!");
		SetFocusSelect(feedBackFrom.f_information);
		return false;
   }

	return true; 
}

// Script for validating Feed Back Form
function checkFeedBackForm() { 
   var feedBackFrom = document.forms['feedBackForm'];

   if(IsEmpty(feedBackFrom.f_name)) {
		alert("Please enter your name!");
		SetFocusSelect(feedBackFrom.f_name);
		return false;
   }

	if(!validateEmail(feedBackFrom.f_email.value)) {
		alert("Please enter a valid email address!");
		feedBackFrom.f_email.select();
		return false;
	}

   if(IsEmpty(feedBackFrom.f_address)) {
		alert("Please fill in your Address!");
		SetFocusSelect(feedBackFrom.f_address);
		return false;
   }

   if(IsEmpty(feedBackFrom.f_enquiry)) {
		alert("Please fill in your Enquiry / Suggession!");
		SetFocusSelect(feedBackFrom.f_enquiry);
		return false;
   }

	return true; 
}

// Script for validating Order Form
function checkOrderForm() { 
   var orderForm = document.forms['orderForm'];

   if(IsEmpty(orderForm.namePub)) {
		alert("Please enter the name of Publication!");
		SetFocusSelect(orderForm.namePub);
		return false;
   }

   if(IsEmpty(orderForm.name)) {
		alert("Please enter your name!");
		SetFocusSelect(orderForm.name);
		return false;
   }

   if(IsEmpty(orderForm.postAdd)) {
		alert("Please enter Postal Address!");
		SetFocusSelect(orderForm.postAdd);
		return false;
   }

   if(IsEmpty(orderForm.tel)) {
		alert("Please enter Telephone!");
		SetFocusSelect(orderForm.tel);
		return false;
   }

	if(!validateEmail(orderForm.email.value)) {
		alert("Please enter a valid email address!");
		orderForm.email.select();
		return false;
	}

	return true; 
}

// Script for validating Call Center Form
function checkCallCenterForm() { 
   var cCForm = document.forms['callCenterForm'];

   if(IsEmpty(cCForm.f_telephone)) {
		alert("Please enter Telephone number!");
		SetFocusSelect(cCForm.f_telephone);
		return false;
   }

   if(IsEmpty(cCForm.f_person_incharge)) {
		alert("Please enter the name of Person-in-charge!");
		SetFocusSelect(cCForm.f_person_incharge);
		return false;
   }

   if(IsEmpty(cCForm.f_title)) {
		alert("Please enter the title!");
		SetFocusSelect(cCForm.f_title);
		return false;
   }

   if(IsEmpty(cCForm.f_company)) {
		alert("Please enter the company name!");
		SetFocusSelect(cCForm.f_company);
		return false;
   }

   if(IsEmpty(cCForm.f_day_specified)) {
		alert("Please enter the day specified!");
		SetFocusSelect(cCForm.f_day_specified);
		return false;
   }

   if(IsEmpty(cCForm.f_time_from)) {
		alert("Please enter the time range!");
		SetFocusSelect(cCForm.f_time_from);
		return false;
   }

   if(IsEmpty(cCForm.f_time_to)) {
		alert("Please enter the time range!");
		SetFocusSelect(cCForm.f_time_to);
		return false;
   }

	return true; 
}


// 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;
   }
}

//  function for checking two field values are correct
function IsCompare(field_1, field_2) {
	
	if(field_1.value != field_2.value) {
		return true;
	}
	return false;
}
// end function

// function to check length of a variable
function checkLength(fieldName, n) {
	if( fieldName.value.length < n ) {
		return true;
	}
	return false;
}

// 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;
}

// To open popup window
function openBrWindow(url,winName,features) {
  if(window.newWinVar) {
     newWinVar.close();
  }
  newWinVar = window.open(url,winName,features);
  newWinVar.focus();
}

// To Disable the submit button when its clicked
function submitonce(theform){
//if IE 4+ or NS 6+
	alert();
	if (document.all||document.getElementById){
	//screen thru every element in the form, and hunt down "submit" and "reset"
	for (i=0;i<theform.length;i++){
		var tempobj=theform.elements[i]
		if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
			//disable em
			tempobj.disabled=true
		}
	}
}

// check a form field for invalid characters
function checkValidChars(fieldName) {
	var strUserName = fieldName.value; // String(document.frmUserName.txtUserName.value);
	var strCurrentChar;
	var bolValidUserName = true;

	for (var iLoop=0; iLoop < strUserName.length; iLoop++) {
		strCurrentChar = strUserName.substring(iLoop,iLoop+1);
		if ( (strCurrentChar >= 'A' && strCurrentChar <= 'Z') || (strCurrentChar >= 'a' && strCurrentChar <= 'z')
			|| (strCurrentChar >= '0' && strCurrentChar <= '9') || strCurrentChar == '-' || strCurrentChar == '_' );
			   // We have a valid string... do nothing.
	    else
			bolValidUserName = false;
	}
	return bolValidUserName;
}

