/* ajax functions for wheel chair accessible vans */
var xmlhttp=false; 

if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest(); 
}else{
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function xmlAction(fragment_url){
	xmlhttp.open("GET", fragment_url); 
	xmlhttp.onreadystatechange = function() { 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
		
		//document.getElementById('loadInfo').innerHTML = '....'; 
		} 
	} 
	xmlhttp.send(null); 
}

function loadFragmentInToElement(fragment_url, element_id, extra, loading) { 
    var element = document.getElementById(element_id); 
    //document.getElementById('loadInfo').innerHTML = 'Loading ...'; 
   if(loading != ''){
   	   element.innerHTML = loading;
   }else{
	   element.innerHTML = 'Loading...';
   }
    xmlhttp.open("GET", fragment_url); 
    xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      element.innerHTML = xmlhttp.responseText; 
      	
		if(extra != ''){
			eval(extra);
		}
	  
      //document.getElementById('loadInfo').innerHTML = '....'; 
      } 
    } 
    xmlhttp.send(null); 
} 

function loadFragmentInToElementPost(fragment_url, element_id, post_data) { 
    var element = document.getElementById(element_id); 
    //document.getElementById('loadInfo').innerHTML = 'Loading ...'; 
    element.innerHTML = 'Loading...';
    //alert(post_data);
	xmlhttp.open("POST", fragment_url);
	xmlhttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded");
	xmlhttp.send(post_data);
    xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      element.innerHTML = xmlhttp.responseText; 
      
      //document.getElementById('loadInfo').innerHTML = '....'; 
      } 
    } 
    //removing this seems to get rid of javascript error...not sure if needed
	//xmlhttp.send(null); 
} 

function fncCenteredChildWindow(URL,vHeight,vWidth,vScroll,vStatus) {
	if (screen.width) {
		var winl=(screen.width-vWidth)/2;
		var wint=(screen.height-vHeight)/2;
	} else { 
		winl=0;wint=0;
	}
	if(winl<0)winl=0;
	if(wint<0)wint=0;
	var child = window.open(URL,'myWin','height='+vHeight+',width='+vWidth+',scrollbars='+vScroll+',status='+vStatus+',top='+wint+',left='+winl+',resizable=no');
	child.focus();
}

/*  Van Search Functions	*/
function refreshModels(obj){
	loadFragmentInToElement('/includes/ajax.php?m=search&sm=model&make='+obj.value,'vanModelSelectDiv', '', '');
}
function compareToggle(id,obj){
	var detailDiv = document.getElementById('ddiv_'+id);
	var detailCheck = document.getElementById('dcheck_'+id);
	var listDiv = document.getElementById('ldiv_'+id);
	var listCheck = document.getElementById('lcheck_'+id);
	
	if(obj.checked){
		detailDiv.className='LvScompareCHECKED';
		listDiv.className='LvScompareCHECKED';
		detailCheck.checked = true;
		listCheck.checked = true;
		
		xmlAction('/includes/ajax.php?m=compare&sm=add&id='+id)
	}else{
		detailDiv.className='LvScompareNOTCHECKED';
		listDiv.className='LvScompareNOTCHECKED';
		detailCheck.checked = false;
		listCheck.checked = false;
		
		xmlAction('/includes/ajax.php?m=compare&sm=remove&id='+id)
	}
}
function compareSToggle(id){
	var div = document.getElementById('sDiv_'+id);
	var check = document.getElementById('sCheck_'+id);
	
	if(check.checked){
		div.className='LvLcompareCHECKED';
		xmlAction('/includes/ajax.php?m=compare&sm=add&id='+id)
	}else{
		div.className='LvLcompareNOTCHECKED';
		xmlAction('/includes/ajax.php?m=compare&sm=remove&id='+id)
	}
}
function compareSimilarToggle(id){
	var div = document.getElementById('similarDiv_'+id);
	var check = document.getElementById('similarCheck_'+id);
	
	if(check.checked){
		div.className='checked';
		check.className='checked';
		xmlAction('/includes/ajax.php?m=compare&sm=add&id='+id)
	}else{
		div.className='notchecked';
		check.className='notchecked';
		xmlAction('/includes/ajax.php?m=compare&sm=remove&id='+id)
	}
}

/*	Van Summary Div Ajax Functions	*/
function updateDeliveryState(obj){
	var total = "loadFragmentInToElement('/includes/ajax.php?m=summary&sm=calcTotal','totalPrice','','Calculating...')"
	var extra = "loadFragmentInToElement('/includes/ajax.php?m=summary&sm=refreshOptions','deliveryOptions', \""+ total +"\", '')";
	loadFragmentInToElement('/includes/ajax.php?m=summary&sm=delCity&state='+obj.value,'deliveryCity', extra, '');
}
function updateDeliveryCity(obj){
	var total = "loadFragmentInToElement('/includes/ajax.php?m=summary&sm=calcTotal','totalPrice','','Calculating...');"
	loadFragmentInToElement('/includes/ajax.php?m=summary&sm=delOption&city='+obj.value,'deliveryOptions', total, '');
}
function updateDeliveryOption(obj){
	var total = "loadFragmentInToElement('/includes/ajax.php?m=summary&sm=calcTotal','totalPrice','','Calculating...');"
	loadFragmentInToElement('/includes/ajax.php?m=summary&sm=changeOption&opt='+obj.value,'deliveryOptions', total, '');
}
function summaryUpgradeToggleClick(obj,id){
	//toggle check box in main div
	if(check = document.getElementById('detailUpgradeCheck_'+id)){
		check.checked = obj.checked;
	}
	
	//call ajax action to update price
	summaryUpgradeToggle(obj,id);
}
function detailUpgradeToggleClick(obj,id){
	//toggle check box in summary div
	if(check = document.getElementById('summaryUpgradeCheck_'+id)){
		check.checked = obj.checked;
	}
	
	//call ajax action to update price
	summaryUpgradeToggle(obj,id);
}
function summaryUpgradeToggle(obj,id){
	var div = document.getElementById('summaryUpgradeDiv_'+id);
	var span = document.getElementById('detailUpgradeSpan_'+id);
	if(div.className == 'moblist'){
		div.className = 'moblistAlt';
		span.className = 'mobilityChecked';
		
		loadFragmentInToElement('/includes/ajax.php?m=summary&sm=mobChange&id='+id,'totalPrice','','Calculating...');
	}else{
		div.className = 'moblist';
		span.className = 'mobilityNotChecked';
		
		loadFragmentInToElement('/includes/ajax.php?m=summary&sm=mobChange&id='+id,'totalPrice','','Calculating...');
	}
}

//require fields in a form and redirect to url
function fncSubmit(obj,url) {
	var req = obj.required_fields;
	var submitflag = true;
	var notice = "Some of the required fields are blank:\n\n";
	if (req) {
		//Means the required fields are present, let's check for values
		var arrFields = obj.required_fields.value.split("|")
		for(var x=0; x<=arrFields.length-1; x++) {
			var arrName = arrFields[x].split(",");
			arrLabel=arrName[0];
			
			if (obj.elements[arrLabel]){
				
				var str = "if (obj."+arrName[0]+".value == '') { notice += '-->  "+arrName[1]+"\\n'; submitflag = false;}";
				eval(str);
			}
    }
		notice += "\nPlease fill in these fields and resubmit!";
	} else {
		//Means there isn't a required fields parameter; keep going
	}
	if (submitflag) {
		
		obj.action = url;
		obj.submit();
	} else {
		alert(notice);
	}
}