//Javascript Document

function checkFields(fields){
	for (i=0; i<fields.length; i++){
		if (fields[i].value == ''){
			alert('The '+fields[i].getAttribute("name")+' field must be completed');
			return false;
		}
	}
	return;
}


/* AJAX Functions */

var target = '';

function onLoad(){}

function doSearch(textfield){
	target = textfield+'_results';
	var q = document.getElementById(textfield).value;
	var url = '/includes/ajax.php';
	var params = 'function=do_search&q='+q+'&target='+target;
	var ajax = new Ajax.Request(url, {method: 'get', parameters: params, onComplete: showResponse});
}

function filterClasses(element, target_id, func, comp){
	target = target_id;
	dog = element.value;
	var params = 'function='+func+"&compID="+comp+"&dogID="+dog;
	var url = '/includes/ajax.php';
	var ajax = new Ajax.Request(url, {method: 'get', parameters: params, onLoading: showLoad, onComplete: showResponse});
}

function editComp(element, target_id, func){
	target = target_id;
	comp = element.value;
	var params = 'function='+func+"&compID="+comp;
	var url = '/includes/ajax.php';
	var ajax = new Ajax.Request(url, {method: 'get', parameters: params, onLoading: showLoad, onComplete: showResponse});
}

function editDog(target_id, func, dog){
	target = target_id+'_'+dog;
	var rows = document.getElementsByTagName('tr');
	for(i=0; i<rows.length; i++){
		var name = rows[i].getAttribute("name");
		if (name == 'edit_row'){
			rows[i].style.display = 'none';
		}
	}
	var params = 'function='+func+'&dogID='+dog;
	var url = '/includes/ajax.php';
	//var ajax = new Ajax.Request(url, {method: 'get', parameters: params, onLoading: showLoad});
	var ajax = new Ajax.Request(url, {method: 'get', parameters: params, onLoading: showLoad, onComplete: showResponse2});
}

function showLoad(){
	document.body.style.cursor = 'wait';
}

function showResponse(request){
	document.body.style.cursor = 'default';
	$(target).innerHTML = request.responseText;
}

function showResponse2(request){
	document.body.style.cursor = 'default';
	document.getElementById(target).innerHTML = request.responseText;
	document.getElementById('row_'+target).style.display = '';
}


/* Other Functions */

function clearText(thefield) { 
	if (thefield.defaultValue == thefield.value) 
		thefield.value = "";
	else 
		thefield.value = thefield.defaultValue;
}


function checkGradeChange(newGradeObj, oldGradeObj) {
	//newGrade is a HTMLSelectElement Object
	//oldGrade is a HTMLInputElement Object
	
	oldGrade = oldGradeObj.value;
	newGrade = newGradeObj.selectedIndex;
	
	if (oldGrade == newGrade){
		return;
	}
	
	alert("You have changed your dog's grade.\n\nPlease make sure you edit any shows that you have entered that have not yet closed.\n\nPlease also inform L&N Show Services (info@lnshowservices.com) so they can update your other entries.");
	
	return;
}
