$(document).ready(function(){
	
	var formValue = new Array("","","","","","");

	var formState = new Array(0,0,0,0,0);
	
	var formValid = new Array(0,0,0,0,0);
	
	var formisValid = false;
	
	var formActive = false;
	
	var formObject = $('#text_area');
	
	var curSec = null;
	
	var pop_vis = false;
	
	$('.state div').css({ display:'none' });
	
	$('.item_h').css({ opacity:0.25 });
	
	$('#enq_input').hover(function() { 
		$(this).stop(true).animate({ opacity:0.8 });
	},function() {
		$(this).stop(true).animate({ opacity:0.5 });
	}).click(function() {
		if (formActive==false) {
			formActive = true;
			$(this).unbind('mouseenter mouseleave').stop(true).animate({ opacity:1 });
			$('#enq_form .b').stop(true).animate({ top: 108 });
			$('#enq_title').animate({ 'background-position':'0px -25px' });
			$('#enq_split').fadeOut();
			$('#enq_input input').css({ color: '#000', cursor:'text' }).val("");
			$('.state div').fadeIn('fast');
			$('#app_note').css({ top:'+=50' });
			hideNote();
			gotoStage(0);
		}
	}).keydown(function(e) {
		if(e.keyCode !== 13) {
			setTimeout(evalState,2);
		}else{
			if(curSec < 4) {
				curSec++;
				gotoStage(curSec);
			}else{
				if (formisValid == true){
					showPop();
				}
			}
		}
	});
	$('#enq_where_val, #enq_item_final, #form_item_subscribe').change(function() {
		evalState();
	});
	$('#enq_other_val').focus(function() {
		if ($(this).css('color') == 'rgb(153, 153, 153)') {
			$(this).css({ color:'#000', cursor:'text' });
		}
	});
	function evalState(ID) {
		var inputCheck = formObject.val();
		var charCount = inputCheck.split('').length;
		formValue[curSec] = inputCheck;
		switch(curSec) {
			case 1:
				var mail_hash = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if((mail_hash.test(inputCheck))?1:0) {
					formState[curSec] = ((mail_hash.test(inputCheck))?1:0);
					formValid[curSec] = 2;
				}else{
					formValid[curSec] = 1;
				}
			break;
			case 2:
				var hash = /^[0-9]+$/;
				if (charCount > 9 && charCount < 14) {
					formState[curSec] = ((hash.test(inputCheck))?1:0);
					formValid[curSec] = 2;
				}else{
					formValid[curSec] = 1;
				}
			break;
			case 4:
				if ($('#enq_where_val').val() == "Other") {
					$('#enq_other_cont').slideDown();
					$('#enq_other_val').focus();
				}else{
					$('#enq_other_cont').slideUp();
				}
				if ($('#enq_item_final').attr('checked') == true) {
					if ($('#enq_where_val').val() !== "...Select an option...") {
						//
						if ($('#enq_where_val').val() !== "Other") {
							//formValue[5] = $('#enq_where_val').val();
							formValid[curSec] = 2;
						}else{
							if ($('#enq_other_val').val() !== "") {
								formValid[curSec] = 2;
							}else{
								formValid[curSec] = 1;
							}
						}
					}else{
						//formValue[5] = $('#enq_where_val').val();
						formValid[curSec] = 1;
					}
				}else{
					formValue[5] = $('#enq_where_val').val();
					formValid[curSec] = 1;
				}
				if ($('#enq_where_val').val() !== "Other") {
					formValue[5] = $('#enq_where_val').val();
				}else{
					formValue[5] = $('#enq_other_val').val();
				}
				formValue[4] = $('#form_item_subscribe').attr('checked');
			break;
			default:
				var hash = /([A-Za-z0-9_\-\.])/;
				if (charCount > 2) {
					formState[curSec] = ((hash.test(inputCheck))?1:0);
					formValid[curSec] = 2;
				}else{
					formValid[curSec] = 1;
				}
			break;
		}
		for(var i = 0; i < formState.length; i++) {
			$('#S'+i).css({
				//'background-position-y': ((formState[i] + formValid[i])*(-20)),
				//'background-position-x':(0)
				'background-position': ("0px "+((formState[i] + formValid[i])*(-20))+"px")
			});
		}
		$('#S'+curSec).css({ 'background-position-x':(-20) });
		if(parseInt(formValid[0]+formValid[1]+formValid[2]+formValid[3]+formValid[4]) == 10) {
			formisValid = true
			$('#enq_form_send').stop(true,true).fadeIn('fast');
		}else{
			formisValid = false
			$('#enq_form_send').stop(true,true).fadeOut('fast');
		}
	}
	$('#enq_form_send').click(function(e) {
		showPop();
	});
	$('.state .item').click(function(e) {
		var ID = $(this).attr('id').split('S');
		gotoStage((parseInt(ID[1])));
	}).hover(function() {
		var ID = $(this).attr('id').split('S');
		$('#H'+(parseInt(ID[1]))).stop(true).animate({ opacity:0.9 });
		showNote(this);
	},function() {
		var ID = $(this).attr('id').split('S');
		$('#H'+(parseInt(ID[1]))).stop(true).animate({ opacity:0.25 });
		hideNote();
	});
	function gotoStage(NUM) {
		curSec = NUM;
		changeFormType(NUM);
		formState[curSec] = Math.max(1,formState[curSec]);
		formValid[curSec] = Math.max(1,formValid[curSec]);
		formObject.val(formValue[curSec]);
		var new_xy_pos = ("0px "+(NUM*(-25)-25)+"px");
		$('#enq_title').animate({ backgroundPosition:new_xy_pos },500,'easeOutElastic');
		evalState(NUM);
		if (pop_vis == true) {
			$('#enq_pop').fadeOut(function() {
				$('#enq_form_validate').attr({src:''}).fadeOut(function() {
					$('#enq_pop #load').css({ display: 'block' });
					pop_vis = false;
				});
			});
		}
		formObject.focus();
	}
	function showPop(CONT) {
		$('#enq_confirm').animate({
			height:480,
			'margin-top':(-240)
		},'slow')
		gotoStage(5);
		$('#enq_pop').fadeIn('slow',function() {
			validateForm();
		});
	}
	function hidePop() {
		$('#enq_confirm').animate({
			height:200,
			'margin-top':(-100)
		},'slow')
		$('#enq_pop').fadeOut('slow');
	}
	function validateForm() {
		if (formisValid == true) {
			pop_vis = true;
			var form_code = "php/enq_form_validate.php?f=validate";
			form_code += "&name="+formValue[0];
			form_code += "&email="+formValue[1];
			form_code += "&phone="+formValue[2];
			form_code += "&notes="+formValue[3];
			form_code += "&subscribe="+formValue[4];
			form_code += "&origin="+formValue[5];
			$('#enq_pop #load').fadeOut(function() {
				$('#enq_form_validate').attr({src:form_code}).fadeIn();
			});
		}
	}
	function changeFormType(NUM) {
		$('#input_textfield, #input_textarea, #input_info').slideUp('slow','easeInOutExpo');
		switch(NUM) {
			case 0:
				formObject = $('#text_input');
				$('#input_textfield').stop(true,true).slideDown('slow','easeInOutExpo');
			break;
			case 1:
				formObject = $('#text_input');
				$('#input_textfield').stop(true,true).slideDown('slow','easeInOutExpo');
			break;
			case 2:
				formObject = $('#text_input');
				$('#input_textfield').stop(true,true).slideDown('slow','easeInOutExpo');
			break;
			case 3:
				formObject = $('#text_area');
				$('#input_textarea').stop(true,true).slideDown('slow','easeInOutExpo');
			break;
			case 4:
				formObject = $('#text_input');
				$('#input_info').stop(true,true).slideDown('slow','easeInOutExpo');
			break;
		}
	}
});
