var currentStep = 1;
var stepTwoSection = 1;





function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return (true)
	}else {
		return false;	
	}
}
function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
		continue;
		parsed = false;
		break;
	}
	return parsed;
}






function setError(msg) {
	$('.formError').html('<img src="images/error_icon.gif" alt="" align="top"/>&nbsp;'+msg);
}

function setStepImag(step) {
	var passedFlag = 1;
	for(var i=1; i<=3;i++) {
		if(step==i) {
			passedFlag = 0;
			$('#step'+i).attr("src","images/steps/step"+i+"_act.jpg");
		} else {
			if(passedFlag) {
				$('#step'+i).attr("src","images/steps/step"+i+"_fac.jpg");
			} else {
				$('#step'+i).attr("src","images/steps/step"+i+"_nef.jpg");
			}
		}
		
	}
}

function setForm(step) {
	$('#stepContent'+(step-1)).fadeOut("slow",function(){
		$('.formError').html('');
		if(step == 2) {
			if($('#project_type1').attr("checked")){
				$('#outdoor').hide();
				stepTwoSection = 1;
			}else {
				$('#indoor').hide();
				stepTwoSection = 2;
			}
		}
		$('#stepContent'+step).fadeIn("slow");
	});
}

function validateStep1() {
	if($('#job_name').val() =='') {
		setError('Please enter job name');
	}else if($('#project_details').val() == '') {
		setError('Please describe your project');
	} else {
		setStepImag(2);
		setForm(2);
		return true;
	}
	return false;
}

function validateStep2() {
	
	var canmove = false;
	if(stepTwoSection == 1) {

		if($('#i_room_length').val() == '') {
			setError('Please fill room length');
		} else if($('#i_room_width').val() == '') {
			setError('Please fill room width');
		}else if($('#i_room_ceiling_height').val() == '') {
			setError('Please fill ceiling height');
		}else if($('#i_mounting_height').val() == '') {
			setError('Please describe mounting height');
		} else {
			canmove = true;
		}	
		if(canmove && e('i_reflectances_specify').checked) {
		
			if($('#i_reflectances_ceiling').val() == '') {
				setError('You selected Reflectances::Specify, please fill ceiling');
				canmove = false;
			} else if($('#i_reflectances_wall').val() == '') {
				setError('You selected Reflectances::Specify, please fill wall');
				canmove = false;
			}else if($('#i_reflectances_floor').val() == '') {
				setError('You selected Reflectances::Specify, please fill floor');
				canmove = false;
			} else {
				canmove = true;
			}
		}
		
		if(canmove && e('i_light_crit_specify').checked) {
			if($('#i_lighting_criteria_specify_avg').val() == '') {
				setError('You selected Lighting criteria::Specify, please fill Average FC');
				canmove = false;
			} else if($('#i_lighting_criteria_specify_min').val() == '') {
				setError('You selected Lighting criteria::Specify, please fill Minimum FC');
				canmove = false;
			} else {
				canmove = true;
			}
		}
		
		if(canmove)
		if(e('i_calc_plane_specify').checked && $('#i_calc_planeHolder input[@type="text"]').val() == '') {
			setError('You selected Calculation Plane::Specify, please fill the field');
		} else if($('#i_special_requirements').val() == '') {
			setError('Please fill special requirements');
		} else {
			setStepImag(3);
			setForm(3);
			return true;
		}
		
	}
	if(stepTwoSection == 2) {
		
		if($('#site_dimensions').val() == '') {
			setError('Please fill site dimensions');
		} else if($('#o_mounting_height').val() == '') {
			setError('Please fill mounting height');
		}else{
			canmove = true;
		}
		
		if(canmove && e('o_light_crit_specify').checked) {
			if($('#o_lighting_criteria_specify_avg').val() == '') {
				setError('You selected Lighting criteria::Specify, please fill Average FC');
				canmove = false;
			} else if($('#o_lighting_criteria_specify_min').val() == '') {
				setError('You selected Lighting criteria::Specify, please fill Minimum FC');
				canmove = false;
			} else {
				canmove = true;
			}
		}
		if(canmove)
		if(e('o_calc_plane_specify').checked && $('#o_calc_planeHolder input[@type="text"]').val() == '') {
			setError('You selected Calculation Plane::Specify, please fill the field');
		} else if($('#o_special_requirements').val() == '') {
			setError('Please fill special requirements');
		} else {
			setStepImag(3);
			setForm(3);
			return true;
		}
	}
	return false;
}

function validateStep3 () {
	var cansubmit = true;
	if($('input[@name="pinfo[requested_by]"]',$('#mainForm')).val() == '') {
		setError('Please fill requested by');
		cansubmit = false;
	}else if($('input[@name="pinfo[company]"]',$('#mainForm')).val() == '') {
		setError('Please fill company');
		cansubmit = false;
	}else if($('input[@name="pinfo[street_adress]"]',$('#mainForm')).val() == '') {
		setError('Please fill street adress');
		cansubmit = false;
	}else if($('input[@name="pinfo[city]"]',$('#mainForm')).val() == '') {
		setError('Please fill city');
		cansubmit = false;
	}else if($('input[@name="pinfo[state]"]',$('#mainForm')).val() == '') {
		setError('Please fill state');
		cansubmit = false;
	}else if($('input[@name="pinfo[zip]"]',$('#mainForm')).val() == '') {
		setError('Please fill zip code');
		cansubmit = false;
	}else if($('input[@name="pinfo[email]"]',$('#mainForm')).val() == '') {
		setError('Please fill email adress');
		cansubmit = false;
	}else if(!checkEmail($('input[@name="pinfo[email]"]',$('#mainForm')).val())) {
		setError('Please fill an valid email adress');
		cansubmit = false;
	}else if($('input[@name="pinfo[phone]"]',$('#mainForm')).val() == '') {
		setError('Please fill phone number');
		cansubmit = false;
	}else if($('input[@name="pinfo[fax]"]',$('#mainForm')).val() == '') {
		setError('Please fill fax number');
		cansubmit = false;
	}else if(!e('switchSameContacts').checked) {
		if($('input[@name="pinfo2[contact]"]',$('#mainForm')).val() == '') {
			setError('Please fill contact from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[company]"]',$('#mainForm')).val() == '') {
			setError('Please fill company from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[street_adress]"]',$('#mainForm')).val() == '') {
			setError('Please fill street adress  from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[city]"]',$('#mainForm')).val() == '') {
			setError('Please fill city  from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[state]"]',$('#mainForm')).val() == '') {
			setError('Please fill state  from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[zip]"]',$('#mainForm')).val() == '') {
			setError('Please fill zip code  from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[email]"]',$('#mainForm')).val() == '') {
			setError('Please fill email adress  from Sender Information');
			cansubmit = false;
		}else if(!checkEmail($('input[@name="pinfo2[email]"]',$('#mainForm')).val())) {
			setError('Please fill an valid email adress from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[phone]"]',$('#mainForm')).val() == '') {
			setError('Please fill phone number  from Sender Information');
			cansubmit = false;
		}else if($('input[@name="pinfo2[fax]"]',$('#mainForm')).val() == '') {
			setError('Please fill fax number  from Sender Information');
			cansubmit = false;
		}
	}
	
	if(cansubmit){ e('mainForm').submit(); }
	
}

function checkNextStep() {
	func = eval("validateStep"+(currentStep));
	if(func()) {
		currentStep++;
	}
}
function init() {
	$('#nextStep').attr("name","jsSubmit");
	$('#formType').attr("value","js");
	$('#mainForm').bind("submit",function(){checkNextStep();return false;});	
	
	
	//  Reflectances specify section disable
	$('#i_reflectances_rest input').click(function(){
		$('#i_reflectances_specify_form input').fadeTo("slow", 0.2);
		$('#i_reflectances_specify_form input').attr("disabled","disabled");
	});
	$('#i_reflectances_specify').click(function(){
		$('#i_reflectances_specify_form input').fadeTo("slow", 1);
		$('#i_reflectances_specify_form input').attr("disabled","");
	});
	
	
	//Calculation Plane input disable
	$('#i_calc_plane_rest_els').click(function(){
		$('#i_calc_planeHolder input[@type="text"]').fadeTo("slow", 0.2);
		$('#i_calc_planeHolder input[@type="text"]').attr("disabled","disabled");
	});
	$('#i_calc_plane_specify').click(function(){
		$('#i_calc_planeHolder input[@type="text"]').fadeTo("slow", 1);
		$('#i_calc_planeHolder input[@type="text"]').attr("disabled","");
	});
	
	
	
	//Lighting criteria specify section disable
	$('#i_light_crit_notsure').click(function(){
		$('#i_light_crit_specify_form input').fadeTo("slow", 0.2,function(){
			$('#i_light_crit_specify').attr("disabled","");
			$('#i_light_crit_specify').fadeTo("fast", 1);
		});
		$('#i_light_crit_specify_form input').attr("disabled","disabled");
	});
	$('#i_light_crit_specify').click(function(){
		$('#i_light_crit_specify_form input').fadeTo("slow", 1);
		$('#i_light_crit_specify_form input').attr("disabled","");
	});
	
	$('#switchSameContacts').click(function(){
		$('#sameForm').slideToggle("fast");
	});
	
	
}

function e(id){return document.getElementById(id);}

window.onload = init;