function GetCheckedCount(oForm, sCheck)
{
  // number of elements in the checkbox collection that are checked
  var iCheckCount=0;
  for (var i=0; i<oForm.elements.length; i++)
  {
    if (oForm.elements[i].name==sCheck && oForm.elements[i].checked)
    {
      iCheckCount++;
    }
  }
  return iCheckCount;
}

function radioSelected(oOptions)
{
  //return true if one of the ContentID options is selected
  return (!(getRadioValue(oOptions) == ""));
}

function getRadioValue(radio)
{   
  // Get checked value from radio button.
  for (var i = 0; i < radio.length; i++)
  {   
    if (radio[i].checked) {return radio[i].value;}
  }
  if (radio.checked){
    return radio.value;
  }else{
    return "";
  }
}

function getSelectValue(select)
{   
  // Get selected value from select list.
  for (var i = 0; i < select.length; i++)
  {   
    if (select[i].selected) {return select[i].value;}
  }
  if (select.selected){
    return select.value;
  }else{
    return "";
  }
}


function linkOffSite(sDest)
{
  //open a new window & display a page from off the rdental site
  //sDest must be URL encoded
  window.open("/offsite.asp?destination="+sDest,null,"height=400,width=600,status=yes,toolbar=yes,menubar=yes,location=yes");
}

function validEmail(sEmail)
{
  return  (sEmail.indexOf('@') != -1) && (sEmail.indexOf('@') != 0) && (sEmail.indexOf(' ') == -1) && (sEmail.indexOf('.') != -1) && (sEmail.lastIndexOf('.') - 1 > sEmail.indexOf('@') && (sEmail.lastIndexOf('.') < sEmail.length - 2))
}


function split(str, split, iSize)
{
	// Function will take a date as a string and split it into an array of the date 
	// values. It will use "split" as the delimiter of each value. mm/dd/yyyy.
	// Returned is the array that holds the date values: month,day,year.
	
	var asSplit = new Array(iSize - 1);
	var nextChar, iCount = 0, iLast = -1, i;
	
	//This will loop through the string, stop when the char = split and place
	//that value before it in the array.	
	for(i = 0; i < str.length; i++)
		{
			nextChar = str.charAt(i);
			if(nextChar == split && iCount <= iSize)
				{
					asSplit[iCount] = str.substring(iLast + 1, i);
					iCount = iCount + 1;
					
					iLast = i;
				}
		}
				
	if(iLast < str.length && iCount <= iSize)
		asSplit[iCount] = str.substring(iLast + 1, str.length);

	return asSplit;
}	
	

function isValidDate(Date)
{
	// This will check that a date is valid. It is all numbers, and that those 
	// numbers are valid for the respective month, day and year values.
	// Returns bvalid which is true if the date is valid, false if not.
	
	var bvalid = true;
	var asDMY, asMonth; 
	var nextChar, i, j, iLeapYear, sMonths;
	
	//Get the split date
	asDMY = split(Date, "/" ,3);
	for(i=0; i<3; i++)
	{

		//Error check to make sure the date entered was in the correct format
		//This checks the array values returned from split for verification.
		if(asDMY[i] == null)
		{
  		return false;
		}
				
	}
	//Seperate the time from the year
	//Date time should be entered in format 1/1/2003 1:23:45 PM
	var time = new Array(1);
	if (asDMY[2].length > 4)
	{
	time = split(asDMY[2], " ", 3);
	
	
	//Set the year as the third value in the array again minus the time
	asDMY[2] = time[0];	
	//Check that the time includes only numbers
	var asHMS = new Array(2);
	if (time[1]) {
	asHMS = split(time[1], ":", 3);
	for (i = 0; i<asHMS.length -1; i++)
	{
		var sHMS = asHMS[i];
		for (j = 0; j < sHMS.length; j++)
		{
			nextChar = sHMS.charAt(j);
			if(!(nextChar >= "0" && nextChar <= "9"))
			{
				bvalid = false;
			}
		}
		if (bvalid) 
		{
			if (i==0)
			{
				if(!(parseInt(sHMS) >= 0 && parseInt(sHMS) <= 12))
				{
					bvalid = false;
				}
			}
			if (i>0)
			{
				if (!(parseInt(sHMS) >= 0 && parseInt(sHMS) <= 59))
				{
					bvalid = false;
				}
			}
		}
		}
	}
	
	if ((time[2] != "AM") && (time[2] != "am") && (time[2] != "PM") && (time[2] != "pm"))
	{
		bvalid = false;
	}
	}

	//Check that they are numbers
	for(i = 0; i<3; i++)
	{
		var sDMY = asDMY[i];
		for(j = 0; j < sDMY.length; j++)
			{
				nextChar = sDMY.charAt(j);
				if(!(nextChar >= "0" && nextChar <= "9"))
					bvalid = false;
			}
	}
	
	//Covert the two digit year to a four digit year.
	//"99" is hard coded to be equal to 1999.
	if(asDMY[2].length == 2)
	{
	/*	if((asDMY[2].charAt(0) == "9") && (asDMY[2].charAt(1) == "9"))
			asDMY[2] = "19" + asDMY[2];
		else
			asDMY[2] = "20" + asDMY[2]; */
	  bValid=false
	}
	
	//Check that they are valid mm/dd/yyyy dates
	//Handles leap years
	if(bvalid)
	{
		if(asDMY[2].length == 4)
		{
			var iDay = parseInt(asDMY[1], 10);
			var iMonth = parseInt(asDMY[0], 10);
			var iYear = parseInt(asDMY[2], 10);
			//month check
			if(!(iMonth >= 1 && iMonth <= 12))
				bvalid = false;
			else
			{		
				//days in month check			
				if((iYear % 4 == 0) && (!(iYear % 100 == 0) || 	(iYear % 400 == 0)))						
				{
					iLeapYear = 0;
					sMonths = "31 29 31 30 31 30 31 31 30 31 30 31";
				}
				else	
				{
					iLeapYear = 1;
					sMonths = "31 28 31 30 31 30 31 31 30 31 30 31";
				}
					
				asMonths = split(sMonths, " ", 12);
				if(iDay < 1 || iDay > parseInt(asMonths[iMonth-1], 10))
					bvalid = false;
			}		
		}
		else 
			bvalid = false;
	}

return bvalid;
}	

function isValidNumber(sVal)
{
  var bvalid=true;
  
  if (sVal.length==0){
    return false;
  }

	for(var j = 0; j < sVal.length; j++)
		{
			var nextChar = sVal.charAt(j);
			if(!(nextChar >= "0" && nextChar <= "9"))
				bvalid = false;
		}
  return bvalid;
}

function isPhoneValid(sPhone)
{
  if (sPhone.length < 12)  //extra chars at end are allowed for extensions
  {
     return false;
  }
  if (sPhone.indexOf('-') == 3 && sPhone.lastIndexOf('-') == 7)
        {
           sPhone.replace(/-/g,'0');  //for verification use only
        }
     else
        {
           return false;
        }    
  if (sPhone.match(/\D/))
     {
         return false;
     }
   return true;
}   


// the following are used with the multi select list boxes to move items to and from
function moveListItem(sourceList, targetList)
{
  var iSelCount=0;
  for (var iOption=0; iOption<sourceList.options.length;iOption++){
    if (sourceList.options[iOption].selected)
    {
      iSelCount++
    }
  }

  if (iSelCount>5){
    alert('Due to a browser bug, you may not move more than 5 values at once.');
    return;
  }

  for (var iOption=0; iOption<sourceList.options.length;iOption++){
    if (sourceList.options[iOption].selected && sourceList.options[iOption].value != '__')
    {
      var oOption = new Option;
      oOption.value = sourceList.options[iOption].value;
      oOption.text = sourceList.options[iOption].text;
      
      targetList.options[targetList.length]= oOption;  
    }
  }
  
  for (var iOption=sourceList.options.length-1; iOption>=0;iOption--){
    if (sourceList.options[iOption].selected && sourceList.options[iOption].value != '__')
    {
        sourceList.options[iOption]=null;
    }
  }

}

function selectAllItems(list)
{
  for (var iOption=list.options.length-1; iOption>=0;iOption--){
    list.options[iOption].selected=true;
  }
}

function validatePickList(list, requireOne, listDescription)
{
  if (requireOne && (list.options.length==0  || (list.options.length==1 && list.options[0].value=='__'))){
    list.focus();
    alert('You must select at least one option for ' + listDescription + '.');
    return false;
  }
    
  selectAllItems(list);
  
  return true;
} 

