// Generic URL variable (needs updating/retiring)
var thispage = location.href;

// Random ID generator (useful for creating multiple unique object instances, i.e. pop-up windows)
function randomID() {
	
	var randomID = "";
	var idLength = 8;
	var validChars = "abcdefghijklmnopqrstuvwxyz1234567890";
	
	for (i = 1; i <= idLength; i++) {
		randomID += validChars.charAt(Math.floor(Math.random() * validChars.length));
	}
	
	return randomID;
	
}

// Opens the passed URL in a smaller pop-up window
function popUp(url) {
	window.open(url, "terms", "height=512,width=384,directories=no,location=no,menubar=no," + "resizeable=no,scrollbars=yes,status=no,toolbar=no");
	return false;
}

// String length checker/indicator
function textCounter(field, countfield, maxlimit) {

	var userText = document.getElementById(field);
	var textLimit = document.getElementById(countfield);

	if (userText.value.length > maxlimit) { // if too long...trim it!
		userText.value = userText.value.substring(0, maxlimit);
	} else { // otherwise, update 'characters left' counter
		textLimit.value = (maxlimit - userText.value.length);
	}
}

// function to initialise the textCounter on loading of a page
function initCount(sourceID, targetID, maxLength) {
	
	var src = document.getElementById(sourceID);
	var len = document.getElementById(targetID);
	
	// alert(src.value.length);
	
	len.value = (maxLength - src.value.length);
}

// Script to refresh page (e.g. after file attachment )
function refresh() {
    window.location.reload();
}

// This function opens a new window with the specified URL.
function showWindow(url) {
	window.open(url, "photo","height=480,width=640,directories=yes,location=yes,menubar=yes," + "scrollbars=yes,status=yes,toolbar=yes");
	return false;
}

// Opens a custom pop-up window
function openit() {
	
	var cwidth="";
	var cheight="";
	var curl="";
	var popup="";
	
    if (popup.open){
        popup.close();
    }
    
    popup = window.open(curl,'Child','height='+cheight+',width='+cwidth+',directories=0,location=0,menubar=0,resizable=0,scrollbars=1,status=0,toolbars=0');
	
}

// Email address validation
function validatetester() {
	
	var flag = true;
	var string1=document.tester._Email.value;
	var thealert="Please input a valid email address!";
	var thealert2="You have already submitted this form - please wait for it to be processed";
	var tester = document.forms.tester;
	
	if (tester.Submit.value=="Processing") {
		alert(thealert2);
		flag = false;
	}
	
	if ((string1.indexOf("@")==-1)||(string1.indexOf(".")==-1)) {
		alert(thealert);
		document.tester.Submit.value="Submit";
		flag = false;
	}
	
	if (flag) {
		document.tester.Sender.value=document.tester._Email.value;
		document.forms.tester.submit();
	}
	
}

// External website opener (spawns new window)
function openExternal(url) {
	
	var todaysDate = new Date();
	var windowID = randomID();
	
	window.open(url,windowID);
	
	return false;
	
}

// Check the value of the passed ID
function checkValue(id) {
	var obj = document.getElementById(id);
	if (obj.value == '') {
		alert('Sorry -- you have either not made a valid selection\nor not entered a required value.\n\nPlease try again.');
		return false;
	} else {
		return true;
	}
}

// Passes the value of one object to another
function passValue(from, to) {
	
	var source = document.getElementById(from);
	var target = document.getElementById(to);
	
	target.value = source.value;

}

// Enables or disables form elements using DOM scripting to add or remove the "disabled" attribute as appropriate
function enableDisable(id) {
	
	var obj = document.getElementById(id);
	
	if (!obj.hasAttribute)
	{
		//this is IE or another browser that cannot handle the DOM approach
		//alert('this is IE');
		return false;
	}
	else
	{
		//this is a browser capable of the DOM approach
		if (obj.hasAttribute("disabled")) {
			obj.removeAttribute("disabled");
		} else {
			obj.setAttribute("disabled", "disabled");
		}
	}
	
}

// Function to update the value of a target element identified by its ID
function updateTargetValue(targetID, newValue) {
	
	var target = document.getElementById(targetID);
	
	target.value = newValue;
	
}

// Modifies the DOM dynamically (based on element ID)
function modifyDOM(id, attr, val) {
	
	var obj = document.getElementById(id);
	
	obj.setAttribute(attr, val);
	
}

// Modifies the inner HTML of an (ID'd) element dynamically
function modifyInnerHTML(id, val) {
	
	var obj = document.getElementById(id);
	
	obj.innerHTML = val;
	
}