//
// formvalidate.js
// (C) Anondesign Ltd. 2003
//
// All these functions return true if the form element is valid
//

/*
	Version		Changes								Author
	1.00		First version release						Steve
	1.01		Added textrequired						Steve
	1.02		Added plusminus support						Steve
	1.03		Added dob function						Daniel
	1.04		Fixed integer offset error in date select box validation	Steve
	1.05		Added actual code for dob function				Steve
	1.06		Supped up the date validation code for multiple types		Steve
	1.07		Support integer boundaries					Steve
	1.08		Added validate_float following chg 1.24 in api_html		Daniel
	1.09		Support for api_html::1.27					Steve
*/
	
function validate_text(strElement,strDescription)
{
	return true;
}

function validate_dob(strElement,strDescription)
{
	// first off validate the date
	if (!validate_date(strElement,strDescription)) return false;
	

	nYear 	= document.getElementById(strElement+'-year').value;
	nMonth 	= document.getElementById(strElement+'-month').value;
	nDay	= document.getElementById(strElement+'-day').value;
	
	//

	// is it in future? 
	//
	now = new Date();
	if (check_date_before(nYear,nMonth,nDay,now)) {
		window.alert('The date entered for '+strDescription+' is in the future');
		return false;
	}
	
	//
	// does the user appear to be under 14? (US law)
	//
	now.year -= 14;
	if (check_date_before(nYear,nMonth,nDay,now)) {
		window.alert('You appear to be under 14. Under US law we are unable to take your details');
		return false;
	}
	
	//
	// are they too old?
	//
	if (nYear+100<now.year) {
		window.alert('The value entered for '+strDescription+' means that you appear to be dead.');
		return false;
	}
	
	return true;
}

function check_date_before(nYear,nMonth,nDay,now)
{
	if (nYear < now.year) return true;

	if (nYear > now.year) return false;
	
	if (nMonth < now.month) return true;
	if (nMonth > now.month) return false;
	
	return (nDay <= now.day);
}

function validate_textrequired(strElement,strDescription)
{
	ctrl = document.getElementById(strElement);
	
	if (ctrl.value == '') {
		window.alert('You have not specified anything for '+strDescription);
		return false;
	}
	else
		return true;
}

function validate_plusminus(strElement,strDescription,min,max)
{
	return validate_integer(strElement,strDescription,min,max);
}

function validate_hoursminutes(strElement,strDescription)
{
	return true;
}

function validate_money(strElement,strDescription)
{
	ctrl = document.getElementById(strElement);
	if (ctrl) {
		str = ctrl.value;
		j = parseFloat(str); 
		if (isNaN(j)) {
			window.alert('The value '+str+' in '+strDescription+' is not a valid monetry value. Do not use a currency denomination');
			return false;
		}
		else
			return true;
	}
	else
		return false;	
}

function validate_float(strElement,strDescription)
{
	ctrl = document.getElementById(strElement);
	if (ctrl) {
		str = ctrl.value;
		j = parseFloat(str); 
		if (isNaN(j)) {
			window.alert('The value '+str+' in '+strDescription+' is not a valid floating point number');
			return false;
		}
		else
			return true;
	}
	else
		return false;	
}

function validate_integer(strElement,strDescription,min,max)
{
	ctrl = document.getElementById(strElement);
	if (ctrl) {
		str = ctrl.value;
		j = parseInt(str);
		if (isNaN(j)) {
			window.alert('The value '+str+' in '+strDescription+' is not a valid integer.');
			return false;
		}
				
		if (!isNaN(min) && (min>j)) {
			window.alert('The value '+str+' in '+strDescription+' must be greater than or equal to '+min);
			return false;
		}
		
		if (!isNaN(max) && (j>max)) {
			window.alert('The value '+str+' in '+strDescription+' must be less than or equal to '+max);
			return false;
		}
		
		return true;
	}
	else
		return false;
}

function validate_password(strElement,strDescription)
{
	//
	// check if there is a second box
	//
	el1	= document.getElementById(strElement);
	el2 	= document.getElementById(strElement+'2');
	if (el2 && el1) {
		if (el1.value == el2.value) 
			return true;
		
		window.alert('The two passwords entered for the '+strDescription+' do not match. Please re-enter');
		return false;
			
	}
	else
		return true;
}

function validate_textarea(strElement,strDescription)
{
	return true;
}

function validate_checkbox(strElement,strDescription)
{
	return true;
}

function validate_radio(strElement,strDescription)
{
	return true;
}

function validate_email(strElement,strDescription)
{
	ctrl = document.getElementById(strElement);
	if (ctrl) {
		//re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/;
		re = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,4})(\]?)$/;
	        
	        if (re.test(ctrl.value))
	        	return true;
	        else {
	        	window.alert('Your entry for '+strDescription+' does not appear to be valid.');
	        	return false;
	        }
	}
	else
		return false;
}

function validate_phone(strElement,strDescription)
{
	ctrl = document.getElementById(strElement);
	if (ctrl) {
		re = /^ *(\+(\d| |\.|-)+)? *(\((\d| |\.|-)+\) *)?(\d| |\.|-)+$/;
	        
	        if (re.test(ctrl.value))
	        	return true;
	        else {
	        	window.alert('Your entry for '+strDescription+' does not appear to be valid.');
	        	return false;
	        }
	}
	else
		return false;
}

function validate_date(strElement,strDescription)
{
	ctrlY	= document.getElementById(strElement+'-year');
	ctrlM	= document.getElementById(strElement+'-month');
	ctrlD	= document.getElementById(strElement+'-day');
	
	nYear 	= (ctrlY ? ctrlY.value: -1);
	nMonth 	= (ctrlM ? ctrlM.value: -1);
	nDay	= (ctrlD ? ctrlD.value: -1);
	
	if (nYear == '') {
		window.alert('No year selected for '+strDescription);
		return false;
	}
	if (nMonth == '') {
		window.alert('No month selected for '+strDescription);
		return false;
	}
	if (nDay == '') {
		window.alert('No day selected for '+strDescription);
		return false;
	}
	
	if (nDay > -1 && nMonth > -1) {

		aDayMax = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		aMonths = ['January','Febuary','March','April','May','June','April','May','June','July','August','September','October','November','December'];
		
		//leap years - if the year divides by 4 exactly then its a leap year (except 2100, 2200, 2300)
		bLeap = (nYear > -1) ? (nYear % 4) ? false : ((nYear % 100) ? true : ((nYear % 400) ? false : true)) : true;
		aDayMax[1] = bLeap ? 29 : 28;
		
		if(nDay > aDayMax[(nMonth-1)])
		{
			//calculate an alternative
			nGuessDay 	= (nDay - aDayMax[nMonth-1]);
			strGuessMonth	= aMonths[nMonth % 12];
			window.alert('The date entered for '+strDescription+' does not exist. Did you mean '+nGuessDay+' '+strGuessMonth+'?');
			return false;
		}
	}
	return true;
}



function do_plusminus(strElement,delta,iMin,iMax,iStepMin)
{
	v = document.getElementById(strElement);
	if (v) {
		var c = parseInt(v.value)+delta;
		
		if (!isNaN(iMin))
			c = Math.max(c,iMin);
		if (!isNaN(iMax))
			c = Math.min(c,iMax);
		
		if (!isNaN(iStepMin)) {
			if (c < iStepMin) {
				if (delta < 0)
					c = 0;
				else
					c = iStepMin;
			}
		}
	
		v.value = c;
		if (v.onchange)
			v.onchange();
	}
}


