// function for validating all the fields
//var arrRequired = new Array("heading","cost");
function validate(form)
{
//check if the array is defined or not
if(arrRequiredFields==null)
	{
	alert("array is empty or not defined");
	var arrRequired = new Array();
	}
else
	{
	//this array will be defined in each and every page
	var arrRequired = arrRequiredFields;
	}
var okay;
var strMessage="The following field(s) are missing !\n\n";
var elements=form.elements;
for(var i=0;i<elements.length;i++)
	{
	for(var j=0;j<arrRequired.length;j++)
		{
		if(elements[i].value=="" && elements[i].name==arrRequired[j])
			{
			okay="false";
			strMessage=strMessage + elements[i].name+"\n";
			}
		}
	}

if(okay=="false")
	{
	alert(strMessage);
	return false;
	}
}
//*************************************************************************************************8
function validateAll(form)
{
//check if the array is defined or not
if(arrRequiredFields==null)
	{
	alert("array is empty or not defined");
	var arrRequired = new Array();
	}
else
	{
	//this array will be defined in each and every page
	var arrRequired = arrRequiredFields;
	}
var first;
var okay;
var strMessage="The following field(s) are missing !\n\n";
var elements=form.elements;
for(var i=0;i<elements.length;i++)
	{
	for(var j=0;j<arrRequired.length;j++)
		{
		if(elements[i].name==arrRequired[j])
			{
			var strName=elements[i].name;
			var prefix=strName.substring(0,3);
			var strCaption=strName.replace(prefix,"");
			//alert(preText+"   "+strCaption);
			switch(prefix)
				{
				case "txt":
					if(elements[i].value=="")
						{
						okay="false";
						strMessage=strMessage + strCaption+"\n";
							if(first==null)
							{
								elements[i].focus();
								first = 1;
							}
						}
				break;
				
				case "num":
					if(elements[i].value=="")
						{
						okay="false";
						strMessage=strMessage + "you must enter "+strCaption+"\n";
						}
					if(isNaN(elements[i].value)==true)
						{
						okay="false";
						strMessage=strMessage + "you must enter number for "+strCaption+"\n";
						}
				break;
				
				case "sel":
					if(elements[i].value==0)
						{
						okay="false";
						strMessage=strMessage + "you must select "+strCaption+"\n";
						}
				break;
				
				case "ema":
					if(elements[i].value=="" || !emailCheck(elements[i].value))
						{
						okay="false";
						strMessage=strMessage + "you must enter valid "+strCaption+" eg. info@nepaltravellers.com\n";
						}
				break;
				}			
					
			}
		}
	}

if(okay=="false")
	{
	alert(strMessage);
	return false;
	}
}
//**********************************************************************************************************

function emailCheck(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					
	}
	
