/* 
	newform.js
	File containing all the general script files prepared for Shaftesbury CC website
	Created by Questronics Computing Service
 */

var ns4 = document.layers
var ie4 = document.all
var ns6 = document.getElementById && !document.all
var isNav = (ns4 || ns6)
var isIE = !isNav

var Months = new Array ("January","February","March","April","May","June","July","August","September","October","November","December")
var Mons = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var Days = new Array ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var cMonths = new Array('January','February','March','April','May','June','July','August','September','October','November','December')
var cDays = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')

/// longDate  return 'dd/mm/yyyy/' as Day + date + month + year  e.g. Saturday 31st March 2001 from 31/03/2001 

function longDate(date, showDay, showYear) {
	var today = new Date()
	if (date.length > 10) {return date}

	var i, x=0, y
	x=date.indexOf("/",x)
	var nDay = date.substr(0,x++) - 0
	y=date.indexOf("/",x)
	var nMonth = date.substring(x,y++) - 1
	var nYear=date.substr(y)
	if (nYear <= 80) {nYear += 2000}
	if (nYear < 100) {nYear += 1900}
	today = new Date(nYear, nMonth, nDay)
	return ((showDay) ? Days[today.getDay()] + ' ' : '') + AddSuffix(nDay) + ' ' + Months[nMonth] + ((showYear) ? ' ' + nYear : '')
/*
	date = date.replace('/','')
	date = date.replace('/','')
	var nDay = date.substring(0,2)
	if (nDay.substr(0,1) == '0') { nDay = nDay.substr(1) }
	var nMonth = date.substr(2,2)
	if (nMonth.substr(0,1) == '0') { nMonth = nMonth.substr(1) }
	var nYear = date.substr(4)
	if (nYear <= 80) {nYear += 2000}
	if (nYear < 100) {nYear += 1900}
	today.setFullYear(nYear)
	today.setMonth(nMonth - 1)
	today.setDate(nDay)
*/
	var fullDate = ((showDay) ? Days[today.getDay()] + ' ' : '') + AddSuffix(nDay) + ' ' + Months[today.getMonth()] + ((showYear) ? ' ' + today.getFullYear() : '')
	return fullDate
}

///  isGone checks to see if startDate is within <interval> days of today 

function isGone(startDate, interval) {
	if (isNaN(interval)) { interval = 0 }
	if (interval == 0) { interval = -1/24 }		//	mod to disable website entry after 11.00 pm night before race
	var i, x, space, cText = ""
	var now = new Date()
	var nDay, nMonth, nYear, entryDate, cTime="" 
 	warnDate = now.setTime(new Date().getTime() - (interval *  24 * 3600000))			 // <interval> days back
	if (startDate.indexOf('/') == -1) {
		for (x = 0; x < startDate.length; x++) {
			if ((!isNaN(startDate.substr(x,1))) && (startDate.substr(x,1) != ' ')) {
				startDate = startDate.substr(x)
				break
			}
		}
		cDay = parseInt(startDate).toString()
		x = startDate.lastIndexOf(' ')
		if (x <= 0) { return true }
		cYear = startDate.substr(x + 1)
		startDate = startDate.substring(0,x)
		x = startDate.indexOf(' ')
		if (x <= 0) return true
		cMonth = startDate.substr(x+1)
	} else {
		x=startDate.indexOf("/",x)
		cDay=startDate.substr(0,x++) - 0
		y=startDate.indexOf("/",x)
		nMonth = startDate.substring(x,y++) - 1											// has to be -1 as arrays start from position 0 (zero)
		cMonth = Months[nMonth]
		cYear=startDate.substr(y)
	}
	if (cYear.indexOf(":") > -1) {
		x=cYear.indexOf(":")
		cTime = cYear.substr(x + 1)
		cYear = cYear.substring(0, x)
	}
	if (cYear.length==2) { cYear = '20' + cYear }
	entryDate = new Date(cMonth + cDay + ", " + cYear + "  " + cTime)
//alert(startDate+'  '+entryDate+'  '+new Date(warnDate)+'  '+(entryDate < warnDate))
	return (entryDate < warnDate)
}

///  is_new checks to see if startDate is within <interval> days of today 

function is_new(startDate, interval) {
	if (isNaN(interval)) { interval = 7 }
	var Today, elapsed, secs1, secs2, cDay, cYear, cMonth, nMonth, x, firstDay, diff
	Today = new Date()
	tDay = Today.getDate()
	tMonth = Today.getMonth()
	tYear = Today.getFullYear()
	elapsed = interval * 24 * 3600 * 1000
	elapsed = Math.abs(elapsed)
	Today.toUTCString()
	secs1=Date.parse(Today)
	if (startDate.indexOf(' ') > 0) {
		for (x = 0; x < startDate.length; x++) {
			if ((!isNaN(startDate.substr(x,1))) && (startDate.substr(x,1) != ' ')) {
				startDate = startDate.substr(x)
				break
			}
		}
		cDay = parseInt(startDate).toString()
		x = startDate.lastIndexOf(' ')
		if (x <= 0) { return true }
		cYear = startDate.substr(x + 1)
		startDate = startDate.substring(0,x)
		x = startDate.indexOf(' ')
		if (x <= 0) return true
		cMonth = startDate.substr(x+1)
		for (x = 0, nMonth = -1; x < 12; x++) { if (Months[x] == cMonth) { nMonth = x; break } }
	} else {
		cYear=startDate.substr(6,4)
		cDay=startDate.substr(0,2)
		nMonth=startDate.substr(3,2)-1	// has to be -1 as arrays start from position 0 (zero)
		cMonth = Months[nMonth]
	}
//alert(tDay+'  '+tMonth+'  '+tYear+'  -  '+parseInt(cDay)+'  '+nMonth+'  '+parseInt(cYear)+'   i='+interval)
	if (cYear == 'yyyy') { cYear = tYear }
	if ((interval == 0) && ((tDay == parseInt(cDay)) && (tMonth == nMonth) && (tYear == parseInt(cYear)))) { return false }
	if (interval == 1) {
		return ((tDay == parseInt(cDay)) && (tMonth == nMonth) && (tYear == parseInt(cYear)))
	}
	firstDay = new Date(cMonth+' '+cDay+', '+cYear)
	firstDay.toUTCString()
	secs2=Date.parse(firstDay)
	if (interval <= 0) { diff =(secs2 - secs1) }
	else { diff = (secs1 - secs2) }
//alert(diff+'  '+elapsed)
	return  ( diff <= elapsed ) 
}

/// 	trim  -  removes leading and trailing whitespace	
function trim(cText) {
	if ((typeof cText == 'string') && (cText.length > 0)) { 
		while (cText.substr(cText.length - 1) == " ") { cText=cText.substr(0, (cText.length - 1)) }		//	removing trailing space
		while (cText.substr(0, 1) == " ") { cText = cText.substr(1) }	//	removing leading space
		if (cText == " ") { cText = "" }
	}
	return cText
}

///  changeThisCase(this, dirn)
///	used as an entry level validation routine that converts case of entered text
///	Uses the generic changeCase() to do the work

function changeThisCase(data, dirn) {
	data.value=changeCase(data.value, dirn)
}

///	changeCase(text, dirn)
///	used as a routine that converts case of text parameter
///	dirn of 'ucase'	=	UPPER CASE but allow for Mc & Mac
///	dirn of 'lower'	=	lower case
///	dirn of 'proper'	=	lower case with Initial letter of each word in CAPITALS, also allow for Mc, Mac & O, also hyphenated names'

function changeCase(cText, dirn) {
	cText=trim(cText)
	var i = cText.search(' ')
	if ((dirn == 'lower') || (dirn == 'proper')) { cText=cText.toLowerCase() }
	else { cText=cText.toUpperCase() }
	if (dirn == 'proper') { 
		cText = cText.substr(0,1).toUpperCase() + cText.substr(1)
		if (i > 0) { cText = cText.substr(0,(i + 1)) + cText.substr((i + 1),1).toUpperCase() + cText.substr(i + 2); cText1=cText }
	}
	if (dirn != 'lower') {
		if (i > 0) {
			if (cText.substr((i+1),2).toUpperCase() == "O'") { cText = cText.substr(0,(i+1)) + "O'" + cText.substr(i+3,1).toUpperCase() + cText.substr(i + 4) }
			if (cText.substr((i+1),2).toUpperCase() == 'MC') { cText = cText.substr(0,(i+1)) + 'Mc'  + cText.substr(i+3,1).toUpperCase() + cText.substr(i + 4) }
			if (cText.substr((i+1),3).toUpperCase() == 'MAC') { cText = cText.substr(0,(i+1)) + 'Mac'  + cText.substr(i+4,1).toUpperCase() + cText.substr(i + 5) }
		} else {
			if (cText.substr(0,2).toUpperCase() == "O'") { cText = cText = "O'" + cText.substr(2,1).toUpperCase() + cText.substr(3) }
			if (cText.substr(0,2).toUpperCase() == 'MC') { cText = 'Mc' + cText.substr(2,1).toUpperCase() + cText.substr(3) }
			if (cText.substr(0,3).toUpperCase() == 'MAC') { cText = 'Mac' + cText.substr(3,1).toUpperCase() + cText.substr(4) }
		}
		i = cText.search('-')
		if (i > 0) { cText = cText.substr(0,(i + 1)) + cText.substr((i + 1),1).toUpperCase() + cText.substr(i + 2); }
	}
	return cText
}

function AddSuffix(val) {
	var num = val, suff='th'
	if ((isNaN(num)) || (typeof num == 'string')) { num = parseInt(num) }
	var n = num % 100
	if ((n < 11) || (n > 13)) {
		switch (num % 10) {
			case 1:
				suff = 'st'
				break
			case 2:
				suff = 'nd'
				break
			case 3:
				suff = 'rd'
				break
		}
	}
	return num.toString(10) + suff
}

function checkThisDate(data) {
	var val = data.value
	if (val.length == 0) { Age = 0; return '' }
	data.value=checkDate(val, 0, true)
	val = data.value
	if (((val.length > 0) && (val.length < 6)) || isNaN(parseInt(val))) { alert('Date Format invalid') }
	return true
}

function checkDate(cDate, Age, Update) {
	var nDay, nMonth, nYear, dateArray
	cDate = trim(cDate)
	if (cDate.length == 0) { Age = 0; return (Update ? '' : true) }
	if ((cDate.length < 6) || isNaN(parseInt(cDate))) { 
		if ((Update != null) && (Update == true)) { 
//			alert('Date Format invalid')
			return cDate 
		} else { return false }
	}
	if (cDate.indexOf('/') > 0) { dateArray = cDate.split('/')	 }
	else if (cDate.indexOf('-') > 0) { dateArray = cDate.split('-')	}
	else if (cDate.indexOf(':') > 0) { dateArray = cDate.split(':')	}
	else if (cDate.indexOf(' ') > 0) { dateArray = cDate.split(' ') 	}
	else { dateArray = new Array(cDate.substr(0,2), cDate.substr(2,2), cDate.substr(4)) }
	for (i = 0; i < 3; i++) { if (dateArray[i].substr(0,1) == '0') { dateArray[i] = dateArray[i].substr(1) } }
	if (dateArray[0].indexOf('&') > 0) { 
		nDay = dateArray[0] 
	} else {
		nDay = parseInt(dateArray[0])
	}
	if (!isNaN(dateArray[1])) { nMonth = parseInt(dateArray[1]) }
	else {
		nMonth = dateArray[1].substr(0,3)
		nMonth = changeCase(nMonth, 'proper')
		for (i = 0; i < 12; i++) { if (nMonth == Mons[i]) { nMonth = i + 1; break } }
	}
	nYear = parseInt(dateArray[2])
	if (nMonth > 12) {
		if (nDay < 13) {
			nMonth = nDay
			nDay = parseInt(dateArray[1])
		} else if (Update) { alert('Date Format invalid') }
		else { return false }
	}
	if (nYear <= 20) { nYear = 2000 + nYear }
	else if (nYear < 100) { nYear = 1900 + nYear }
	if (dateArray[0].indexOf('&') > 0) { 
		cDate = nDay+' '+ Months[nMonth-1]+' '+nYear
	} else {
		cDate = ((nDay < 10) ? '0' : '')+nDay+'/'+((nMonth < 10) ? '0' : '')+nMonth+'/'+nYear
//		cDate = ((nDay < 10) ? '0' : '')+nDay.toString(10)+'/'+((nMonth < 10) ? '0' : '')+nMonth.toString(10)+'/'+nYear.toString(10)
	}
	return (Update ? cDate : true)
}

function getAge(aDate, eDate) {
	var nDay, nMonth, nYear, dateArray, today, tDay, tMonth, tYear, dat, i, x, day, month, year, cDate
	if ((aDate == "") || (aDate == null) || (aDate == "undefined") || (aDate.length = 0)) { aDate = "" }
	if ((eDate == "") || (eDate == null) || (eDate == "undefined") || (eDate.length = 0)) { eDate = "" }
	if ((aDate == "") && (eDate == "")) { Age = 19; return Age }
	for (x = 0; x < 2; x++) {
		cDate = ((x == 0) ? aDate : eDate)
		if (cDate == "") { continue }
		if (cDate.indexOf('/') > 0) { dateArray = cDate.split('/')	 }
		else if (cDate.indexOf('-') > 0) { dateArray = cDate.split('-') }
		else if (cDate.indexOf(':') > 0) { dateArray = cDate.split(':') }
		else if (cDate.indexOf(' ') > 0) { dateArray = cDate.split(' ') }
		else { dateArray = new Array(cDate.substr(0,2), cDate.substr(2,2), cDate.substr(4)) }
		for (i = 0; i < 3; i++) { if (dateArray[i].substr(0,1) == '0') { dateArray[i] = dateArray[i].substr(1) } }
		day = parseInt(dateArray[0])
		month = parseInt(dateArray[1])
		if (isNaN(month)) {
			month = changeCase(dateArray[1], 'proper')
			for (j = 0; j < 12; j++) {
				if ((month == Months[j]) || (month == Mons[j])) { month = (j + 1); break }
			}
		}
		year = parseInt(dateArray[2])
		if (month > 12) {
			if (day < 13) {
				month = day
				day = parseInt(dateArray[1])
			}
		}
		if (year <= 20) { year = 2000 + year }
		else if (year < 100) { year = 1900 + year }
		cDate = ((day < 10) ? '0' : '') + day + '/' + ((month < 10) ? '0' : '') + month + '/' + year
		if (x == 0) { aDate = cDate; nDay = day; nMonth = month, nYear = year }
		else { eDate = cDate; tDay = day; tMonth = month, tYear = year }
	}
	if (eDate == "") {
		today = new Date()
		tDay = today.getDate()
		tMonth = today.getMonth() + 1
		tYear = today.getFullYear()
	}
	Age = tYear - nYear
	if (tMonth < nMonth) { Age-- }
	else if ((tMonth == nMonth) && (tDay < nDay)) { Age-- }
	return Age
}
