// JavaScript Document

function checkDateFields(year,month,day,element,dateYear,dateMonth,dateDay,name)
{
 errorCount=0;
	  
 day = parseInt(day);
	 
 if (year < dateYear)
   errorCount++;
 else if (year == dateYear)
 {
  if (month < dateMonth)
   errorCount++;
  else if (month == dateMonth && (day < dateDay))
   errorCount++;
  }
	  
  if (errorCount > 0)
  {
   errorMsg=errorMsg+"An invalid date range has been selected for the "+name+"!\n";
   document.getElementById(element).style.color = 'red';
   }
	else
	 document.getElementById(element).style.color = '#666666';
}

function checkTwoDates(startYear, finishYear, startMonth, finishMonth, startDay, finishDay, startElement, finishElement, startFieldName, finishFieldName)
{
 errorCount = 0;
 
  startDay = parseInt(startDay);
  finishDay = parseInt(finishDay);
 
  if (finishYear < startYear)
  {
   errorMsg = errorMsg+"The year selected for the "+finishFieldName+" predates the year selected for the "+startFieldName+"!\n";
   document.getElementById(startElement).style.color="red";
   document.getElementById(finishElement).style.color="red";
  }
  else
  {
   document.getElementById(startElement).style.color="#666666";
   document.getElementById(finishElement).style.color="#666666";
  }
 
 if (startYear == finishYear)
 {
   if (finishMonth < startMonth)
   {
    errorMsg = errorMsg+"The month selected for the "+finishFieldName+" predates the month selected for the "+startFieldName+"!\n";
    document.getElementById(startElement).style.color="red";
   document.getElementById(finishElement).style.color="red";
   }
   else
   {
    document.getElementById(startElement).style.color="#666666";
    document.getElementById(finishElement).style.color="#666666";
   }
   
   if (finishMonth == startMonth)
   {
    if (finishDay < startDay)
     errorMsg = errorMsg+"The day selected for the "+finishFieldName+" predates the day selected for the "+startFieldName+"!\n";
     document.getElementById(startElement).style.color="red";
     document.getElementById(finishElement).style.color="red";
   }
   else
   {
    document.getElementById(startElement).style.color="#666666";
    document.getElementById(finishElement).style.color="#666666";
   }
 
  }
}

function checkDaySelected(daySelected,daysInMonth,element)
{
 
 if (daySelected > daysInMonth)
 {
  errorMsg = errorMsg+"There are only "+daysInMonth+" days in the month you have selected!\n";
  document.getElementById(element).style.color="red";
 }
 else
  document.getElementById(element).style.color="#666666";
}

function validateField(fieldValue,msg,element)
{
 //alert(fieldValue);
 if(fieldValue==null || fieldValue=="")
 {
  errorMsg=errorMsg+msg+"\n";
  document.getElementById(element).style.color = 'red';
 }
 else
  document.getElementById(element).style.color = '#666666';
}

function validateEmail(fieldValue,element)
{
 if (fieldValue.indexOf('@')==-1 || fieldValue.indexOf('.')==-1)
 {
  errorMsg=errorMsg+"Please enter a valid e-mail address\nAn e-mail address must include both the '@' symbol and a '.'\n";
  document.getElementById(element).style.color = 'red';
 }
 else
  document.getElementById(element).style.color = '#666666';
}

function charLimit(number, limit, field, element)
{
 //alert(number);
//alert(limit);
 charLeft = limit-number;
  //
if (number > limit)
{
 alert("You have exceeded the limit of "+limit+" characters!");
 document.getElementById(field).value=document.getElementById(field).value.substring(0,limit);
}
else
 document.getElementById(element).innerHTML="You have "+charLeft+" characters remaining. Maximum no of characters: "+limit;
//
}

function validateTelephone(fieldValue,fieldName,element)
{
 if ((fieldValue.length > 0)&&(isNaN(fieldValue)==true))
 {
  errorMsg=errorMsg+"Only digits can be entered in the "+fieldName+" field!\n";
  document.getElementById(element).style.color = 'red';
 }
 else{
  document.getElementById(element).style.color = '#666666';
 }
}

function checkPasswords(firstId, secondId, elementOne, elementTwo)
{
 var count=0;

 if (document.getElementById(firstId).value.length > 0)
  count++;
  
 if (document.getElementById(secondId).value.length > 0)
  count++;
  
  if (count == 1)
  {
   errorMsg=errorMsg+"You must enter the new password, in both the new password and the confirm new password fields!\n";
    document.getElementById(elementOne).style.color = 'red';
	document.getElementById(elementTwo).style.color = 'red';
 }
 else{
  document.getElementById(elementOne).style.color = '#666666';
 document.getElementById(elementTwo).style.color = '#666666';
 }
 
 if ((count==2)&&(document.getElementById(firstId).value!=document.getElementById(secondId).value))
 {
   errorMsg=errorMsg+"The values entered, in both the new password and confirm new password fields do not match!\n";
    document.getElementById(elementOne).style.color = 'red';
	document.getElementById(elementTwo).style.color = 'red';
 }
 else{
  document.getElementById(elementOne).style.color = '#666666';
 document.getElementById(elementTwo).style.color = '#666666';
 }																				  

 

  
}
