function trim(sStr)
{
   var s;
   sStr = sStr.toString();
   sStr = sStr.replace(/(^\s*)|(\s*$)/g,"");
   //sStr = sStr.replace(/\s{2,}/g," "); /*----- Removes the unwanted spaces(more than one)-----*/
   return(sStr);
}/*---- Regular expression functions for clearing the spaces ----*/

function validate(obj)
{
	obj1 = trim(obj.value)
	if(obj1 == "")
	{		
		alert("Search Text is Empty");
		obj.focus();
		return false;
	}
	else
	return true;
}/*----- Validate Search Text is empty or not-----*/

function clear_spaces(formName)
{
	var element_all =  formName.elements;
	var i;
	for(i=0;i<element_all.length;++i)
	{
		if (element_all[i].type == "text")
			element_all[i].value = trim(element_all[i].value);
			
	}
}/*----- Trim all the text boxes Only-----*/

function checkText(obj)
{
/*-----The trim function has to be called before calling this function -----*/
/*---- Later , type will be passed as a parameter so that the type will be like email, phone no , numeric, character ---*/
if(obj.value == "")
	{
		return false;
	}
else
	return true;
}/*---- Checks the text box for empty string -----*/

function checkConfPassword(obj1,obj2)
{
	
/*-----The trim function has to be called before calling this function -----*/

if(obj1.value != "" && obj2.value != "")
	{
		if(trim(obj1.value) != trim(obj2.value))
		return false;
		else
		return true;
	}
else
	return false;
}/*---- Checks the cofirm password and password are same -----*/

function checkCombo(obj)
{
/*-----The trim function has to be called before calling this function -----*/
/*---- Later , type will be passed as a parameter so that the type will be like email, phone no , numeric, character ---*/
if(obj.value == "0")
	{
//		alert("Please enter the "+name);
		//obj.focus();
		return false;
	}
else
	return true;
}/*---- Checks the text box for empty string -----*/


function checkEqual(string1,string2)
{
	var retVal = (string1 === string2) ? true : false;
	return retVal;
}/*-----checkEqual()----*/

function checkSelected(obj)
{
	/*if(obj.options[obj.selectedIndex].value == 0 )
		return false;
	else
	return true;*/
//return (obj.options[obj.selectedIndex].value == 0 ) ? false : true;
//alert(obj.selectedIndex);
return (obj.selectedIndex == -1 ) ? false : true;
}/*----checkSelected()-----*/

function checkSelectedVideo(obj)
{
	/*if(obj.options[obj.selectedIndex].value == 0 )
		return false;
	else
	return true;*/
//return (obj.options[obj.selectedIndex].value == 0 ) ? false : true;
//alert(obj.selectedIndex);
return (obj.selectedIndex == 0 ) ? false : true;
}/*----checkSelected()-----*/


function checkMSelected(obj)
{
var i = 0;
for( i =0 ;i< obj.length; i++)
	{
	 if ((obj[i].selected) || (obj[i].checked)) 
		 return true;
	}

return false;
}/*----checkSelected()-----*/


 function telephoneCheck(telephoneNum)
 {
 	var validCharRegExp = /^\+?[\d\- ]+$/;
	var isValid = (validCharRegExp.test(telephoneNum));
	
	return isValid;
 }
function PostalCode(postalCode)
  {
  	var validCharRegExp = /^\d[\d\- ]+$/; 
	var isValid = (validCharRegExp.test(postalCode));
	
	return isValid;
  }
 
 function OnlyAlphabetic(string)
 {
 //	if(string == "" ) return false;
 	var invalidCharRegExp = /[^a-z ]/i;
	var isValid = !(invalidCharRegExp.test(string));
	
	return isValid;
 }
 //To validate Numbers only .................................
function isOnlyNumeric(string)
 {
 	if(string == "" ) return false;
 	var invalidCharRegExp = /[^\d]/;
	var isValid = !(invalidCharRegExp.test(string));	
	return isValid;
 }
 
function emailCheck(email)
  {
	 // alert(email);
	var validCharRegExp = /^\w(\.?[-\w])*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i;
//var validCharRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.([a-z]{3}(\.[a-z]{2})?|[a-z]{2}(\.[a-z]{2})?)$/i;
	

	var isValid = (validCharRegExp.test(email));
	
	return isValid;
}/*----- Email-----*/

  function openCalendar(element)
  {

  window.open( 'popupCalendar.php?e='+element, 'Calendar', 'top=250,left=250,width=272, height=292' );
  }/*---- openCalendar()-----*/
   
  /*function opencolorwheel(element) 
  {
  window.open('colorwheel.jpg?e='+element, 'select color', 'top=250,left=250,width=260, height=220, scrollbars=false' );
  }*/

function checkDate(fromDate, toDate) 
{
	var from = fromDate.value;
	var to = toDate.value;
	if(from != "" && to != "")
	{
		from = from.substr(8,2)+"-"+from.substr(5,2)+"-"+from.substr(0,4);
		to = to.substr(8,2)+"-"+to.substr(5,2)+"-"+to.substr(0,4);
	//	alert(" from date "+ from +"\n To date :"+to);
		if (Date.parse(from) <= Date.parse(to )) 
		{
			 return true;
		}
	
	}
	else
		return true;// here if the from date or to date is empty ... no error message should be thrown
	return false;
}/*----CheckDate()-----*/

function cancel()
{
window.history.back(-1);
}/*---Cancel()---*/

function validateSearch()
{
	var frm = document.searchForm;
 	if(frm.txtSearch.value=="")
	{
		alert("Enter the Search text");
		return false;
	}else {
		frm.submit();
	}
 }
 function validate()
{
 	var frm = document.loginfrm;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
 	error[0] = checkText(frm.username) ?  "" : "Username is empty" //Username is empty
	error[1] = isEmpty(frm.password) ?  "Password is empty" : "" ; //Password is empty

	for(i= 0 ;i<error.length; ++i)
	errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
		
	if(errorMessage == "")
	{
		frm.submit();
	}
	else
	{
		alert(errorMessage);
		return false;
	}
}
function dispaly()
{
	 window.open('forgotPassword.php',"mywindow","menubar=0,resizable=0,width=430,height=320,scrollbars=0");
}
function register() {
	window.location.href = "register.php";
}
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
function addValidate()
{
	frm = document.broker;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
	error[0] = checkText(frm.firstname) ?  "" : "First Name is empty" ;//"Comments Empty";
	error[1] = checkText(frm.lastname) ?  "" : "Last Name is empty" ;
	error[3] = checkText(frm.email) ?  "" : "Email is empty" ;
	error[6] = checkText(frm.address) ?  "" : "Address is empty" ;
	error[7] = checkText(frm.contactno) ?  "" : "Phone is empty" ;
	error[8] = checkText(frm.mobile) ?  "" : "Mobile is empty" ;
	error[9] = checkText(frm.fax) ?  "" : "Fax is empty" ;
	if(frm.broker_photo.value!='') {
		error[12] = imgCheck(frm.broker_photo.value) ? "" : "Select the file of type gif or jpg or png or jpeg";
	}
	
	if(error[3] == "")
	{
		error[3] = emailCheck(frm.email.value) ? "" : "Email is not valid";	
	}
	
				
	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == "")
	{
		frm.submit();
	}
	else
	{
		alert(errorMessage);
		return false;
	}
}
function updateValidate()
{
	frm = document.broker;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
	error[0] = checkText(frm.firstname) ?  "" : "First Name is empty" ;//"Comments Empty";
	error[1] = checkText(frm.lastname) ?  "" : "Last Name is empty" ;
	error[2] = checkText(frm.email) ?  "" : "Email is empty" ;
 
	error[3] = checkText(frm.address) ?  "" : "Address is empty" ;
	error[4] = checkText(frm.contactno) ?  "" : "Phone is empty" ;
	
	if(frm.broker_photo.value!='') {
		error[5] = imgCheck(frm.broker_photo.value) ? "" : "Select the file of type gif or jpg or png or jpeg";
	}
	if(error[2] == "")
	{
	error[2] = emailCheck(frm.email.value) ? "" : "Email is not valid";	
	}
	 
				
	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == "")
	{
		frm.submit();
	}
	else
	{
		alert(errorMessage);
		return false;
	}
}
function ValidateMyDetails()
{
	frm = document.broker;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
	error[1] = checkText(frm.company_name) ?  "" : "Company name is empty" ;
	error[2] = checkText(frm.email) ?  "" : "Email is empty" ;
	error[3] = checkText(frm.address) ?  "" : "Address is empty" ;
	error[4] = checkText(frm.contactno) ?  "" : "Phone is empty" ;
	error[5] = checkText(frm.fax) ?  "" : "Fax is empty" ;
	error[6] = checkText(frm.DX) ?  "" : "DX is empty" ;
	if(frm.varCustom1.value==1){
		error[7] = checkText(frm.custom1) ?  "" :  frm.msg1.value+" is Empty" ;
	}
	if(frm.varCustom2.value==2){
		error[8] = checkText(frm.custom2) ?  "" : frm.msg2.value+" is Empty" ;
	}
	//if(frm.broker_photo.value!='') {
		//error[5] = imgCheck(frm.broker_photo.value) ? "" : "Select the file of type gif or jpg or png or jpeg";
	//}
	if(error[2] == "")
	{
	error[2] = emailCheck(frm.email.value) ? "" : "Email is not valid";	
	}
	 
				
	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == "")
	{
		frm.submit();
	}
	else
	{
		alert(errorMessage);
		return false;
	}
}
function imgCheck(imageFile){
	frm = document.broker;
	var actionurl;
	var chkThmFileType = ['JPG','GIF','PNG','JPEG'];	
	filePath = imageFile;
	fileLen = filePath.length;
	slashPos = filePath.lastIndexOf("\\");
	fileName = filePath.substring(slashPos+1,fileLen);
	fileNameLen = fileName.length;
	fileDotExt = fileName.lastIndexOf(".");
	fileExt = fileName.substring(fileDotExt+1,fileNameLen);
	fileExtchg = fileExt.toUpperCase();
	if(filePath != null && filePath != "" ) {
		if(chkThmFileType.in_array(fileExtchg)) {
			return true;
		}else{
			return false;
		}
	}				 			
}

function addFedValidate()
{

	frm = document.feedback;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
	
	error[0] = checkText(frm.name) ?  "" : "Name is empty" ;
	error[1] = isOnlyNumeric(frm.contact_no.value) ?  "" : "Invalid Contact No" ;
	error[3] = checkText(frm.message) ?  "" : "Message is empty" ;	

	if(error[1] != "" && frm.contact_no.value == ""){
		error[1] = checkText(frm.email) ?  "" : "Contact No or Email is empty" ;
		
		if((error[1] == "")){
			error[2] = emailCheck(frm.email.value) ?  "" : "Invalid Email Id" ;
		}
	}
	if(document.getElementById("email").value != ""){
		error[2] = emailCheck(frm.email.value) ?  "" : "Invalid Email Id" ;
	}
	
		
	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == ""){
		frm.submit();
	}else{
		alert(errorMessage);
		return false;
	}
}
function validComments(){
	frm = document.broker;
	var error = new Array();
	var errorMessage = "";

	error[0] = checkText(frm.comments) ?  "" : "Comments is empty";

	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == ""){
		frm.submit();
	}else{
		alert(errorMessage);
		return false;
	}
		
}

function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27)  || (key==43) || (key==45))
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}



Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
function addRegValidate()
{
	frm = document.Register;
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);
	error[0] = checkText(frm.firstname) ?  "" : "First Name is empty" ;//"Comments Empty";
	error[1] = checkText(frm.lastname) ?  "" : "Last Name is empty" ;
	error[2] = checkText(frm.email) ?  "" : "Email is empty" ;
	error[3] = checkText(frm.username) ?  "" : "Username is empty" ;
	error[4] = checkText(frm.pwd) ?  "" : "Password is empty" ;
 	error[5] = ((!checkText(frm.cpwd)) && error[3] == "") ? "Confirm password is empty" : "";
	error[6] = checkText(frm.address) ?  "" : "Address is empty" ;
	error[7] = checkText(frm.contactno) ?  "" : "Phone is empty" ;
	error[8] = checkText(frm.broker_photo) ?  "" : "Please Upload Photo" ;
	if(error[8]=="") {
		error[8] = imgCheck1(frm.broker_photo.value) ? "" : "Select the file of type gif or jpg or png or jpeg";
	}
	if(error[2] == "")
	{
		error[2] = emailCheck(frm.email.value) ? "" : "Email is not valid";	
	}
	if(error[3]=="") {
		error[3] = (frm.username.value.length<4) ? "Username must be atleast four character" : "";
	}
	if(error[4]=="") {
		error[4] = (frm.pwd.value.length<=4) ? "Password must be atleast five character" : "";
	}
	if(error[4] == "" && error[5] == "")
	{
		error[5] =	(frm.pwd.value != frm.cpwd.value) ? "Password and confirm password must be same" : "";
	}
	
				
	for(i= 0 ;i<error.length; ++i){
		if(error[i]!=undefined)
			errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
	}
	
	if(errorMessage == "")
	{
		frm.submit();
	}
	else
	{
		alert(errorMessage);
		return false;
	}
}
function imgCheck1(imageFile){
	frm = document.Register;
	var actionurl;
	var chkThmFileType = ['JPG','GIF','PNG','JPEG'];	
	filePath = imageFile;
	fileLen = filePath.length;
	slashPos = filePath.lastIndexOf("\\");
	fileName = filePath.substring(slashPos+1,fileLen);
	fileNameLen = fileName.length;
	fileDotExt = fileName.lastIndexOf(".");
	fileExt = fileName.substring(fileDotExt+1,fileNameLen);
	fileExtchg = fileExt.toUpperCase();
	if(filePath != null && filePath != "" ) {
		if(chkThmFileType.in_array(fileExtchg)) {
			return true;
		}else{
			return false;
		}
	}				 			
}
function validate_forget()
{
	frm = document.forgorPass;
	
	var error = new Array();
	var errorMessage = "";
	clear_spaces(frm);

	error[0] = isEmpty(frm.user_name) ? "Username is empty" : ""; //Username is empty
	error[1] = isEmpty(frm.email) ? "Email is empty" : ""; //Password is empty
	
	if(frm.email.value != ""){
		error[1] = emailCheck(frm.email.value) ? "" : "Invalid Email" ; //Password is empty
	}
  
	for(i= 0 ;i<error.length; ++i)
	errorMessage+= error[i] != "" ? " * " +error[i]+"\n" : "";
		
	if(errorMessage == "")
	{
		frm.submit();
	}else{
		alert(errorMessage);
		return false;
	}
}

