// JavaScript Document

//Validates input for a new site
function validateNewSite(){
	//alert("Implement validateNewSite();");
	if(document.mainForm.siteCode.value==""){
		return displayAlert("Please enter a site code.", "siteCode");
	}
	if(document.mainForm.managerName.value==""){
		return displayAlert("Please enter a manager name.", "managerName");
	}
	if(document.mainForm.address1.value==""){
		return displayAlert("Please enter the first line of the sites' address.", "address1");
	}
	if(document.mainForm.city.value==""){
		return displayAlert("Please enter the sites' town or city.", "city");
	}
	if(document.mainForm.postcode.value==""){		
		return displayAlert("Please enter the sites' postcode.", "postcode");
	}	
	if(document.mainForm.email.value==""){
		return displayAlert("Please enter a contact email address.", "email");
	}
	if(!echeck(document.mainForm.email.value)){
		return displayAlert("Please enter a valid email address.", "email");
		document.mainForm.email.value="";
	}
	if(document.mainForm.numEmployees.value!=""){
		if(parseInt(document.mainForm.numEmployees.value)!=document.mainForm.numEmployees.value){
			return displayAlert("Please enter a valid whole number for number of employees.", "numEmployees");
		}
	}
	if(document.mainForm.numEmployees.value==""){
		return displayAlert("Please enter the number of employees at the site.", "numEmployees");
	}
	return true;
}


function validateUpdateSiteNum(){
	if(document.siteNumberForm.numSites.value!=""){
		if(parseInt(document.siteNumberForm.numEmployees.value)!=document.siteNumberForm.numEmployees.value){
			return displayAlert("Please enter a valid whole number for number of sites.", "numSites");
		}
	}
	if(document.siteNumberForm.numEmployees.value==""){
		return displayAlert("Please enter the number of sites.", "numSites");
	}
	return true;
}

function validateJobDescriptions(){
	alert("Implement validateJobDescriptions();");
	return true;
}

//Function to check for a valid email address
function echeck(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					
}

//Function to display an alert and focus on the input object which hasn't been entered.
function displayAlert(alertText, inputObject){
	alert(alertText);	
	$('#'+inputObject).focus();	
	return false;
}

//Standard blurb
var standard = "This is the standard blurb.  There " +
 		"would be some general details here about the " +
 		"reason for the survey to explain to the companies " +
 		"employees.  This information would also be " +
 		"editable by the user before their employees see it.";

//Function to set the standard blurb in register.jsp and edit.jsp
function standardBlurb(){	
	if(document.mainForm.standard.checked){
		if(!document.mainForm.blurb.value==""){
			if (confirm("Are you sure? You will loose your custom blurb.")){
				document.mainForm.blurb.value=standard;
			}else{
				document.mainForm.standard.checked=false;
			}
		}else{			
			document.mainForm.blurb.value=standard;
		}
	}else if(!document.mainForm.standard.checked){
		document.mainForm.blurb.value="";
	}	
}

function blurbEdited(){	
	if(document.mainForm.blurb.value!=standard){
		document.mainForm.standard.checked=false;
	}
}

//Function to display or hide sections of the menu
function toggle(div_id){
	var el = typeof div_id == 'string' ? document.getElementById(div_id) : div_id;
	if(el.style.display == "block"){
		hide(div_id);
	}else{
		show(div_id);
	}
}


function show(div_id){
	//alert('Showing '+div_id);
	try
	{
		$("#"+div_id).show("slow");
	}
	catch(e)
	{
		var el = typeof div_id == 'string' ? document.getElementById(div_id) : div_id;
		el.style.display = "block";	
	}	
}

function hide(div_id){	
	//alert('Hiding '+div_id);
	//try
	//{
	//	$("#"+div_id).hide("slow");
	//}
	//catch(e)
	//{
		var el = typeof div_id == 'string' ? document.getElementById(div_id) : div_id;
		el.style.display = "none";
	//}
}

//Function for finding a label associated with an input element
function findLabelFor (elOrId) {
  var el = typeof elOrId == 'string' ? document.getElementById(elOrId) : elOrId;
  var labels = document.getElementsByTagName('label');
  var found = false;
  for (var l = 0; l < labels.length; l++)
    if (found = el.id == labels[l].htmlFor)
      break;
  if (found)
    return labels[l];
  else
    return null;
}

function findLabelTextFor (elOrId) {
  return findLabelFor(elOrId).firstChild.nodeValue;
}

//Function for navigating the grid used in viewsites and viewcompanies and survey summary tables
function doNavigate(pstrWhere, pintTot)
	{
	  var strTmp;
	  var intPg; 
	  strTmp = document.frmMain.txtCurr.value;
	  intPg = parseInt(strTmp);
	  if (isNaN(intPg)) intPg = 1; 
	  if ((pstrWhere == 'F' || pstrWhere == 'P') && intPg == 1)
	  {
		alert("You are already viewing first page.");
		return;
	  }
	  else if ((pstrWhere == 'N' || pstrWhere == 'L') && intPg == pintTot)
	  {
		alert("You are already viewing last page.");
		return;
	  }
	  if (pstrWhere == 'F')
		intPg = 1;
	  else if (pstrWhere == 'P')
		intPg = intPg - 1;
	  else if (pstrWhere == 'N')
		intPg = intPg + 1;
	  else if (pstrWhere == 'L')
		intPg = pintTot; 
	  if (intPg < 1) intPg = 1;
	  if (intPg > pintTot) intPg = pintTot;
	  document.frmMain.txtCurr.value = intPg;
	  document.frmMain.submit();
	}

//Function for sorting the grid used in viewsites and viewcompanies
function doSort(pstrFld, pstrOrd)
{
	  document.frmMain.txtSortCol.value = pstrFld;
	  document.frmMain.txtSortAsc.value = pstrOrd;
	  document.frmMain.submit();
}

function getHidden(name, value)
{
	return '<input type="hidden" id="'+name+'" name="'+name+'" value="'+value+'"/>';
}

