// script to get the time and date for display on the page
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var time = new Date();
var hour = time.getHours();
var mins = time.getMinutes();
var secs = time.getSeconds();
var minString = mins.toString()
var minCorrect = '';
var digit = /\d{2}/
if (digit.test(minString) == false) minCorrect='0' + minString;
else minCorrect = minString;
var lmonth = months[time.getMonth()];
var date = time.getDate();
var year = time.getYear();
if (year < 2000) year = year + 1900;


function getParams() {
	var pLocation = " "+document.location;
	var idx = pLocation.indexOf('?');
	var params = new Array();
	if (idx != -1) {
		var pairs = pLocation.substring(idx+1, pLocation.length).split('&');
		for (var i=0; i<pairs.length; i++) {
			nameVal = pairs[i].split('=');
			params[nameVal[0]] = nameVal[1];
	   }
	}
	return params;
}

// variable to store the number of the open row
var theOpenRow = "";

function showRow(rowNum) {
	var rowItem = document.getElementById('row_' + rowNum);
	var rowDetail = document.getElementById('detail_' + rowNum);
	var openRowDetail = document.getElementById('detail_' + theOpenRow);
	// if we are opening the row
	if (rowDetail.style.visibility == "hidden" ) {
		// close open row if necessary
		if (theOpenRow != "") {
			openRowDetail.style.visibility = "hidden"; 
			openRowDetail.style.display = "none"; 
			document.getElementById('row_' + theOpenRow).className = ""; 
		}
		// open the row
		rowDetail.style.visibility = "visible";
		rowDetail.style.display = "block";
		rowItem.className='ListItemHover';
		// set the open row variable
		theOpenRow = rowNum;
	}
	// else we are closing the row
	else {
		// set the open row to nothing
		theOpenRow = "";
		// close the row
		rowDetail.style.visibility = "hidden";
		rowDetail.style.display = "none";
		rowItem.className=''; 
	}
}

function saveNote(rowNum) {
	var theNoteField = document.getElementById('note_field_'+rowNum);
	document.noteForm.action = '/oaa/ppr#'+rowNum;
	document.noteForm.pcrRowId.value = rowNum;
	document.noteForm.note.value = theNoteField.value;
	document.noteForm.submit();
}

function editNote(rowNum) {
	// set last note value
	document.getElementById('last_note_value_'+rowNum).value = document.getElementById('note_field_'+rowNum).value;
	// enable text area
	document.getElementById('note_field_'+rowNum).className = "editableNote";
	document.getElementById('note_field_'+rowNum).readOnly = false;
	// change buttons
	document.getElementById('note_button_'+rowNum).disabled = true;
	document.getElementById('note_button_'+rowNum).style.cursor = "";
	document.getElementById('save_note_button_'+rowNum).disabled = false;
	document.getElementById('save_note_button_'+rowNum).style.cursor = "hand";
	document.getElementById('cancel_note_button_'+rowNum).disabled = false;
	document.getElementById('cancel_note_button_'+rowNum).style.cursor = "hand";
}

function cancelNote(rowNum){
	// disable text area
	document.getElementById('note_field_'+rowNum).className = "savedNote";
	// reset text
	document.getElementById('note_field_'+rowNum).value = document.getElementById('last_note_value_'+rowNum).value;
	// change buttons
	document.getElementById('note_button_'+rowNum).disabled = false;
	document.getElementById('save_note_button_'+rowNum).disabled = true;
	document.getElementById('save_note_button_'+rowNum).style.cursor = "";
	document.getElementById('cancel_note_button_'+rowNum).disabled = true;
	document.getElementById('cancel_note_button_'+rowNum).style.cursor = "";
}

function saveStaticNote(rowNum){
	// set hidden hasNote field
	document.getElementById('hasNote').value = true;
	// set hidden note field
	document.getElementById('hidden_note_'+rowNum).value = document.getElementById('note_field_'+rowNum).value;
	// disable text area
	document.getElementById('note_field_'+rowNum).className = "savedNote";
	document.getElementById('note_field_'+rowNum).readOnly = true;
	// change buttons
	document.getElementById('note_button_'+rowNum).value = "Edit Note";
	document.getElementById('note_button_'+rowNum).disabled = false;
	document.getElementById('note_button_'+rowNum).style.cursor = "hand";
	document.getElementById('save_note_button_'+rowNum).disabled = true;
	document.getElementById('save_note_button_'+rowNum).style.cursor = "";
	document.getElementById('cancel_note_button_'+rowNum).disabled = true;
	document.getElementById('cancel_note_button_'+rowNum).style.cursor = "";
}

function isUTF8(){
	// checks all fields contain valid UTF-8 characters
	var UTF8chars = " !\"#£$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\n\r	";
	var input = document.getElementsByTagName('input');
	var textarea = document.getElementsByTagName('textarea');
	// check all text fields contain only UTF-8 characters
	if ( input != null ){
		for ( var i=0; i<input.length; i++ ){
			if( input[i].type == "text"	){
				for ( var c=0; c<input[i].value.length; c++ ){
					if (UTF8chars.indexOf(input[i].value.charAt(c)) == -1 ){
						alert("The character ' " + input[i].value.charAt(c) + " ' is not allowed!");
						input[i].focus();
						input[i].select();
						return false;
					}
				}
			}
		}
	}
	// now check all textareas contain only UTF-8 characters
	if ( textarea != null ){
		for ( var t=0; t<textarea.length; t++ ){
			for ( var v=0; v<textarea[t].value.length; v++ ){
				if (UTF8chars.indexOf(textarea[t].value.charAt(v)) == -1 ){
					alert("The character ' " + textarea[t].value.charAt(v) + " ' is not allowed!");
					textarea[t].focus();
					textarea[t].select();
					return false;
				}
			}
		}
	}
	return true;
}

function validateTxt(theField) {
	if (theField.value.length > 2000) {
		theField.value = theField.value.substr(0,2000);
		alert("Notes field cannot contain more than 2000 characters");
		theField.focus();
	}
}


var layerShowingName = "";

function showExceptionDetails(layerName) {
	// hide visible exception layer (if there is one)
	if (layerShowingName != "" && document.getElementById(layerShowingName)) {
		document.getElementById(layerShowingName).style.visibility = "hidden";
	}
	if (layerName == layerShowingName) {
		layerShowingName = "";
		return;
	}
	// show exception layer
	document.getElementById(layerName).style.visibility = "visible";
	layerShowingName = layerName;
}



