function dateAdd(startDate, numDays, numMonths, numYears)
{	
	cleanDate = new Date(startDate);	
	var returnDate = new Date(cleanDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth() + numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);

	// Get the month and year
	var month = returnDate.getMonth() + 1;
	var year = returnDate.getYear();

	// Get the day
	var strTemp = returnDate.toString();
	var splitArray = strTemp.split(' ');
	var day = splitArray[2];
	
	// Put together the date in formatted string format
	var strStringDate = month + '/' + day + '/' + year;
	
	return strStringDate;
}

function yearAdd(startDate, numYears)
{
	return DateAdd(startDate, 0, 0, numYears);
}

function monthAdd(startDate, numMonths)
{
	return DateAdd(startDate, 0, numMonths,0);
}

function dayAdd(startDate, numDays)
{
	return DateAdd(startDate, numDays, 0, 0);
}

/* Takes two strings that represents dates, and returns a boolean
   indicating whether or not the first date is after the second date. */
function isFirstDateEarlier(strFirstDate, strSecondDate)
{
	var month;
	var day;
	var year;
	var splitArray;
	
	// Get the first date
	splitArray = strFirstDate.split("/");
	month = splitArray[1];
	day = splitArray[0];
	year = splitArray[2];
	var firstDate = new Date(year, month - 1, day)

	// Get the second date
	splitArray = strSecondDate.split("/");
	month = splitArray[1];
	day = splitArray[0];
	year = splitArray[2];
	var secondDate = new Date(year, month - 1, day)
	
	if (firstDate < secondDate)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// Trim leading and trailing spaces
function trim(lstr) 
{
    return ltrim(rtrim(lstr));
}
  
//  This function trims all spaces from the left-hand side of a string.
function ltrim(lstr) 
{
	if (lstr != "") 
	{
		var strlen, cptr, lpflag, chk;
		strlen = lstr.length;
		cptr = 0;
		lpflag = true;

		do 
		{
			chk = lstr.charAt(cptr);
            if (chk != " ") 
            {
				lpflag = false;
			}
            else 
            {
                if (cptr == strlen) 
                {
					lpflag = false;
				}
                else 
                {
					cptr++;
				}
			}
		}
        
        while (lpflag == true)
		if (cptr > 0) 
		{
			lstr = lstr.substring(cptr,strlen);
		}
	}
	
	return lstr;
}

//  This function trims all spaces from the right-hand side of a string.
function rtrim(lstr) 
{
	if (lstr != "") 
	{
		var strlen, cptr, lpflag, chk;
		strlen = lstr.length;
		cptr = strlen;
		lpflag = true;

		do 
		{
			chk=lstr.charAt(cptr-1);
			if (chk != " ") 
			{
			    lpflag = false;
			}
			else 
			{
				if (cptr == 0) 
				{
					lpflag = false;
				}
				else 
				{
				    cptr--;
				}
			}
		}

        while (lpflag == true)
        if (cptr < strlen) 
        {
			lstr = lstr.substring(0, cptr);
		}
	}
    
    return lstr;
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************
function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper European date or not. It validates format matching either
// dd-mm-yyyy or dd/mm/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************
function isDateEuropean(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
        return false;
    }

    day = matchArray[1]; // parse date into variables
    month = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}


/*	The isNumeric function validates a string to determine whether or not it contains a numeric value.
	Parameters: lstr - Contains string value to validate
	Returns: true/false  */
function isNumeric(lstr) 
{
	lstr = rtrim(lstr);
	if (lstr != "") 
	{
		//declare local variables
		var strlen, curptr, setptr, chk, inloop, decflag, minusflag, iserror;
		iserror = false;
		decflag = false;
		minusflag = false;
		strlen = lstr.length;
		curptr = 0;
		chk;
		// first check for space - .
		inloop = true;
		for (curptr = 0; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk >= "0" && chk <= "9")
			{
				break;
			}
			
			if (chk == "-") 
			{
				minusflag = true;
				break;
			}
			if (chk == ".") 
			{
				decflag = true;
				break;
			}
			if (chk != " ") 
			{
				return false;
			}
		}
		
		if (curptr >= strlen-1) 
		{
			if (decflag || minusflag || chk == " ") 
			{
				return false;
			}
			else 
			{
				return true;
			}
		}
		setptr = curptr + 1;
		for (curptr = setptr; curptr < strlen; curptr++) 
		{
			chk = lstr.charAt(curptr);
			if (chk < "0" || chk > "9") 
			{
				if (chk != ".") 
				{
					return false;
				}
				else
				{
					if (decflag) 
						{
							return false;
						}
					else 
					{
						decflag = true;
					}
				}
			}
		}
		return true;
	}
	return false;
}

function IsEmail(strEmail) {
	str = strEmail;
	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 IsPostcode(Postcode) {
	rexp = /^[a-zA-Z]{1,2}(([0-9] )|([0-9]{2}))[0-9][a-zA-Z]{2}$/;
	if(rexp.test(Postcode))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function reloadcaptcha(currentlink) {
	currentlink.parentNode.parentNode.parentNode.getElementsByTagName('img')[0].src+='&r='+(new Date()).getTime();
}
