/* This changes the big photo on the project detail page */
function changeBigPhoto (image) {
	document.getElementById("bigphoto").src = image;
}

/* Create an error, used in form checking*/
function onError(obj,message) {
	alert(message);
    obj.focus();
	return false;	
    }		

function isNull(obj) {
	return !obj.value.replace(/^\s+/g, '').replace(/\s+$/g, '').length;
}

function isChecked(obj) {
	for (i=0; i<obj.length; i++)
		{
		if (obj[i].checked) return true;
		}
	return false;	
}
	
function isSelected(obj) {
	return (obj.selectedIndex > 0);
}
	
function isEmail(sEmailObj) {
	sEmail = sEmailObj.value;
	
	if (sEmail.indexOf ('@', 0) < 1)
		{
		return false;
		}
	else
		{
		if (sEmail.indexOf ('.', sEmail.indexOf ('@', 0)) < (sEmail.indexOf ('@', 0) + 3))
			{
			return false;
			}
		if (sEmail.lastIndexOf(".") == (sEmail.length - 1))
			{
			return false;
			}
		}
	return true;
}

function clear_all_files(){
	var fileids = document.getElementsByName('fileid');
	for(var i=0;i < fileids.length; i++){
		fileids[i].checked = false;
	}
}

function validate_file_selected() {
	var valid = false;
	var fileids = document.getElementsByName('fileid');
	for(var i=0;i < fileids.length; i++) {
		if (fileids[i].checked == true) {
			valid = true;
		}
	}
	
	if (valid == false) {
		alert('Please select some files.');
	}
	
	return valid;
}

/* Primitive checking to see if a validation code is in the correct format */
function isValid(sValidObj)	{
	sValid = sValidObj.value
	
	if (sValid.length != 36)
		{
		return false;
		}
	else
		{
		if (sValid.charAt(8) != '-' || sValid.charAt(13) != '-' || sValid.charAt(18) != '-' || sValid.charAt(23) != '-')
			{
			return false;
			}		
		}
	
	return true;
}

/* DMS functions */

// generic function which toggles tables content display/none

function toggleTableContent(fieldValue,table,field) {		

	if (document.getElementById(field).value == fieldValue) {
		document.getElementById(table).style.display = 'block';
		document.getElementById(table).style.display = 'block';
	}
	else {
		document.getElementById(table).style.display = 'none';
		document.getElementById(table).style.display = 'none';
	}	
}

function changedate (fieldid, numMonths) {
	var d = new Date();
	d.setMonth( d.getMonth() + parseInt(numMonths) );
	d.setDate( d.getDate() - 1 );

	// Format for output in form field
	var dd = d.getDate();
	var mm = (d.getMonth() + 1);
	var yyyy = d.getFullYear();

	//need a leading zero if the date is less than 10
	if (dd>= 1 && dd < 10) {
		dd = '0'+dd;
	}

	//need a leading zero if the date is less than 10
	if (mm>= 1 && mm < 10) {
		mm = '0'+mm;
	}

	//set form field output to the newly calculated date
	return document.getElementById(fieldid).value = yyyy+'-'+mm+'-'+dd+' 23:59:59';
}