/*
var emailErrorVar = "";
function emailError(a_response){
		emailErrorVar = emailErrorVar;
}
*/
store = ""
make = ""
model = ""
cab = ""
bed = ""
interest = ""
valid = ""
urlstr = "a=a&"
fields = Array( 'scheduled',	
			    'store',
				'make',
				'model',
				'cab',
				'bed',
				'name',
				'l_name',
				'business',
				'email',
				'call_or_not',
				'phone',
				'before',
				'q1','q2','q3','q4','q5','q6','with','questions'
				
				);
labels = Array(
			   	'Appointment Scheduled',
			   	'Nearest Location',
				'Truck Make',
				'Truck Model',
				'Cab Size',
				'Bed Size',
				'First Name',
				'Last Name',
				'Business or Organization',
				'Email',
				'Recieve a call?',
				'Phone Number',
				'Have I done business with Yipes! before?',
				'<img src="images/quote/check.gif">',
				'<img src="images/quote/check.gif">',
				'<img src="images/quote/check.gif">',
				'<img src="images/quote/check.gif">',
				'<img src="images/quote/check.gif">',
				'<img src="images/quote/check.gif">',
				'with',
				'Additional Questions'
				);


var	results = {};
rh = document.getElementById('ResponseHolder');

function startQuote(st){	
	if( rh ){
		if(!st){
			rhC =  "<h3>Select the location nearest you</h3><div>"
			rhC +="<SELECT id=\"store\"  onChange=\"startQuote(this.value)\">"
			rhC +="<OPTION value=\"\" selected=\"selected\">Please Select One</OPTION>"
			rhC +="<OPTION value=\"Williston\" >Williston, VT</OPTION>"
			//rhC +="<OPTION value=\"St_Albans\" >St. Albans, VT</OPTION>"
			rhC +="<OPTION value=\"Plattsburgh\" >Plattsburgh, NY</OPTION>"		
			rhC +="</SELECT></div>";
			rh.innerHTML = rhC;
		}else{
		store = st
		results['store'] = st;
		onyxQuote('1')
		}
	}	
}

function quoteStep1(req,cur,val){	// $next, $name, this.value : passed from php script
	if( rh ){
			switch (cur){
				case "make":
					make = val;
					model = '';
					cab = '';
					bed = '';
					results['make'] = val;
					document.getElementById('cab_holder').innerHTML="";
					document.getElementById('bed_holder').innerHTML="";
					goNext('X')
					break;
				case "model":
					model = val;
					cab = '';
					bed = '';
					results['model'] = val;
					document.getElementById('bed_holder').innerHTML="";
					goNext('X')
					break;				
				case "cab":
					cab = val;
					bed = '';
					results['cab'] = val;
					goNext('X')
					break;
				case "bed":
					bed = val;
					results['bed'] = val;
					goNext('2')
					break;							
			}		
		}
		
		url = "includes/onyxquote/select_truck.php?r="+req+"&make="+make+"&model="+model+"&cab="+cab+"&bed="+bed ;
		id = req+'_holder';
		ajaxLoader(url,id,'replace')		
		//alert('make ='+ make+'\nmodel ='+ model+'\ncab ='+ cab+'\nbed ='+ bed+'\nreq ='+ req)	
}

function quoteStep2(obj){
	thisinterest = ""
	if(obj.value !=''){
		for (i = 0; i < obj.length; i++) {
			if (obj[i].selected) {
				thisinterest += obj[i].value + ","
			} 
		}
		results['interest'] = thisinterest;
		goNext('3')
	}
}

function quoteStep3(){ //validate the form
	valid = 'yes'

	validateFields = Array('name','l_name','email','call_or_not')
	validateLabels = Array('Name','Last Name','Email','Recieve a call?');


	cif = document.getElementById('customerInfoForm');
	cifv = ""
	for (i=0;i< cif.elements.length;i++){	
		cvn = cif[i].name
		cvv = cif[i].value
		cif[i].style.borderColor = '';
		if(	 cif[i].type=='checkbox'  || cif[i].type=='radio' ){
			if(cif[i].checked){
			results[cvn] = cvv;
			}
		}else{
		results[cvn] = cvv;
		}
		if( (cvn == 'call_or_not') && ( cvv == 'Yes') && ( cif[i].checked)){
			fields.push('phone');
			labels.push('Phone');
		}
		if(cvn == 'phone'){
			p=cvv;
			p=p.replace("(","")
			p=p.replace(")","")
			p=p.replace("-","")
			p=p.replace("-","")
			if ( isNaN(p)== true ){
				valid = 'no'
				alert("Please enter a number in the phone field");
				return false;
			}
		}
		/*if(cvn == 'email'){	
			if(emailErrorVar==''){
				ajaxLoader('includes/etc/vEmail.php?Email='+cvv,'', 'email')	
			}else if(emailErrorVar!='OK'){
				alert(emailErrorVar)
				valid = 'no'
				return false;
			}else{
				if(valid == 'yes'){
					onyxQuote('4')
				}
			}
		}
		*/
		if(cvn == 'email'){
			////////////////////////   BOF Validate Email   ////////////////////////////// 
				if(cvv !=''){
					emailCheck = sjaxRetriever("js/verifyEmail.php?email="+cvv);
					if(emailCheck != 'OK'){
						valid = 'no';
						alert(emailCheck);
						cif[i].style.borderColor = 'red';
						cif[i].focus();
						return false;
					}
				}
			////////////////////////   EOF Validate Email   ////////////////////////////// 
		}
		
		
		if( !cif[i].value){	
			for(x in validateFields){
				if (cvn == validateFields[x]){
					label = validateLabels[x]
					cifv  = "The "+ label +" field is empty.\n Please click OK to correct this";
					valid = 'no'
					alert(cifv);
					cif[i].style.borderColor = 'red';
					cif[i].focus();
					return false;						
				}
				
			}
		}
	}

	if(valid == 'yes'){
		
		onyxQuote('4')	
	}
}
function quoteStep4(){
	qf = document.getElementById('questionsForm');
	for (i=0;i< qf.elements.length;i++){
		if(qf[i].type=='checkbox'){
			if(qf[i].checked){
				qfn = qf[i].name
				qfv = qf[i].value
				results[qfn] = qfv;	
			}	
		}else{
			qfn = qf[i].name
			qfv = qf[i].value
			results[qfn] = qfv;	
		}
	}
	onyxQuote('5')
	

}
function dontGoNext(){
	if(document.getElementById('goNext')){
		document.getElementById('goNext').style.visibility = "visible"
	}	
}
function goNext(step){
	if(!document.getElementById('goNext')){
		rh.innerHTML +='<input id=\"goNext\" type=\"button\" value=\"Next\" />';
	}
	if(document.getElementById('goNext')){
		if(step == "X"){
			document.getElementById('goNext').style.visibility = "hidden";
		}else{
			document.getElementById('goNext').style.visibility = "visible"
		}
	}
	document.getElementById('goNext').onclick = function(){onyxQuote(step)};	
}


function onyxQuote(step){
	if(step == '1'){
		setform = '<img src=\"images/quote/progress_1.png\" class = \"progress\"><h3>What type of Truck do you have?</h3><div id="make_holder"></div><div id="model_holder"></div><div id="cab_holder"></div><div id="bed_holder"></div>'
		rh.innerHTML = setform;
		url = "includes/onyxquote/select_truck.php?r=make";
		id = "make_holder";
		ajaxLoader(url,id,'replace')		
	}
	if(step == '2'){
		rhC =  "<img src=\"images/quote/progress_2.png\" class = \"progress\"><h3>What type of Bedliner are you interested in?</h3><div>"
		rhC +="<SELECT id=\"interest\"  onChange=\"quoteStep2(this)\" size=\"5\" multiple>"
		rhC +="<OPTION value=\"ONYX Over The Rail\" >ONYX Over The Rail</OPTION>"
		rhC +="<OPTION value=\"ONYX Under The Rail\">ONYX Under The Rail</OPTION>"
		rhC +="<OPTION value=\"EXPRESS LINER Over The Rail\">EXPRESS Over The Rail</OPTION>"
		rhC +="<OPTION value=\"EXPRESS LINER Under The Rail\">EXPRESS Under The Rail</OPTION>"
		rhC +="</SELECT>"
		rhC +="</div>"
		rh.innerHTML = rhC;		
	}
	if(step == '3'){
		rh.innerHTML ='<img src=\"images/quote/progress_2.png\" class = \"progress\">'
		url = "includes/onyxquote/customer_info.php?"+urlstr;
		id = "ResponseHolder";
		ajaxLoader(url,id,'add')
	}	
	if(step == '4'){
		rh.innerHTML ='<img src=\"images/quote/progress_3.png\" class = \"progress\">'
		url = "includes/onyxquote/questions.php?"+urlstr;
		id = "ResponseHolder";
		ajaxLoader(url,id,'add')	
	}
	if(step == '5'){
		listString = "";
		urlstr = "a=a&"
		for (x in results){
			if(results[x]){
				urlstr += x+"="+results[x]+"&"
				label = x
				for (y in fields){
					if (label == fields[y]){
						label = labels[y]
						listString += "<p  class=\"result\">"+label + ": "+results[x]+"</p> ";
						if(x=='bed'){
						listString +="<p class=\"edit\" onClick=\"onyxQuote('1')\">EDIT</p>";		
						}
						if(x=='before'){
						listString +="<p class=\"edit\" onClick=\"onyxQuote('3');\">EDIT</p>";		
						}
						if(x=='questions'){
						listString +="<p class=\"edit\" onClick=\"onyxQuote('4')\">EDIT</p>";		
						}
					}
				}
			}
		}
		setform = '<img src=\"images/quote/progress_4.png\" class = \"progress\"><h3>Please review your information</h3>'+listString;
		
		rh.innerHTML = setform;	
		goNext('6')
	}
	if(step == '6'){
		document.getElementById('ResponseHolder').innerHTML = "Sending request... please wait."
		params = "";
			for (x in results){
				if (x){
					params += x + "="+encodeURI(results[x])+"&";
				}
			}
		rh.innerHTML ='<img src=\"images/quote/progress_5.png\" class = \"progress\">'  	
		url = "includes/onyxquote/quote_mailer.php";
		id = "ResponseHolder";
		ajaxLoaderPost(url,id,params)
	}	
}
window.addEvent('domready', function() {
	$('begin').addEvents({
		'click' : function(event) {	
			startQuote(this.name);
			return false;
		}
	});

});