function ValidateLoadDataForm()
{
	if (document.loadDataForm.fileLoad.value.length == 0)
	{
		alert("Please select a file to upload.");
		return false;
	}
	return true;
}

function Trim(str)
{
	
   if(str.length < 1)
   {
     return"";
   }
   str = RTrim(str);
   str = LTrim(str);


   if(str=="")
   {
      return "";
   }


   else
   {
      return str;
   }
}


 function RTrim(str)
 {
      var w_space = String.fromCharCode(32);
      var v_length = str.length;
      var strTemp = "";


     if(v_length < 0)
	 {
        return"";
     }
     var iTemp = v_length -1;

     while(iTemp > -1)
	 {
        if(str.charAt(iTemp) == w_space)
		{
		}
        else
		{
		    strTemp = str.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
     }
     return strTemp;
 }     


function LTrim(str)
{
   var w_space = String.fromCharCode(32);

   if(v_length < 1)
   {
      return"";
   }
   var v_length = str.length;
   var strTemp = "";
   var iTemp = 0;

   while(iTemp < v_length)
   {
      if(str.charAt(iTemp) == w_space)
	  {
	  }
      else
	  {
          strTemp = str.substring(iTemp,v_length);
          break;
      }
      iTemp = iTemp + 1;
   }
   return strTemp;
}


function ValidateHouseholdIdentification()
{
	
	if((Trim(document.tabForm.stayInLocation.value).length < 1)
	    || (isNaN(document.tabForm.stayInLocation.value))) 
	{
			Main.setSelectedIndex(0);
			Identification.setSelectedIndex(0);  
	    HouseholdId.setSelectedIndex(1);		
		document.tabForm.stayInLocation.select();
		document.tabForm.stayInLocation.focus();
		alert("Please enter correct value for Stay In Location");		
		return false;
	}
		
	if((Trim(document.tabForm.spotmapId.value).length < 1)
	   || (isNaN(document.tabForm.spotmapId.value))) 
	{
		Main.setSelectedIndex(0);
		Identification.setSelectedIndex(0);  
	    HouseholdId.setSelectedIndex(1);		
		document.tabForm.spotmapId.select();
		document.tabForm.spotmapId.focus();
		alert("Please enter correct value for Household Id");		
		return false;
	}						
	return true;
}

function ValidateSurveyIdentification() 
{
	
	if(Trim(document.tabForm.dateSurveyed.value).length < 1) 
	{
			Main.setSelectedIndex(0);
	    Identification.setSelectedIndex(1);
		document.tabForm.dateSurveyed.select();
		document.tabForm.dateSurveyed.focus();
		alert("Please enter correct value for Survey Date");
		
		return false;
	}
	if(Trim(document.tabForm.respondentLastName.value).length < 1) 
	{
			Main.setSelectedIndex(0);
	    Identification.setSelectedIndex(1);
		document.tabForm.respondentLastName.select();
		document.tabForm.respondentLastName.focus();
		alert("Please enter correct value for Respondent Last Name");		
		return false;
	}
	if(Trim(document.tabForm.respondentFirstName.value).length < 1) 
	{
			Main.setSelectedIndex(0);
	    Identification.setSelectedIndex(1);
		document.tabForm.respondentFirstName.select();
		document.tabForm.respondentFirstName.focus();
		alert("Please enter correct value for Respondent First Name");
		return false;
	}
	
	if(Trim(document.tabForm.enumeratorLastName.value).length < 1) 
	{
			Main.setSelectedIndex(0);
		  Identification.setSelectedIndex(1);  
		document.tabForm.enumeratorLastName.select();
		document.tabForm.enumeratorLastName.focus();
		alert("Please enter correct value for Enumerator Last Name");		
		return false;
	}
	if(Trim(document.tabForm.enumeratorFirstName.value).length < 1) 
	{
			Main.setSelectedIndex(0);
		Identification.setSelectedIndex(1);  
		document.tabForm.enumeratorFirstName.select();
		document.tabForm.enumeratorFirstName.focus();
		alert("Please enter correct value for Enumerator First Name");	
		return false;
	}
		
			
	return true;
}


function ValidateUsersForm()
{
	for ( count=1 ; count <= document.dataForm.elements["noUsers"].value; count++ ) 
	{	    
		var indexUsername = "usersName_" + count;
		var indexPassword = "usersPassword_" + count;
					
		
		if(Trim(document.dataForm.elements[indexUsername].value).length < 1) 
		{
			document.dataForm.elements[indexUsername].select();
			document.dataForm.elements[indexUsername].focus();
			alert("Please enter correct value for Username");		
			return false;
		}	
		
		if(Trim(document.dataForm.elements[indexPassword].value).length < 1)
		{
			document.dataForm.elements[indexPassword].select();
			document.dataForm.elements[indexPassword].focus();
			alert("Please enter correct value for Password");		
			return false;
		}														
	}
	
	for ( count=1 ; count <= document.dataForm.elements["noUsers"].value; count++ ) 
	{	    
		var indexUsername = "usersName_" + count;
		var userName = document.dataForm.elements[indexUsername].value;
		var userNameCount = 0;
		for ( count1=1 ; count1 <= document.dataForm.elements["noUsers"].value; count1++ ) 
		{
			var indexUsernameCheck = "usersName_" + count1;
			if (document.dataForm.elements[indexUsernameCheck].value == userName)
				++userNameCount;				
		}	  
		if (userNameCount > 1)
		{
			document.dataForm.elements[indexUsername].select();
			document.dataForm.elements[indexUsername].focus();
			alert("A duplicate username exists.");		
			return false;			
		}
		
	}
	
				
	return true;
}

function ValidateLocationTypesForm()
{
	for ( count=0 ; count < document.tabForm.elements["noLocationTypes"].value; count++ ) 
	{	    
		var indexType = "locationType_" + count;
		if(Trim(document.tabForm.elements[indexType].value).length < 1) 
		{
			document.tabForm.elements[indexType].select();
			document.tabForm.elements[indexType].focus();
			alert("Please enter correct value for Location Type");		
			return false;
		}			
	}
	for ( count=0 ; count < document.tabForm.elements["noLocationTypes"].value; count++ ) 
	{	    
		var indexType = "locationType_" + count;
		
		var locationType  = document.tabForm.elements[indexType ].value;
		var locationTypeCount = 0;
		for ( count1=0 ; count1 < document.tabForm.elements["noLocationTypes"].value; count1++ ) 
		{
			var indexTypeCheck = "locationType_" + count1;
			if (document.tabForm.elements[indexTypeCheck].value == locationType)
				++locationTypeCount;				
		}	  
		if (locationTypeCount > 1)
		{
			document.tabForm.elements[indexType].select();
			document.tabForm.elements[indexType].focus();
			alert("A duplicate location type exists.");		
			return false;			
		}
		
	}
	
	return true;
}

function ValidateLocationsForm()
{
	
	for ( count=0 ; count < document.tabForm.elements["noLocations"].value; count++ ) 
	{	    
		var indexName = "locationName_" + count;
		if(Trim(document.tabForm.elements[indexName].value).length < 1) 
		{
			document.tabForm.elements[indexName].select();
			document.tabForm.elements[indexName].focus();
			alert("Please enter correct value for Name");		
			return false;
		}			
	}
	for ( count=0 ; count < document.tabForm.elements["noLocations"].value; count++ ) 
	{	    
		var indexName = "locationName_" + count;
		
		var locationName = document.tabForm.elements[indexName].value;
		var locationNameCount = 0;
		for ( count1=0 ; count1 < document.tabForm.elements["noLocations"].value; count1++ ) 
		{
			var indexLocationCheck = "locationName_" + count1;
			if (document.tabForm.elements[indexLocationCheck].value == locationName)
				++locationNameCount;				
		}	  
		if (locationNameCount > 1)
		{
			document.tabForm.elements[indexName].select();
			document.tabForm.elements[indexName].focus();
			alert("A duplicate location exists.");		
			return false;			
		}
		
	}
	return true;
}


function ValidateLocationGroupsForm()
{
	for ( count=1 ; count <= document.dataForm.elements["noLocationGroups"].value; count++ ) 
	{	    
		var indexLocationGroupName = "locationGroupsName_" + count;
		
		
		if(Trim(document.dataForm.elements[indexLocationGroupName].value).length < 1) 
		{
			document.dataForm.elements[indexLocationGroupName].select();
			document.dataForm.elements[indexLocationGroupName].focus();
			alert("Please enter correct value for Location Group Name");		
			return false;
		}	
		
		
	}
	
	for ( count=1 ; count <= document.dataForm.elements["noLocationGroups"].value; count++ ) 
	{	    
		var indexLocationGroupName = "locationGroupsName_" + count;
		var locationGroupName = document.dataForm.elements[indexLocationGroupName].value;
		var locationGroupNameCount = 0;
		for ( count1=1 ; count1 <= document.dataForm.elements["noLocationGroups"].value; count1++ ) 
		{
			var indexLocationGroupNameCheck = "locationGroupsName_" + count1;
			if (document.dataForm.elements[indexLocationGroupNameCheck].value == locationGroupName)
				++locationGroupNameCount;				
		}	  
		if (locationGroupNameCount > 1)
		{
			document.dataForm.elements[indexLocationGroupName].select();
			document.dataForm.elements[indexLocationGroupName].focus();
			alert("A duplicate Location Group name exists.");		
			return false;			
		}
		
	}
	
				
	return true;
}

function ValidateHouseholdGroupsForm()
{
	for ( count=1 ; count <= document.dataForm.elements["noHouseholdGroups"].value; count++ ) 
	{	    
		var indexHouseholdGroupName = "householdGroupsName_" + count;
		
		
		if(Trim(document.dataForm.elements[indexHouseholdGroupName].value).length < 1) 
		{
			document.dataForm.elements[indexHouseholdGroupName].select();
			document.dataForm.elements[indexHouseholdGroupName].focus();
			alert("Please enter correct value for Household Group Name");		
			return false;
		}	
		
		
	}
	
	for ( count=1 ; count <= document.dataForm.elements["noHouseholdGroups"].value; count++ ) 
	{	    
		var indexHouseholdGroupName = "householdGroupsName_" + count;
		var householdGroupName = document.dataForm.elements[indexHouseholdGroupName].value;
		var householdGroupNameCount = 0;
		for ( count1=1 ; count1 <= document.dataForm.elements["noHouseholdGroups"].value; count1++ ) 
		{
			var indexHouseholdGroupNameCheck = "householdGroupsName_" + count1;
			if (document.dataForm.elements[indexHouseholdGroupNameCheck].value == householdGroupName)
				++householdGroupNameCount;				
		}	  
		if (householdGroupNameCount > 1)
		{
			document.dataForm.elements[indexHouseholdGroupName].select();
			document.dataForm.elements[indexHouseholdGroupName].focus();
			alert("A duplicate Household Group name exists.");		
			return false;			
		}
		
	}
	
				
	return true;
}

function ValidatePolygonCoordsForm()
{
	for ( count=1 ; count <= document.tabForm.elements["noPolygonCoords"].value; count++ ) 
	{	    
		var index = "polygonXCoords_" + count;
		
		if(Trim(document.tabForm.elements[index].value).length < 1 ||
			  (isNaN(document.tabForm.elements[index].value))) 
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Coordinate");		
			return false;
		}	
		var index = "polygonYCoords_" + count;
		
		if(Trim(document.tabForm.elements[index].value).length < 1 ||
			 (isNaN(document.tabForm.elements[index].value))) 
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Coordinate");		
			return false;
		}									
	}
	return true;		
}

function ValidateLabelCoordsForm()
{
		    
	var index = "nameXCoord";
		
	if(Trim(document.tabForm.elements[index].value).length < 1 ||
		  (isNaN(document.tabForm.elements[index].value))) 
	{
		document.tabForm.elements[index].select();
		document.tabForm.elements[index].focus();
		alert("Please enter correct value for Coordinate");		
		return false;
	}	
	var index = "nameYCoord";
		
	if(Trim(document.tabForm.elements[index].value).length < 1 ||
		  (isNaN(document.tabForm.elements[index].value))) 
	{
		document.tabForm.elements[index].select();
		document.tabForm.elements[index].focus();
		alert("Please enter correct value for Coordinate");		
		return false;
	}
	var index = "valueXCoord";
		
	if(Trim(document.tabForm.elements[index].value).length < 1 ||
		  (isNaN(document.tabForm.elements[index].value))) 
	{
		document.tabForm.elements[index].select();
		document.tabForm.elements[index].focus();
		alert("Please enter correct value for Coordinate");		
		return false;
	}	
	var index = "valueYCoord";
		
	if(Trim(document.tabForm.elements[index].value).length < 1 ||
		  (isNaN(document.tabForm.elements[index].value))) 
	{
		document.tabForm.elements[index].select();
		document.tabForm.elements[index].focus();
		alert("Please enter correct value for Coordinate");		
		return false;
	}	

	return true;
}

function ValidateThemesForm()
{
	for ( count=1 ; count <= document.tabForm.elements["noThemes"].value; count++ ) 
	{	    
		var index = "themesDescription_" + count;
		
		if(Trim(document.tabForm.elements[index].value).length < 1 ) 
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Description");		
			return false;
		}	
		var index = "themesColor_" + count;
		
		if(Trim(document.tabForm.elements[index].value).length < 6 ) 
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Color");		
			return false;
		}									
	}
	return true;		
}

function ValidateHouseholdCoordsForm()
{
	var xCoord;
	var yCoord;
	var index;
	
	for ( count=1 ; count <= document.tabForm.elements["noHouseholdCoords"].value; count++ ) 
	{	   
		xCoord = false;
		index = "householdXCoords_" + count;				
		if(Trim(document.tabForm.elements[index].value).length > 0)
		{
			xCoord = true;			
			 if (isNaN(document.tabForm.elements[index].value)) 
			{
				document.tabForm.elements[index].select();
				document.tabForm.elements[index].focus();
				alert("Please enter correct value for Coordinate");		
				return false;
			}
		}
		yCoord = false;
		index = "householdYCoords_" + count;
		if(Trim(document.tabForm.elements[index].value).length > 0)
		{
			yCoord = true;
			 if (isNaN(document.tabForm.elements[index].value)) 
			{
				document.tabForm.elements[index].select();
				document.tabForm.elements[index].focus();
				alert("Please enter correct value for Coordinate");		
				return false;
			}
		}
		
		if (xCoord && !yCoord)
		{
			index = "householdYCoords_" + count;
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Coordinate");		
			return false;
		}
		if (!xCoord && yCoord)
		{
			index = "householdXCoords_" + count;
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Coordinate");		
			return false;			
		}
	}
	return true;		
}

function ValidateUserDefinedIndicatorsForm()
{
	var index;
	
	for ( count=1 ; count <= document.dataForm.elements["noIndicators"].value; count++ ) 
	{	   
		index = "indicatorsName_" + count;				
		if(Trim(document.dataForm.elements[index].value).length < 1)
		{
			document.dataForm.elements[index].select();
			document.dataForm.elements[index].focus();
			alert("Please enter correct value for Name");		
			return false;
			
		}

		index = "indicatorsTitle_" + count;				
		if(Trim(document.dataForm.elements[index].value).length < 1)
		{
			document.dataForm.elements[index].select();
			document.dataForm.elements[index].focus();
			alert("Please enter correct value for Title");		
			return false;
			
		}

		index = "indicatorsShortTitle_" + count;				
		if(Trim(document.dataForm.elements[index].value).length < 1)
		{
			document.dataForm.elements[index].select();
			document.dataForm.elements[index].focus();
			alert("Please enter correct value for Short Title");		
			return false;
			
		}


	}
	return true;		

}

function ValidateUserDefinedIndicatorDataValuesForm(percentageType)
{
	var index;	
	for ( count=1 ; count <= document.tabForm.elements["noDataValues"].value; count++ ) 
	{	   
		index = "dataValuesYear_" + count;		
		if((Trim(document.tabForm.elements[index].value).length < 4) ||
				isNaN(document.tabForm.elements[index].value)   )
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Year");		
			return false;			
		}
		index = "dataValues_" + count;				
		if((Trim(document.tabForm.elements[index].value).length < 1) ||
				isNaN(document.tabForm.elements[index].value)   )
		{
			document.tabForm.elements[index].select();
			document.tabForm.elements[index].focus();
			alert("Please enter correct value for Value");		
			return false;			
		}
		
		if (percentageType)
		{
			if((document.tabForm.elements[index].value > 100 ) ||
				(document.tabForm.elements[index].value < 0) )
			{
				document.tabForm.elements[index].select();
				document.tabForm.elements[index].focus();
				alert("Please enter correct value for Value");		
				return false;			
			}
		}
		
	}
	return true;		
}

function validateDevInfoInterfaceForm()
{
	if ( document.tabForm.DevInfo_sector.value.length == 0 )
	{
		alert( "Invalid value for Sector." );
		document.tabForm.DevInfo_sector.focus();
		return false;
	}
	
	if( document.tabForm.DevInfo_class.value.length == 0 )
	{
		alert( "Invalid value for Class.");
		document.tabForm.DevInfo_class.focus();
		return false;
	}
	if( document.tabForm.DevInfo_indicator.value.length == 0)
	{
		alert( "Invalid value for Indicator.");
		document.tabForm.DevInfo_indicator.focus();
		return false;
	}

	return true;

}


function ValidateProjectsForm()
{
	for ( count=1 ; count <= document.dataForm.elements["noProjects"].value; count++ ) 
	{	    
		var indexProjectName = "projectsName_" + count;
		
		
		if(Trim(document.dataForm.elements[indexProjectName].value).length < 1) 
		{
			document.dataForm.elements[indexProjectName].select();
			document.dataForm.elements[indexProjectName].focus();
			alert("Please enter correct value for Project Name");		
			return false;
		}	
		
		
	}
	
	for ( count=1 ; count <= document.dataForm.elements["noProjects"].value; count++ ) 
	{	    
		var indexProjectName = "projectsName_" + count;
		var projectName = document.dataForm.elements[indexProjectName].value;
		var projectNameCount = 0;
		for ( count1=1 ; count1 <= document.dataForm.elements["noProjects"].value; count1++ ) 
		{
			var indexProjectNameCheck = "projectsName_" + count1;
			if (document.dataForm.elements[indexProjectNameCheck].value == projectName)
				++projectNameCount;				
		}	  
		if (projectNameCount > 1)
		{
			document.dataForm.elements[indexProjectName].select();
			document.dataForm.elements[indexProjectName].focus();
			alert("A duplicate Project name exists.");		
			return false;			
		}
		
	}					
	return true;
}







