function ExpandQuotes(tmpText) {
//Replaces all quotes with double quotes
   return tmpText.replace(/'/g,"''");
}

function ValidateMe(el, validate) {
	
	if (validate != "") {
		switch (validate) {
			case "date": {
				if (el.value != "") {
					if (!isValidDate(el.value)){
						el.value = "";
					}
				}
				break;
			}
			case "required": {
				break;
			}
		}
	}	
}


function isValidDate(dateStr) 
{

// Checks for the following valid date formats:
// DD/MM/YY   DD/MM/YYYY   DD-MM-YY   DD-MM-YYYY
// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	var result = "";

	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		return "Invalid Date!";
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		return "Invalid Date! Month must be between 1 and 12.";
	}
	if (day < 1 || day > 31) {
		return "Invalid Date! Day must be between 1 and 31.";
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return "Invalid Date! Month "+month+" doesn't have 31 days!"
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			return "Invalid Date! February " + year + " doesn't have " + day + " days!";
	   }
	}
	return "";  // date is valid
}

function isNumeric(n)
{
	if (isNaN(n))
		return "Not a number";
	else
		return "";
}

function isNumericInRange(n, nMin, nMax)
{
	if (isNaN(n))
		return "Not a number"
	else
	{
		if (isNaN(nMin))
			return "Invalid nMin!";
		if (isNaN(nMax))
			return "Invalid nMax!";
			
		if ((parseInt(n) < parseInt(nMin)) | (parseInt(n) > parseInt(nMax)))
			return "Out of range: (" + nMin + "-" + nMax + ")";
		else
			return "";
	}
}

function isEmailAddress(emailStr)
{
	if (emailStr.match(/\w+\@[.\w]+/) == null)
		return "Invalid email address!";
	else
		return "";
}


function XXisEmpty(n)
{
	if (n == "")
		return "Empty"
	else
		return "";


}

function isEmpty(n)
{
	if (!n)
		return true;
		
	return (n=='') ? true : false;
}

function ValidateForm(f)
{
	var t = "";
	var v = "";
	var title = "";
	var bRequired = false;
	
	//alert("valiodate!");
	
	for (var i = 0; i < f.elements.length; i++)
	{
		el = f.elements[i];
		
		v = "";
		validParams = aValidParams[el.name];
		
		if (!validParams) continue;
		
		validParams = validParams.split(",");
		
		var elValue = '';
		switch (el.type)
		{
			case "text":
			case "textarea":
			case "password":
				elValue = el.value;
				break;
			case "select-one":
				if (el.selectedIndex >= 0) {
					elValue = el.options[el.selectedIndex].value;
				}
				break;
			case "select-multiple":
				for ( var j=0; j<el.options.length; j++)
				{
					//elValue += el.options[j].value;
				    if  (el.options[j].selected)
				    {
						if (elValue != "") elValue += ",";
				        elValue += el.options[j].value;
				    }
				}
				break;
		}

		//Check if Required field
		bRequired = (validParams[0] == "r");
		if (bRequired)
		{
			if (isEmpty(elValue))
				v = "Required field!";
		}

		//Validate contents of field
		if (v == "")
		{


			switch (el.type)
			{
				case "text":
				case "textarea":
				case "select-one":
		
					switch (validParams[1])
					{
						case "x": //Not empty
							v = isEmpty(elValue);
							break;
							
						case "d": //Date
							if (!isEmpty(elValue))
								v = isValidDate(elValue);
							break;

						case "n": //Numeric
							v = isNumeric(elValue);
							break;
							
						case "nr": //Numeric range
							v = isNumericInRange(elValue, validParams[2], validParams[3]);
							break;
						
						case "e": //Email address
							v = isEmailAddress(elValue);
							break;
					}
					break;
				
				case "button":
					break;
			}
		}
		
		if (v != "")
		{
			if (el.title)
				title = el.title;
			else
				title = el.name;
				
			t = t + title + ": " + v + "\n";
		}
	}
	
	return(t);
	
}


  function GetAge(dd1, mm1, yy1) 
  {
    if (isNaN(mm1) || 
       (mm1 < 1)   ||
       (mm1 > 12)) 
    { 
		alert ("Bad Month"); 
    }
    else 
    {
		mm1--;
    }
    if (isNaN(dd1)) { alert ("Invalid Day"); }
    if (isNaN(yy1)) { alert ("Invalid Year"); }
    if (dd1 < 1) 
    {
        dd1 = 1;
    }
    else if ((dd1 > 30) && ((mm1 == 3) || (mm1 == 5) || (mm1 == 8) || (mm1 == 10))) 
    { 
		dd1 = 30;
    }
    else if ((dd1 > 29) && (mm1 == 1) && LeapYear(yy1))
    {
		dd1 = 29;
    }
    else if ((dd1 > 28) && (mm1 == 1))
    {
		dd1 = 28;
        document.agedata.dd.value = 28;
    }
    else if (dd1 > 31) 
    {
		dd1 = 31;
    }

    myDate = new Date(yy1,mm1,dd1);
    var agedays = Math.floor((new Date() - myDate) / 24 / 60 / 60 / 1000);
    var ageyrs = Math.floor((new Date() - myDate) / 24 / 60 / 60 / 1000 / 365.25);
    
    return ageyrs;
  }

  function round(x) { return Math.round (x * 100) / 100; }

  function LeapYear(year) {
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) return true;
    else return false;
  }
