function javascriptCheck(elementId){
	document.getElementById(elementId).style.display='none';
}

function displayChange(elementId,state,imageId,image1,image2){
	var element = document.getElementById(elementId);
	var image = document.getElementById(imageId);
	image.src = image.src == image1?image2:image1;

	if(state !== ''){
		element.style.display = state?'none':'inline';
	}else{
		if(element.style.display == 'none'){
			element.style.display = 'inline';
		}else{
			element.style.display = 'none';
		}
	}
	return false;
}

function CreateEmailAddress(address1, address2, address3, subject, message) {
	var x = address1 + '&' + address2 + '*' + address3;
	var y = 'mai';
	var z = 'lto';
	var s = '?subject=' + subject;
	var m = '&body=' + message;
	var n = '';
	var o = '';
	x = x.replace('&','@');
	x = x.replace('*','.');
	x = x.replace('_','');
	x = x.replace('_','');

	var b = y + z +':'+ x + s + m + n + o;
	window.location=b;
}

/* Set cookies
Don't forget to put in empty quotes for the unused parameters or you'll get an error when you run the code.

Use 'domain' on the Javascript cookie if you are using it on a subdomain, like widgets.yoursite.com, where
the cookie is set on the widgets subdomain, but you need it to be accessible over the whole yoursite.com domain.

It's good practice to not assume the path to the site root will be set the way you want it by default, so do
this manually as a rule, '/'. If no value is set for expires, it will only last as long as the current session
of the visitor, and will be automatically deleted when they close their browser. 
*/
function setCookie(name, value, expires, path, domain){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	if (expires){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + "=" +escape(value) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "") +
	((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "");
}
// this function gets the cookie, if it exists
function getCookie(name){
	var start = document.cookie.indexOf(name + "=" );
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))){
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}
// this deletes the cookie when called
function Delete_Cookie(name, path, domain){
	if (getCookie(name)){
		document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function checkForm(form,currentPassword,username,password1,password2,outputID){
	if(currentPassword != null) {
		if(document.getElementById(currentPassword).value == "") {
			document.getElementById(outputID).innerHTML="Please enter your current password.";
			document.getElementById(currentPassword).focus();
			return false;
		}
	}
	if(username != null) {
		if(document.getElementById(username).value == "") {
			document.getElementById(outputID).innerHTML="Please enter a username.";
			document.getElementById(username).focus();
			return false;
		}
		if(document.getElementById(username).value.length < 5){
			document.getElementById(outputID).innerHTML="Username cannot be less than 5 characters.";
			document.getElementById(username).focus();
			return false;
		}
		if(document.getElementById(username).value.length > 20){
			document.getElementById(outputID).innerHTML="Username cannot be longer than 20 characters.";
			document.getElementById(username).focus();
			return false;
		}
		var re = /^\w+$/;
		if(!re.test(document.getElementById(username).value)) {
			document.getElementById(outputID).innerHTML="Username must contain only letters, numbers and underscores.";
			document.getElementById(username).focus();
			return false;
		}
		if(password2 != null) {
			if(document.getElementById(password1).value.length < 8) {
					document.getElementById(outputID).innerHTML="Password must contain at least eight characters.";
					document.getElementById(password1).focus();
					return false;
			}
			if(document.getElementById(password1).value != "" && document.getElementById(password1).value == document.getElementById(password2).value) {
				if(document.getElementById(password1).value == document.getElementById(username).value) {
					document.getElementById(outputID).innerHTML="Password must be different from Username.";
					document.getElementById(password1).focus();
					return false;
				}
				/*re = /[0-9]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one number (0-9)!");
				form.pwd1.focus();
				return false;
				}
				re = /[a-z]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one lowercase letter (a-z)!");
				form.pwd1.focus();
				return false;
				}
				re = /[A-Z]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one uppercase letter (A-Z)!");
				form.pwd1.focus();
				return false;
				}*/
			}
			else {
				document.getElementById(outputID).innerHTML="Please re-type your password.";
				document.getElementById(password2).focus();
				return false;
			}
		}
		else {
			if(document.getElementById(password1).value != "") {
				if(document.getElementById(password1).value.length < 8) {
					document.getElementById(outputID).innerHTML="Password must contain at least eight characters.";
					document.getElementById(password1).focus();
					return false;
				}
				/*re = /[0-9]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one number (0-9)!");
				form.pwd1.focus();
				return false;
				}
				re = /[a-z]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one lowercase letter (a-z)!");
				form.pwd1.focus();
				return false;
				}
				re = /[A-Z]/;
				if(!re.test(form.pwd1.value)) {
				alert("Error: password must contain at least one uppercase letter (A-Z)!");
				form.pwd1.focus();
				return false;
				}*/
			}
			else {
				document.getElementById(outputID).innerHTML="Please check your password.";
				document.getElementById(password1).focus();
				return false;
			}
		}
	}
	document.getElementById(outputID).innerHTML="Working...";
	return true;
}


var formInUse = false;

function setFocus(formID) {
	if(!formInUse) {
		document.getElementById(formID).focus();
	}
}

/*
Finds and returns a .Net control by ID and tagName.  We don't know the exact ID because
.Net renames the ID's, so this searches for it.
Parameters:
	findID - the control's ID
	tagName- the type of tag to look for, like "div", "table", etc.
*/
function FindControl(findID, tagName) {
	var elementList = document.getElementsByTagName(tagName);
	
	for (var i=0; i < elementList.length; i++) {
		var element = elementList[i];
		
		if (element.id.indexOf(findID) != -1)
			return element;
	}
	
	return null;
}

/*
For checkboxes that have an image next to them instead of text, this hooks 
up the <label> type functionality for Internet Explorer, which doesn't work 
properly with images.
*/
function CheckboxImageLabel(checkboxID) {
	var checkbox = FindControl(checkboxID, 'input');
	checkbox.checked = !checkbox.checked;
}


/*
Positions an element below another element, with X and Y offsets.
Parameters:
	elementID  - the element or the element's ID
	attachToID - the element to attach to, or it's ID
	offsetX    - an amount in pixels to offset the left position
	offsetY    - an amount in pixels to offset the top position
*/
function PositionBelow(attachToID, elementID, offsetX, offsetY) {
	var attachTo = GetElementByIdOrReference(attachToID);
	var element = GetElementByIdOrReference(elementID);
		
	//Get the initial left and top positions for the attachTo element, relative to it's parent.
	var topValue = attachTo.offsetTop + attachTo.offsetHeight;
	var leftValue = attachTo.offsetLeft;
	
	//Calculate the absolute position of the attachTo element relative to the client area.
	while((attachTo = attachTo.offsetParent) != null) {
		topValue += attachTo.offsetTop;
		leftValue += attachTo.offsetLeft;
	}
	
	//Position the element.
	element.style.position = 'absolute';
	element.style.left = leftValue + offsetX + 'px';
	element.style.top = topValue + offsetY + 'px';
}



/*
Inserts an element immediately after another element.
*/
function InsertAfter(attachToID, elementID) {
	var attachTo = GetElementByIdOrReference(attachToID);
	var element = GetElementByIdOrReference(elementID);
	
	var parentElement = attachTo.parentNode;
	var nextSibling = GetNextSibling(attachTo);
	
	if (nextSibling) {
		parentElement.insertBefore(element, nextSibling);
	}
	else {
		parentElement.appendChild(element);
	}
}

function GetNextSibling(element) {
	if (element.nextSibling) {
		var sib = element.nextSibling;
		while (sib.nodeType != 1) {
			if (sib.nextSibling) {
				sib = sib.nextSibling;
			} 
			else {
				return false;
			}
		} 
		
		return sib;
	} 
	
	return false;
}


/*
UTILITY FUNCTIONS
*/

//If idOrElement is an ID of an element, get a the element and return it.  If it's 
//already an element, return that.
function GetElementByIdOrReference(idOrElement) {
	if (typeof(idOrElement) == "string") {
		return document.getElementById(idOrElement);
	}
	
	return idOrElement;
}

function copyToClipboard(elt) {// Requires: prototype.js, scriptaculous.js?load=effects
	var urlSwf = "/include/swf/_clipboard.swf";
	var strMssgBoxId = "notifyTextCopied";
	var eltNotify = null;

	// Display Notifications
	if((eltNotify = $(strMssgBoxId)) == null){
		// Attach the notification to the DOM
		var eltBody = document.getElementsByTagName('body').item(0);

		eltNotify = document.createElement('div');
		eltNotify.setAttribute('id', strMssgBoxId);
		eltNotify.style.display = 'none';

		eltNotify.innerHTML = 'Copied';

		eltBody.appendChild(eltNotify);
	}
	elt.onblur =
		function(e){
			Element.hide(eltNotify);
			return true;
		}

	var z = Position.cumulativeOffset(elt);
	var x = z[0];
	var y = z[1];

	Element.show(eltNotify);

	if(navigator.appName == 'Microsoft Internet Explorer'){
		if(x < 100){
			eltNotify.style.left = (x + (elt.offsetWidth - 23)) + 'px';
		}
		else{
			eltNotify.style.left = (x - (eltNotify.offsetWidth + 2)) + 'px';
		}
	}
	else{
		if(x < 100){
			eltNotify.style.left = (x + (elt.offsetWidth + 3)) + 'px';
		}
		else{
			eltNotify.style.left = (x - (eltNotify.offsetWidth + 2)) + 'px';
		}
	}

	eltNotify.style.top = y + 'px';

	var xEffect = Effect.Fade(eltNotify, { fps: 75, from: 1.9, to: 0.0, duration: 1.0, queue: 'front' } );
	window.status = 'Copied text to clipboard';

	// Copy the text inside the text box to the user's clipboard
	var flashcopier = 'flashcopier';
	if(!$(flashcopier)){
		var divholder = document.createElement('div');
		divholder.id = flashcopier;
		document.body.appendChild(divholder);
	}

	$(flashcopier).innerHTML = '';
	var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard='+escape(elt.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
	$(flashcopier).innerHTML = divinfo;

	elt.select();

	return true;
}

function printArray(array,arrayName){
	var temp = '<table border="1" cellpadding="0" cellspacing="0"><tr><td align="center" colspan="2" style="border-right:solid 0px; border-bottom:solid 1px">'
	+arrayName+'</td></tr>';
	for (key in array) {
		temp += '<tr><td align="right" width="30%" style="border-right:solid 0px; border-bottom:solid 0px; border-top:solid 0px">'+key+
		': </td><td align="left" width="70%" style="border-left:solid 0px; border-bottom:solid 0px; border-top:solid 0px">'+array[key]+
		'</td></tr>';
	}
	temp += '</table>';
	/*for(var i=0; i<array.length; i++) {
		temp += i+": "+array[i]+'<br />';
	}*/
	temp += '<br />';
	return temp;
}


