/* Upload Progress Bar */
var HttpRequestObject = false;
if(window.XMLHttpRequest)
	{
	HttpRequestObject = new XMLHttpRequest();
	}
else if(window.ActiveXObject)
	{
	HttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
function startProgress(uid)
	{
	document.getElementById('upload').target = 'upload_frame';
	document.getElementById('upload').style.display = 'none';
	document.getElementById('progressbase').style.display = 'block';
	setTimeout('getProgress("' + uid + '")', 1000);
	}
function getProgress(uid)
{
if(HttpRequestObject)
	{
	HttpRequestObject.open('GET', 'inc/uploadprogress.php?uid=' + uid, true);
	HttpRequestObject.onreadystatechange = function()
		{
		if(HttpRequestObject.readyState == 4 && HttpRequestObject.status == 200)
			{
			var progress = eval('('+HttpRequestObject.responseText+')');
			var percent = Math.round(progress.current/progress.total*100);
			document.getElementById('progresspercent').style.width = percent + '%';
			document.getElementById('progresspercent').innerHTML = percent + '%';
			if(percent < 100)
				{
				setTimeout('getProgress("' + uid + '")', 5500);
				}
			else
				{
				document.getElementById('progressbase').style.display = 'none';
				document.getElementById('upload').style.display = 'none';
				document.getElementById('upload_frame').style.display = '';
				}
			}
		}
	HttpRequestObject.send(null);
	}
}

/*Form Validation */
function validateFormOnSubmit(theForm)
	{
	var reason = "";
	reason += validateName(theForm.fName);
	reason += validateEmail(theForm.email);
	reason += validateCompany(theForm.company);
	reason += validatePhone(theForm.phone);
	if (reason != "")
		{
		document.getElementById('send').setAttribute("id", "send-disabled");
		document.getElementById('send-disabled').disabled=true;
		return false;
		}
	else
		{
		document.getElementById('send-disabled').setAttribute("id", "send");
		document.getElementById('send').disabled=false;
		}
	return true;
	}
	
function validateName(fld)
	{
	var error = "";
	if (fld.value == "")
		{
		fld.setAttribute("class", "invalid");
		error = "Please enter a name.\n";
		document.getElementById('fNameNotice').style.display = '';
		document.getElementById('fNameNotice').innerHTML = error;
		}
	else if ((fld.value.length < 2) || (fld.value.length > 30))
		{
		fld.setAttribute("class", "invalid");
		error = "The name is the wrong length.\n";
		document.getElementById('fNameNotice').style.display = '';
		document.getElementById('fNameNotice').innerHTML = error;
		}
	else
		{
		fld.setAttribute("class", "");
		document.getElementById('fNameNotice').style.display = 'none';
		}
	return error;
	}	
	
function validateCompany(fld)
	{
	var error = "";
	if (fld.value == "")
		{
		fld.setAttribute("class", "invalid");
		error = "Please enter a company name.\n";
		document.getElementById('companyNotice').style.display = '';
		document.getElementById('companyNotice').innerHTML = error;
		}
	else if ((fld.value.length < 2) || (fld.value.length > 55))
		{
		fld.setAttribute("class", "invalid");
		error = "Company Name is the wrong length.\n";
		document.getElementById('companyNotice').style.display = '';
		document.getElementById('companyNotice').innerHTML = error;
		}
	else
		{
		fld.setAttribute("class", "");
		document.getElementById('companyNotice').style.display = 'none';
		}
	return error;
	}
	
function validatePhone(fld)
	{
	var error = "";
	if (fld.value == "")
		{
		fld.setAttribute("class", "invalid");
		error = "Please enter a phone number.\n";
		document.getElementById('phoneNotice').style.display = '';
		document.getElementById('phoneNotice').innerHTML = error;
		}
	else if ((fld.value.length < 7) || (fld.value.length > 25))
		{
		fld.setAttribute("class", "invalid");
		error = "Phone number is the wrong length.\n";
		document.getElementById('phoneNotice').style.display = '';
		document.getElementById('phoneNotice').innerHTML = error;
		}
	else
		{
		fld.setAttribute("class", "");
		document.getElementById('phoneNotice').style.display = 'none';
		}
	return error;
	}
	
	
function trim(s)
	{
	return s.replace(/^\s+|\s+$/, '');
	}
function validateEmail(fld)
	{
	var error="";
	var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	if (fld.value == "")
		{
		fld.setAttribute("class", "invalid");
		error = "Please enter an email address.\n";
		document.getElementById('emailNotice').style.display = '';
		document.getElementById('emailNotice').innerHTML = error;
		}
	else if (!emailFilter.test(tfld))
		{
		fld.setAttribute("class", "invalid");
		error = "Please enter a valid email address.\n";
		document.getElementById('emailNotice').style.display = '';
		document.getElementById('emailNotice').innerHTML = error;
		}
	else if (fld.value.match(illegalChars))
		{
		fld.setAttribute("class", "invalid");
		error = "The email address contains illegal characters.\n";
		document.getElementById('emailNotice').style.display = '';
		document.getElementById('emailNotice').innerHTML = error;
		}
	else
		{
		fld.setAttribute("class", "");
		document.getElementById('emailNotice').style.display = 'none';
		}
	return error;
	}

/* Convert a single file-input element into a 'multiple' input list - Created by Stickman - http://www.the-stickman.com */
function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			//element.name = 'userfile' + this.id++;
			element.name = 'userfile[]';

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';
				new_element.size = "10"

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
				document.getElementById('browse').setAttribute("id", "browse-disabled");
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );
		new_row.className = 'attachment';

		// Delete button
		var new_row_button = document.createElement( 'a' );
		new_row_button.className = 'del';
		new_row_button.href = 'javascript:;';
		new_row_button.innerHTML = 'x';
		//new_row_button.src = 'delete.gif';
		//new_row_button.type = 'button';
		//new_row_button.value = 'Delete';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;
			document.getElementById('browse-disabled').setAttribute("id", "browse");

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		new_row.innerHTML = win_basename(element.value);

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};

function win_basename(str)
	{
	return str.substr(str.lastIndexOf('\\') + 1);
	}





/* CUSTOM FORM ELEMENTS - Created by Ryan Fait - http://www.ryanfait.com */
var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "190";

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;

/*
//confetti
const NUMBER_OF_CONFETTI=100;function init(){var container=document.getElementById('confettiContainer');for(var i=0;i<NUMBER_OF_CONFETTI;i++){container.appendChild(createALeaf())}}function randomInteger(low,high){return low+Math.floor(Math.random()*(high-low))}function randomFloat(low,high){return low+Math.random()*(high-low)}function pixelValue(value){return value+'px'}function durationValue(value){return value+'s'}function createALeaf(){var confettiDiv=document.createElement('div');var image=document.createElement('img');image.src='img/confetti'+randomInteger(1,7)+'.png';confettiDiv.style.top="-100px";confettiDiv.style.left=pixelValue(randomInteger(40,980));var spinAnimationName=(Math.random()<0.5)?'clockwiseSpin':'counterclockwiseSpinAndFlip';confettiDiv.style.webkitAnimationName='fade, drop';image.style.webkitAnimationName=spinAnimationName;var fadeAndDropDuration=durationValue(randomFloat(5,11));var spinDuration=durationValue(randomFloat(4,8));confettiDiv.style.webkitAnimationDuration=fadeAndDropDuration+', '+fadeAndDropDuration;var confettiDelay=durationValue(randomFloat(0,5));confettiDiv.style.webkitAnimationDelay=confettiDelay+', '+confettiDelay;image.style.webkitAnimationDuration=spinDuration;confettiDiv.appendChild(image);return confettiDiv}window.addEventListener('load',init,false);*/
