
// the following functions are for forms that have inputs with initial values of their own names
// on entering the field it clears the value, for the user to type
// if they don't type a value it resets to its original value (and colour)

// class names to apply to inputs 
var class_when_users_value = "input_when_users_value";
var class_when_label_value = "input_when_label_value";

// automatically save the orginal values in each input - name/value pairs where name is the ID of the input
var storeValues = '';

function clearInput(input) {
	var findLocation = storeValues.indexOf(input.id);
	if (findLocation < 0) {
		findLocation = storeValues.length;
		//store the original value
		storeValues += input.id + '|' + input.value + '|';
	}
	// if the value stored = the value now, then clear it - else leave it
	var startSpot = storeValues.indexOf('|', findLocation) + 1
	if(storeValues.substr(startSpot, input.value.length) == input.value) {
		input.value = '';
		input.className = SetClassName(input.className, class_when_users_value);
	}
}

function checkInput(input) {
	// if the value is blank then reset it to the original value
	if (input.value == '') {
		var findLocation = storeValues.indexOf(input.id);
		var startSpot = findLocation + input.id.length + 1;
		var endSpot = storeValues.indexOf('|', startSpot)
		input.value = storeValues.substring(startSpot, endSpot)
		input.className = SetClassName(input.className, class_when_label_value);
	}
}

function SetClassName(existingClass, newClassName) {
	// clears out the class names above if they are in there
	var resultString = existingClass;
	
	var re1 = new RegExp("[ ]?" + class_when_users_value, "g");
	resultString = resultString.replace(re1, "");

	var re2 = new RegExp("[ ]?" + class_when_label_value, "g");
	resultString = resultString.replace(re2, "");
		
	resultString += " " + newClassName;
	return resultString;
}

function KeepUsersLoggedIn(siteRoot) {
	$.get(siteRoot + 'services/keepSession.aspx', { "r": Math.random });
	window.setTimeout(function () { KeepUsersLoggedIn(siteRoot) }, 600000); // 10 mins
}

