// signal an error on a form
function signalFormError(objField, sMsg){
	var sId =  objField.id + "_error";
	var objError = getObj( sId );
	if( objError ){
		objError.innerHTML = sMsg;
		objError.style.visibility = "visible";
		new Effect.Pulsate( objError, {duration:1} );
	}
}
// remove an error on a form
function clearFormError(objField){
	var objError = getObj( objField.id + "_error" );
	if( objError ){
		objError.innerHTML = "";
		objError.style.visibility = "hidden";
	}
}

// validation functions
function isValidEmail(sString){
	var iAtPos = sString.indexOf( "@" );
	// must have an @ sign and must have at least one char before it
	if( iAtPos < 2 ){
		return false;
	}
	var sDomain = sString.split( "@" )[1];
	if( sDomain.length < 1 ){
		return false;
	}
	if( sDomain.match("[a-z0-9]").length < 1 ){
		return false;
	}
	return true;
}

// check if the values of the given date component fields make a valid date
function isValidDate( objYearFld, objMonthFld, objDayFld ){
	
	var objDate = makeDate( objYearFld, objMonthFld, objDayFld );
	
	if( isNaN( objDate ) ){
		return false;
	}
	
	return true;
}

// get a Date object from form fields
function makeDate( objYearFld, objMonthFld, objDayFld ){

	var iDay = objDayFld.value;
	var iMonth = objMonthFld.value;
	var iYear = objYearFld.value;

	// NOTE: Potential Gotcha! - in JS, the month must be an index into an internal array,
	// because month 2 == MARCH, not February!
	var objDate = new Date( iYear, iMonth - 1, iDay );
	// NOTE : Another potential gotcha!
	// If you try to create a date of, say, 31st February,
	// the Date constructor just rolls it over to be 3rd March.
	// So, we need to read the values back, and check that the
	// day and month matches what we were given
	// (bearing in mind that the month value is offset by one, 
	//	and that getDay() returns day-of-week, so we need to call getDate() instead)
	if( 	objDate.getDate() != iDay 
		||	objDate.getMonth() != (iMonth-1)
	){
		objDate = NaN;
	}
	return objDate;
}


function showCount( objFld, iMaxLength, objCount ){
	var iLength = objFld.value.length;
	var sMsg = "<span>&laquo; " + (iMaxLength - iLength) + " characters left</span>";
	objCount.innerHTML = sMsg;
	objCount.style.top = findPosY( objFld ) + "px";
	objCount.style.left = findPosX( objFld ) + getObjWidth( objFld ) + "px";
	objCount.style.visibility = "visible";
}

function hideCount( ){
	objCount = document.getElementById( "divCharCount" );
	objCount.style.visibility = "hidden";
}

function trim(s) {
	while (s.substring(0,1) == ' ' || s.charCodeAt(0) == 13 || s.charCodeAt(0) == 10 ) {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ' || s.charCodeAt(s.length-1) == 13 || s.charCodeAt(s.length-1) == 10 ) {
		s = s.substring(0,s.length-1);
	}
	return s;
}

function checkLength(e){
	
	if( !e ){ var e = window.event; }
	
	obj = eventElement(e);
	
	var iMax = obj.getAttribute("maxlength");
	
	var iVal = trim(obj.value);
	if( iVal.length > iMax ){
		obj.value = iVal.substring(0,iMax);
		cancelEvent(e);
		return false;
	}
	
	showCount( obj, iMax, document.getElementById("divCharCount") );
}

function delConfirm( sTitle, sURL ){
	if( confirm( "Are you sure you want to delete '" + sTitle + "'?" ) ){
		document.location.href = sURL;
	}
}


function setupFormEvents(){
	var arrForms = document.forms;
	for( var i=0; i < arrForms.length; i++ ){
		if( arrForms[i].bCharCount && arrForms[i].bCharCount.value ){
			var arrElements = arrForms[i].elements;
			for( var j=0; j < arrElements.length; j++ ){
				setupElementEvents( arrElements[j] );
			}
		}
	}
}

function setupElementEvents( obj ){
	var iMax = obj.getAttribute("maxlength");
	if( iMax ){
		// obj.onchange = checkLength;
		addListener( obj, "blur", hideCount );
		addKeyListener( obj, checkLength );
	}	
}

// check tags
function checkTagList(objTags){
	var sTags = objTags.value;
	// first we strip certain punctuation
	var rePunct = /['",;]/g;
	sTags = sTags.replace( rePunct, "" );
	// then replace any multiple spaces with single spaces 
	var reSpaces = /\s+/g;
	sTags = sTags.replace( reSpaces, " " );
	// then we split at spaces
	arrTags = sTags.split(" ");
	// then check each element for length < 25
	for( var i = 0; i < arrTags.length; i ++ ){
		if( arrTags[i].length > 25 ){
			alert( "Each tag must be less than 25 characters long\n\t" + arrTags[i] + " is " + arrTags[i].length + " characters long!" );
			objTags.focus();
			return false;
		}
	} 
	
	// if we're here, it went ok
	// so return the sanitised value
	return sTags;
}

addOnLoad( setupFormEvents );
