// ------ //
// Images //
// ------ //
function mouseOver(id,name,add){
	path = document.getElementById(id).src;
	pathArray = path.split("/");
	pathLength = pathArray.length;
	
	var newPath = 'http:';
	
	for(i = 1; i < pathLength; i++){
		if(i < (pathLength - 1)){
			newPath = newPath + '/' + pathArray[i];
		}
		else{
			newPath = newPath + '/' + name + '_' + add + '.gif';
		}
	}
	
	document.getElementById(id).src = newPath;
}

function replaceImage(id, img1, img2){
	path = document.getElementById(id).src;
	pathArray = path.split("/");
	pathLength = pathArray.length;
	//alert(pathArray[(pathLength-1)]);
	
	var newPath = 'http:';
	
	for(i = 1; i < pathLength; i++){
		if(i < (pathLength - 1)){
			newPath += '/' + pathArray[i];
		}
		else if(pathArray[(pathLength-1)] == img1){
			newPath += '/' + img2;
		}
		else{
			newPath += '/' + img1;
		}
	}
	
	document.getElementById(id).src = newPath;
}

// ---------- //
// navigation //
// ---------- //
function openMenu(id){
	element = document.getElementById(id);
	element.style.display = 'block';	
}

function closeMenu(id){
	element = document.getElementById(id);
	element.style.display = 'none';
}

// ----- //
// Forms //
// ----- //
function focusElement(thisForm, thisElement, thisLength, focusTo){
	if(thisElement != '' && thisLength != ''){
		if(thisForm[thisElement].value.length == thisLength){
			thisForm[focusTo].focus();
		}
	}
	else{
		thisForm[focusTo].focus();
	}
}

function disableElement(thisForm, disableElement){
	thisForm[disableElement].disabled = true;
	
}

function enableElement(thisForm, enableElement){
	thisForm[enableElement].disabled = false;
}

function fillElement(thisForm, emptyElement, string){
	if(thisForm[emptyElement].value.length == 0){
		thisForm[emptyElement].value = string;
	}
	else{
		return;
	}
}

function selectElement(thisForm, enableElement){
	thisForm[enableElement].checked = true;
}

function validateAlphaNumerique(thisForm, thisElement, type){
	var inputString = thisForm[thisElement].value;
	var outputString = '';
	var notAlpha = false;
	
	if(type == 'alpha'){
		var regex = /[A-Z]|[a-z]/;
	}
	else if(type == 'number'){
		var regex = /[0-9]/;
	}
	
	for(i=0;i<=inputString.length;i++){
		if(!regex.test(inputString.charAt(i))){
			notAlpha = true;
		}
		else{
			outputString += inputString.charAt(i);
		}
	}
	
	if(notAlpha == true){
		thisForm[thisElement].value = outputString;
	}
}

function validateTelephone(thisForm, thisElement){
	var inputString = thisForm[thisElement].value;
	var outputString = '';
	var badChar = false;
	
	var regex = /[0-9]|[-]|[(]|[)]|[ ]/;
	
	for(i=0;i<=inputString.length;i++){
		if(!regex.test(inputString.charAt(i))){
			badChar = true;
		}
		else{
			outputString += inputString.charAt(i);
		}
	}
	
	if(badChar == true){
		thisForm[thisElement].value = outputString;
	}
}

function showElement(thisForm, element){
	document.getElementById(element).style.display = 'block';
}

function hideElement(thisForm, element){
	document.getElementById(element).style.display = 'none';
}

function showHideElement(thisForm, element){
	var value = document.getElementById(element).style.display;
	if(value == 'none'){
		document.getElementById(element).style.display = 'block';
	}
	else{
		document.getElementById(element).style.display = 'none';
	}
}

function submitForm(thisForm){
	thisForm.submit();
}


// Error handling
function emptyTextInput(thisForm, thisElement, errorElement, msg){
	if(thisForm[thisElement].value.length == 0){
		document.getElementById(errorElement).innerHTML = "* " + msg;
		return false;
	}
	else{
		document.getElementById(errorElement).innerHTML = "";
		return true;
	}
}

function emptyMultipleTextInput(thisForm, elements, errorElement, msg){
	var elementArray = elements.split(",");
	var error = false;
	
	for(i=0;i<elementArray.length;i++){
		if(thisForm[elementArray[i]].value.length == 0){
			error = true;
		}
	}
	
	if(error == true){
		document.getElementById(errorElement).innerHTML = "* " + msg;
		return false;
	}
	else{
		document.getElementById(errorElement).innerHTML = "";
		return true;
	}
}

function emptyRadioInput(thisForm, thisElement, errorElement, msg){
	var isEmpty = true;
	
	for(i=0;i<thisForm[thisElement].length;i++){
		if(thisForm[thisElement][i].checked == true){
			isEmpty = false;	
		}
	}
	
	if(isEmpty == true){
		document.getElementById(errorElement).innerHTML = "* " + msg;
		return false;
	}
	else{
		document.getElementById(errorElement).innerHTML = "";
		return true;
	}	
}

function emptyCheckboxInput(thisForm, elements, errorElement, msg){
	var elementArray = elements.split(",");
	
	var isEmpty = true;
	
	for(i=0;i<elementArray.length;i++){
		if(thisForm[elementArray[i]].checked == true){
			isEmpty = false;
		}
	}
	
	if(isEmpty == true){
		document.getElementById(errorElement).innerHTML = "* " + msg;
		return false;
	}
	else{
		document.getElementById(errorElement).innerHTML = "";
		return true;
	}	
}

function emptyPulldown(thisForm, thisElement, errorElement, emptyOption, msg){
	if(thisForm[thisElement].value == emptyOption){
		document.getElementById(errorElement).innerHTML = "* " + msg;
		return false;
	}
}

