function strpos(haystack, needle){
	return 	haystack.indexOf(needle);
}

function strrpos(haystack, needle){
	pos = 0;
	while( haystack.indexOf(needle) > 0 ){
		pos += haystack.indexOf(needle)+1;
		haystack=haystack.substring(haystack.indexOf(needle)+1);
	}
	return pos;
}

function add_events(){
	validation_func = function(){
		if( validate(this, this.alt) || this.length==0 ){
			this.nextSibling.style.visibility = 'hidden';
			this.style.backgroundColor = '';
			this.style.color = '';
		} else {
			this.nextSibling.style.visibility = 'visible';
			this.style.backgroundColor = '#330000';
			this.style.color = '#ff0000';
		}	return true;
	};
	
	validation_boxes = $$('input[alt="email"]', 'input[alt="alpha"]', 'input[alt="numeric"]', 'input[alt="alpha_numeric"]');
	for(x=0; x<validation_boxes.length; x++){
		if( validation_boxes[x].id != 'join_email_text' ){
			validation_boxes[x].onblur = validation_func;
			validation_boxes[x].onchange = validation_func;
		}
	}
	
	files = $$('input[alt="file"]');
	for(x=0; x<files.length; x++){
		files[x].onchange = handle_file;
	}
	
	numbered_list = document.getElementsByClassName('numbered');
	for( x=0; x<numbered_list.length; x++ ){
		numImg = document.createElement('IMG');
		numImg.src = 'images/layout/numbers_'+(x+1)+'.gif';
		numbered_list[x].insertBefore( numImg, numbered_list[x].firstChild );
	}
	
	if( $('hmpg_img_map') ){
		$('hmpg_text_two').onmouseout = function(){
			$('hmpg_text_one').style.visibility = 'visible';
			$('hmpg_text_two').style.display = 'none';
		}
		$('hmpg_img_map').onclick = function(){
			$('hmpg_text_one').style.visibility = 'hidden';
			$('hmpg_text_two').style.display = 'block';
			return false;
		};
		$('hmpg_img_map').onmouseover = function(){ window.status='Click to learn more about us!'; };
	}
	
	if( $('join_email_text') ){
		$('join_email_text').onfocus = function(){ $('join_email').className = 'join_email_enlarge'; };
		$('join_email_text').onblur = function(){ setTimeout("$('join_email').className = 'join_email';", 250); };
	}
	
	if( $('join_email_submit') ){
		$('join_email_submit').onclick = function(){ submitJoinEmail(); };
	}
}

function handle_file(){
	if( this.value.length>0 ){
		if( validate(this, 'file') ){
			$('file_list').parentNode.style.display = 'block';
			file_name = this.value.substring(strrpos(this.value,'\\'));
			file_display_name = (file_name.length<=44) ? (this.value.substring(0,(44-(file_name.length+3))) +'...'+file_name) : file_name.substring(0,44);
			file_value = document.createTextNode( file_display_name );
			delete_link = document.createElement('A');
			delete_link.appendChild( document.createTextNode('X') );
			delete_link.href = this.value;
			delete_link.onclick = delete_link.onkeyup = file_remove;
			delete_link.title = 'Remove this file.';
			file_div = document.createElement('DIV');
			file_div.appendChild( delete_link );
			file_div.appendChild( file_value );
			$('file_list').appendChild( file_div );
			new_file_uploader = this.cloneNode(true);
			
			this.onblur = null;
			this.onchange = null;
			$('upload_holder').appendChild(this);
			
			
			new_file_uploader.value = '';
			new_file_uploader.onblur = handle_file;
			new_file_uploader.onchange = handle_file;
			$('uploader').appendChild( new_file_uploader );
			$('uploader').firstChild.data = 'Upload another...';
			return true;
		} else {
			extension = this.value.substring(strrpos(this.value, '.'));
			alert('We are currently not accepting '+extension.toUpperCase()+' files.');
			return false;
		}
	}
}

function file_remove(){
	files = $('upload_holder').getElementsByTagName('INPUT')
	for( x=0; x<files.length; x++ ){
		if( files[x].value.toLowerCase() == this.href.toLowerCase() ){
			files[x].parentNode.removeChild( files[x] );
			this.parentNode.parentNode.removeChild( this.parentNode );
		}
	}
	
	if( files.length==0 ) $('uploader').firstChild.data = 'Upload your DESIGN';
	return false;
}

function validate( input, mode ){
	content = (input.tagName && input.tagName=='INPUT') ? input.value.toString() : input.toString();
	if(content.length==0) return true;

	switch( mode ){
		case 'alpha':
			return (content.match(new RegExp(/^[a-zA-Z \t\r\n]+$/))) ? true : false;
			break;
		case 'numeric':
			return (content.match(new RegExp(/^[0-9]+$/))) ? true : false;
			break;
		case 'alpha_numeric':
			return (content.match(new RegExp(/^[a-zA-Z0-9]+$/))) ? true : false;
			break;
		case 'email':
			if( content.indexOf('@')<=0 || content.indexOf('.')<=0 ) return false;
			return (content.match(new RegExp(/^([a-z0-9])(([\-.]|[_]+)?([a-z0-9]+))*(@)([a-z0-9])((([a-z0-9\-\.]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/i))) ? true : false;
			break;
		case 'file':
			return ( allowed_files.indexOf(input.value.substring(strrpos(input.value, '.')).toLowerCase()) < 0 ) ? false : true;
			break;
	}
}

function main_submit( obj ){
	if( $('agreed').checked == true ){
		$('main_submit_button').style.display = 'none';
		$('main_submit_notice').style.display = 'block';
		$('uploader').removeChild( $('uploader').lastChild );	
		return true;
	} else {
		alert('You must agree to the rules and regulations.');
		return false;
	}
}

function submitJoinEmail(){
	if( $('join_email_text') ){
		if( !validate($('join_email_text'), 'email') && $('join_email_text').value.length>0 ){
			alert('Invalid email address...');
			setTimeout("$('join_email_text').focus();", 275);	// Event bubbling order correction.
		} else {
			var myAjax = new Ajax.Request('./ajax/join_email.php', {
				method: 'post',
				parameters: 'email_address='+escape( $('join_email_text').value ), 
				onComplete: function(req){
					if( req.responseText == 'true' ){
						$('join_email_start').style.display = 'none';
						$('join_email_done').style.display = 'block';
						$('join_email_done').firstChild.data = 'Thinking...';
						setTimeout("$('join_email_done').firstChild.data = 'Done!';", 1500);
					} else {
						alert('Error!');
						setTimeout("$('join_email_text').focus();", 275);	// Event bubbling order correction.
					}
				}
			});
		}
	} else {
		alert('Error!');
		setTimeout("$('join_email_text').focus();", 275);	// Event bubbling order correction.
	}
}

// /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/