var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;

//general array to store list of error cells
var errorCells = new Array();


function hasErrorCell(cell){
	for(i=0; i< errorCells.length; i++){
		if (errorCells[i] == cell) return true;
	}
	return false;
}

//add a new error cell into the form
function addError(cellId, errorMessage){
	if (!hasErrorCell(cellId)){
		var errorcell = document.createElement('div');
		errorcell.innerHTML = errorMessage;
		errorcell.id = "error_" + cellId;
		errorCells.push(cellId);
	
		var cell = Dom.get(cellId);
		cell.className = "errorCell";
		cell.appendChild(errorcell);
	}
}

//remove the specific error cell from the form
function removeError(cellId){
	if (hasErrorCell(cellId)){
		errorCells.splice(i,1); //i is a variable from function hasErrorCell
		var cell = Dom.get(cellId);
		cell.className = '';
		
		var errorcell = Dom.get("error_" + cellId);
		errorcell.parentNode.removeChild(errorcell);
	}
}

//////////////
//validations
//////////////
function checkRequired (field, errorMsg, cellId ){
	if (cellId == undefined) cellId = field.name;
	if (/\w+/.test(field.value) == false)
		addError("edit_" + cellId, errorMsg);
	else removeError("edit_" + cellId);
}
function checkPhone(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if (field.value != ''){
		if(/^[+\s\d-\.\(\)]{7,20}$/.test(field.value) == false)
			addError("edit_" + cellId, "Invalid phone number. Use only digits and only characters: +-().");
		else removeError("edit_" + cellId);
	}
	else removeError("edit_" + cellId);
}
function checkYear(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if (field.value != ''){
		if(/^((19)||(20))\d\d$/.test(field.value) == false)
			addError("edit_" + cellId, "Invalid year. Year range is between 1900 to 2099");
		else removeError("edit_" + cellId);
	}
	else removeError("edit_" + cellId);
}
function checkUrl(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if (field.value != ''){
		if(/^(www\.)?.+(\.\w{2,3})+.*$/.test(field.value) == false)
			addError("edit_" + cellId, "Invalid Url. Do not include \"http://\"");
		else removeError("edit_" + cellId);
	}
	else removeError("edit_" + cellId);
}
function checkNum(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if (field.value != ''){
		if(/^[-]?\d+$/.test(field.value) == false){
			addError("edit_" + cellId, "Invalid number. Please only enter digits 0-9");
			return false;
		}
		else removeError("edit_" + cellId);
		return true;
	}
	else{
		removeError("edit_" + cellId);
		return false;
	}
}
function checkEmail(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if (field.value != ''){
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value) == false)
			addError("edit_" + cellId, "Invalid email address");
		else removeError("edit_"+cellId);
	}
	else removeError("edit_"+cellId);
}
function checkTime(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if(/^\d\d:\d\d:?\d?\d?$/.test(field.value) == false){
		addError("edit_" + cellId, "Invalid time. Please enter the time in HH:MM:SS");
		return false;
	}
	else removeError("edit_" + cellId);
	return true;
}
function checkDate(field, cellId){
	if (cellId == undefined) cellId = field.name;
	if(/^((19)||(20))\d\d-\d?\d-\d?\d$/.test(field.value) == false){
		addError("edit_" + cellId, "Invalid date. Please enter the date in YYYY-MM-DD");
		return false;
	}
	else removeError("edit_" + cellId);
	return true;
}
function checkMaxSum(field1, field2, maxSum, cellId1, cellId2){
	if (cellId1 == undefined) cellId1 = field1.name;
	if (cellId2 == undefined) cellId2 = field2.name;
	
	//check to see if both are digits
	var bothNum = checkNum(field1, cellId1);
	bothNum = checkNum(field2,cellId2) && bothNum; //placement of && determines whether the function will be run
	
	if (bothNum){
		if (parseInt(field1.value) + parseInt(field2.value) > maxSum){ //error
			addError("edit_" + cellId1, "Invalid. The sum of these two fields must be less than " + maxSum);
			addError("edit_" + cellId2, "Invalid. The sum of these two fields must be less than " + maxSum);
		}
		else{
			removeError("edit_" + cellId1);
			removeError("edit_" + cellId2);
		}
	}
}
//////////////
//end of validations
//////////////
