

var caprules = {
	rulesOff:false,havingTrouble:false,chkboxes:null,exterior:false,dosubmit:false,lastChkBox:0,pgloaded:false,alertLinkClicked:false
}


caprules.init = function (){
	with(caprules){
		var i=0;
		var chk = document.getElementsByTagName("input");
		var txt,pr,pc,isDefault=0,isMetPrice=0;

		//onclick
		chkboxes = new Array();
		
		//the follwoing varibles are set in the price tile (net van ones)
		if(typeof(pricesShownAsNet)!='undefined' && pricesShownAsNet==true){
			metallicPrice = parseInt(metallicPrice*100 / (vatRate+100));
		}
		if(typeof(vpriceUseThis)!='undefined'){
			vprice = vpriceUseThis;
		}
		
		for(var b=0;b<chk.length;b++)if(chk[b].type=="checkbox" && chk[b].id && chk[b].id.indexOf("opt")>-1){
			chkboxes[i] = chk[b];
			chkboxes[i].optionCode = getOptionCode(chkboxes[i].id.substr(3))
			txt = chkboxes[i].parentNode.parentNode.getElementsByTagName("TD")[1].innerHTML
			chkboxes[i].title = txt.split("<")[0];
			pr = chkboxes[i].parentNode.parentNode.getElementsByTagName("TD")[2]
			chkboxes[i].price = (pr.getElementsByTagName("A")[0])?pr.getElementsByTagName("A")[0].innerHTML:pr.innerHTML;
			isDefault=0;isMetPrice=0;
			pc = chkboxes[i].className.split(",");
			for(n in pc){
				if(pc[n].indexOf("isDefault")==0)isDefault = (pc[n].substr(9).indexOf("1")>0);	
				if(pc[n].indexOf("isMetPrice")==0)isMetPrice = (pc[n].substr(10)=="true");
			}
			chkboxes[i].isDefault = isDefault;
			chkboxes[i].isMetPrice = isMetPrice;
			chkboxes[i].onclick = caprules.captureCheckboxclick;
			i++;
		}

		$('input[name=deliveryMethodId]').click(caprules.captureCheckboxclick);
		document.getElementById('submitbtn').disabled=false; 
		document.getElementById('submitbtn').value = "Continue to Complete the Quote ›";
		if(window.location.toString().indexOf("www1")>-1){document.getElementById('parkCarForm').target="";} //affiliates sometime put us in frames which we want to pop out of. this stops this when in a pop up.
		
		caprules.chkGarageOptions();
		if(isSold){
			var html = '<h6>Sorry.. this great deal is no longer available</h6>'
			html = html + '<p>The good news is that we have lots of other great deals for you.</p>'
			html = html + '<div class="btnRight">'
			html = html + '<p style="width:400px">To search for similar deals click the Search now button.</p>'
			html = html + '<a href="' + newcarURL + '" class="btn">Search now &rsaquo;</a></div>'
			overlay.setMsg(html,"Vehicle Unavailable");
			overlay.onclose = null;
			overlay.show();
			return;
		}
		if( $("div.scrollable img").size()>0 ){ //just check there are items to scroll through
			$("div.scrollable").scrollable({circular:true}).autoscroll({ autoplay: true,interval:4000 }).navigator().find(".items").css("left","-268px");
			//css:left is added due to bug in chrome
		}

	}
	
}

caprules.getPrice = function(chk){
	return parseFloat(chk.price.replace("&pound;","").replace("£","").replace(",",""))
}

caprules.updatePage = function(){
	var ncsSel = "", ti
	var ototal = 0
	with(caprules){
		for(var b=0;b<chkboxes.length;b++){
			if(chkboxes[b].checked){
				$(chkboxes[b]).closest("tr").addClass("checked");
				//chkboxes[b].parentNode.parentNode.className = "checked"; //highlight row
				ti = (chkboxes[b].title.length>26)?chkboxes[b].title.substr(0,25)+"..":chkboxes[b].title; //add html to sidebar list
				ncsSel = ncsSel + "<li " + ((lastChkBox==chkboxes[b])?"id='fitler'":"") + "><a href='#' title='remove this feature' onclick='return caprules.untick(" + chkboxes[b].optionCode + ")'>" + ti + "</a></li>";
				if(chkboxes[b].pzero!=true){
					if(chkboxes[b].isMetPrice && metallicPrice>10){
						ototal = ototal + metallicPrice;
					}else{
						ototal = ototal + getPrice(chkboxes[b]) // add up cost of options
					}
				}
			}else{
				$(chkboxes[b]).closest("tr").removeClass("checked");
				//chkboxes[b].parentNode.parentNode.className = ""; // remove hightlight
			}
		}
		document.getElementById('ncsSelected').innerHTML = (ncsSel=="")? "<li>&#xA0;</li>":ncsSel; //update sidebar
		var del,box, delc ="0";

		del=document.getElementsByName("deliveryMethodId");
		$.each(del,function(index, box){
			if(box.checked){
				delc = box.parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML;
				box.parentNode.parentNode.className = "checked"; //highlight row
			}else{
				box.parentNode.parentNode.className = ""; // remove hightlight
			}
		});
		delc = delc.replace("&pound;","").replace("£","").replace(",","");
		var delivcost = parseFloat(delc);
		
		if(typeof(pricesShownAsNet)!='undefined' && pricesShownAsNet==true){
			var totalNet = Math.round(ototal + delivcost + vpriceNet);
			var totalPrice = Math.round(((ototal + delivcost) * (vatRate+100) /100 ) + vprice);
			var vatAmount = Math.round((ototal + delivcost + vpriceNet) * vatRate /100);
		}else{
			var totalPrice = Math.round(ototal + delivcost + vprice);
		}
		if(vprice>10){
			if(document.getElementById("totalOptions1"))document.getElementById("totalOptions1").innerHTML = "&pound;" + Math.round(ototal + delivcost);
			if(document.getElementById("totalCar1"))document.getElementById("totalCar1").innerHTML = "&pound;" + totalPrice;
			//if(document.getElementById("basiccost2"))document.getElementById("basiccost2").innerHTML = "&pound;" + Math.round(vprice)
			if(document.getElementById("totalOptions3"))document.getElementById("totalOptions3").innerHTML = "&pound;" + Math.round(ototal + delivcost);
			if(document.getElementById("totalCar3"))document.getElementById("totalCar3").innerHTML = "&pound;" + ((typeof(pricesShownAsNet)!='undefined' && pricesShownAsNet==true)?totalNet:totalPrice);
			
			if(document.getElementById("totalNet2"))document.getElementById("totalNet2").innerHTML = "&pound;" + totalNet;
			if(document.getElementById("totalVAT1"))document.getElementById("totalVAT1").innerHTML = "&pound;" + vatAmount;
		}
		if(lastChkBox!=0 && typeof(changeFitlerClass)=="function")changeFitlerClass(3);
		lastChkBox=0;
	}
}

caprules.showsummary = function(sup){
	var chkdOpt ="";
	with(caprules){
		if(chkboxes == null)return;
		//get options selected
		for(var b=0;b<chkboxes.length;b++){
			if(chkboxes[b].checked){
				chkdOpt = chkdOpt+ getSummaryTR(chkboxes[b]);
			}
		}
		//get car title
		var cartbl = document.getElementById("carOptionsCar");
		var cartr = cartbl.getElementsByTagName("tr")[1];
		var cartitle = cartr.getElementsByTagName("td")[0].innerHTML;
		//get other car details
		var infotr,infot,info="";
		for(i=2;i<cartbl.getElementsByTagName("tr").length;i++){
			infotr = cartbl.getElementsByTagName("tr")[i];
			infot = infotr.getElementsByTagName("td")[0].innerHTML;
			info = info + '<tr><td colspan="2">' + infot + '</td></tr>';
		}
		//get delivery details
		var del, delc, deltype
		del = $('input[name=deliveryMethodId]:checked')[0];
		//for(i=1;del=document.getElementById('parkCarForm_deliveryMethodId'+i);i++){
		//	if(del.checked){
				delc = del.parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML;
				deltype = del.parentNode.parentNode.getElementsByTagName("TD")[1].innerHTML.split("<d")[0];
		//	}
		//}
		var html = "<h6>Below is a summary of the vehicle you have built:</h6>";
		html = html + '<table width="500"  border="0" cellspacing="0" cellpadding="3" class="carSummaryNoCheck">';
		html = html + '<tr><td><b>The selected car is:</b></td><td><b>Price</b></td></tr>';
		html = html + '<tr><td>' + cartitle + '</td><td>&pound;' + vprice +'</td></tr>';
		html = html + '<tr><td><b>The selected options for this car are:</b></td><td><b>Price</b></td></tr>';
		html = html + chkdOpt;
		html = html + '<tr><td><b>This also includes:</b></td><td><b>Price</b></td></tr>';
		html = html + '<tr><td>12 months vehicle excise duty (car tax) at current rate</td><td>&pound;0</td></tr>';
		html = html + '<tr><td>' + deltype + '</td><td>' + delc +'</td></tr>';
		html = html + info;
		html = html + '<tr><td colspan="2"><b>Price Summary:</b></td></tr>';
		html = html + '<tr><td><i>TOTAL COST OF OPTIONS</i></td><td><i>' + document.getElementById("totalOptions1").innerHTML +'</i></td></tr>';
		html = html + '<tr><td><b>TOTAL BUYACAR ON THE ROAD PRICE</b></td><td><b>' + document.getElementById("totalCar1").innerHTML +'</b></td></tr>';
		html = html + "</table>";
		html = html + "<p>'''...'''...'''...'''</p><p><a href='" + sup + "' target='_blank'>click here to view the supplier</a></p>";
		html = html + getRuleEnd();
		overlay.setMsg(html,"Vehicle Summary");
		overlay.onclose = null;
		overlay.show();
		return false;
	}
}

caprules.untick = function(oc){
	with(caprules){
		for(var b=0;b<chkboxes.length;b++){
			if(chkboxes[b].optionCode==oc){
				chkboxes[b].checked = false
			}
		}
		caprules.runRules();
		return false;
	}
}

caprules.chkGarageOptions = function(){
	with(caprules){
		if(typeof(choosenOptions) != "undefined" && choosenOptions.length > 1){
			for(var n=0; n < choosenOptions.length-1; n++){
				var ch = document.getElementById("opt" + choosenOptions[n][0])
				if(ch){
					ch.checked=(choosenOptions[n][2]==-1)
					if(ch.checked && ch.isMetPrice)ch.metPriceShown = true;
				}
			}
			// show alert about exterior colour if missing when page is loaded during edit only.
			exterior = true;
			runRules();
			updatePage();
			exterior = false;
		}else{
			chkReset()			
		}
	}
}

caprules.chkReset = function(){
	with(caprules){
		//if(document.getElementById('parkCarForm_deliveryMethodId1'))document.getElementById('parkCarForm_deliveryMethodId1').checked=true
		$('input[name=deliveryMethodId].isDefaulttrue').prop('checked', true);
		var dn = false
		for(var b=0;b<chkboxes.length;b++){
			obj = chkboxes[b]
			obj.checked=obj.isDefault;
		}
		//set interior and exterior generic descriptions to off
		for(var b=0;b<chkboxes.length;b++){
			obj = chkboxes[b]
			while(obj != null && obj.tagName!="TABLE")obj = obj.parentNode;
			if(obj.id=="optionsinterior" || obj.id=="optionsexterior"){ //iterate checkboxes until interior or exterior one is found
				for(var n=0; n < vrules.length-1; n++){
					if(vrules[n][6]==chkboxes[b].optionCode && vrules[n][4]=="OO"){ //iterate rules until OO rule on interiors is found
						for(var e=0; e < vrules.length-1; e++){
							if(vrules[e][2]==vrules[n][2]){ //find other checkboxes applying to this rule
								for(var a=0;a<chkboxes.length;a++){
									if(chkboxes[a].optionCode==vrules[e][6] && chkboxes[a].checked){//get the checked checkboxes and uncheck
										chkboxes[a].checked = false
										dn = true
									}
								}
							}
						}
					}
				}
				//if(dn)break;
			}
		}
		caprules.runRules();
		return false;
	}
}

caprules.captureCheckboxclick = function (e){
	if(!e){
		var e = window.event;
		caprules.lastChkBox = e.srcElement
	}else{
		caprules.lastChkBox = (e.target)? e.target : e.srcElement
	}
	caprules.runRules();
}

caprules.getOptionCode = function(optionId){
	return optionId.split(":")[1]
}

caprules.rsubmit = function(){
	caprules.setRulesOff(true);
	caprules.runRules();
	return true;
}

caprules.runRules = function(){
	var ncsSel = "",error=false
	with(caprules){
		//if(rulesOff)overlay.hide();
		updatePage();
		setQuoteAssistantRulesError(true);
		$("tr.error").removeClass("error");
		for(var b=0;b<chkboxes.length;b++){
			//do all other rules
			if(chkboxes[b].checked || chkboxes[b].pricezero){
				if(caprules.checkboxRules(chkboxes[b])==false){
					caprules.showErrorAlertLink(chkboxes[b]);
					error = true;
					if(!rulesOff)return false;
				}
			}
			//do metallic price rule
			if(chkboxes[b].isMetPrice){
				if(caprules.ruleMetPriceRequired(chkboxes[b])==false)return false;
			}
		}
		//do paint required rule
		if(exterior){
			if(caprules.ruleExteriorRequired()==false){
				error = true;
				if(!rulesOff)return false;
			}
		}
		overlay.hide();
		if(error)return false;
		setRulesOff(false);
		if(dosubmit)document.getElementById('parkCarForm').submit();
		return true;
	}
}

caprules.checkboxRules = function(checkbox){
	with(caprules){
		var optionCode=getOptionCode(checkbox.id.substr(3));
		var checkReturn = true
		for(n in vrules){
			if(vrules[n][6]==optionCode){
				switch(vrules[n][4]){
					case "OO":
						checkReturn = ruleOO(vrules[n],checkbox)
					break
					case "RO":
						checkReturn = ruleRO(vrules[n],checkbox)
					break						
					case "RA":
						checkReturn = ruleRA(vrules[n],checkbox)
					break				
					case "NW":
						checkReturn = ruleNW(vrules[n],checkbox)
					break	
					case "IN":
						checkReturn = ruleIN(vrules[n],checkbox)
					break
					case "IO":
						checkReturn = ruleIO(vrules[n],checkbox)
					break
				}
				if(!checkReturn){return false;}
			}
		}
	}
}

/*
vrules[n][0] = [RP_EffectiveFrom]
vrules[n][1] = [RP_EffectiveTo]
vrules[n][2] = [RR_RuleCode]
vrules[n][3] = [RR_PeriodCode]
vrules[n][4] = [RR_RuleType]
vrules[n][5] = [RI_RuleCode]
vrules[n][6] = [RI_OptionCode]
vrules[n][7] = [RI_IsPrimary]
*/
caprules.showRulePopUp = function(msg,title){
	if(caprules.alertLinkClicked || (!caprules.rulesOff && !caprules.havingTrouble)){
		if(typeof(genericDescription)=="string" && genericDescription!="car" && msg.indexOf(" car")>-1){
		 msg = msg.replace(/ car/g," "+genericDescription);
		}
		overlay.setMsg(msg);
		overlay.onclose = "caprules.popUpCloseClicked()";
		overlay.show();	
	}
}

caprules.popUpIsOpen = function(){
	return overlay.api.isOpened()
}

caprules.popUpCloseClicked = function(havingTrouble){
	if(havingTrouble)caprules.havingTrouble=true
	caprules.setRulesOff(true);
	caprules.continueButtonClicked();
	if(caprules.dosubmit)document.getElementById('parkCarForm').submit();
}

caprules.setRulesOff = function(r){
	caprules.rulesOff=r;
	caprules.setQuoteAssistantRulesError(r);
}

caprules.setQuoteAssistantRulesError = function(r){
	if(document.getElementById("quoteAssistantRulesError"))document.getElementById("quoteAssistantRulesError").value=r;
}

caprules.showErrorAlertLink = function(chk){
	if(caprules.havingTrouble) return;
	var tr = chk.parentNode.parentNode;
	var container = tr.getElementsByTagName("TD")[1];
	$(tr).addClass("error");
	if(container.innerHTML.toString().indexOf('optionError')==-1){
		$(container).append("<a href=\"#\" onclick=\"return caprules.showErrorAlert('" + chk.id + "')\" class=\"optionError\">issue</a>");
	}
}

caprules.showErrorAlert = function(chkid){
	caprules.alertLinkClicked = true;
	var chk = document.getElementById(chkid);
	if(chk){
		caprules.checkboxRules(chk);
		caprules.alertLinkClicked = false;
	}
	return false
}

caprules.getRuleTR = function (chk){
	var txt,pr;
	if(chk.pzero){
		pr = "&pound;0";
	}else if(chk.metPriceShown){
		pr = "&pound;" + metallicPrice;
	}else{
		pr = chk.price;
	}
	txt =  "<tr><td width='4%'><input type='checkbox' id='oc" + chk.optionCode + "'" + ((chk.checked)?" checked=true":"") + "/></td>"
	+"<td width='84%'>" + chk.title + ((chk.pzero)? " (usually "+chk.price+")" : "") + "</td>"
	+"<td width='12%'>" + pr + "</td></tr>";
	return txt
}
caprules.getSummaryTR = function (chk){
	return "<tr><td width='84%'>" + chk.title + ((chk.pzero)? " (usually "+chk.price+")" : "") + "</td><td width='12%'>" + ((chk.pzero)? "&pound;0" : chk.price) + "</td></tr>"
}

caprules.getRuleEnd = function(){
	var txt = "<div class='btnRight'><button type='button' class='btn' onclick='return caprules.continueButtonClicked()'>Continue &rsaquo;</button></div>"
	txt = txt + "<p><a href='#' onclick='caprules.havingtrouble();return false;' class='blkU'>HAVING TROUBLE WITH THE QUOTE ASSISTANT?</a></p>"
	return txt;
}

caprules.ruleMetPriceRequired = function (checkbox){
	var chkdOpt ="",showmsg=false, obj;
	with(caprules){
		if(checkbox.checked && metallicPrice > 10 && parseInt(metallicPrice)!=getPrice(checkbox)){
			if(checkbox.metPriceShown != true)showmsg=true;
			checkbox.parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = "&pound;" + metallicPrice
			checkbox.metPriceShown = true;
			chkdOpt = getRuleTR(checkbox);
			
		}else{
			checkbox.parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = checkbox.price	
			checkbox.metPriceShown = false;
		}
		if(showmsg){
			var html = "<h6>The exterior colour chosen requires payment</h6>"
			html = html + "<p>We can obtain the saving on this particular vehicle, on the condition that we order with a metallic paint.</p>"
			html = html + "<p>You have choosen a non metallic paint option. We can order you a solid colour but you will be paying the same price as a metallic.</p>"
			html = html + "<p>The data shown on this page comes from a external company and we cannot amend their data, but work around this issue using this pop up!</p>"
			html = html + '<p>If you are looking to choose a solid colour, we are unable to discount the price any further, and if you wish to choose solid paint it will be charged as follows:</p>'
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + "<p>Why not choose a metallic colour? It will cost the same. To keep this colour please click CONTINUE otherwise uncheck it first.</p>"
			html = html + "<p>The price quoted here is the price you will pay. Choose your paint and other options to get a final price.</p>"
			html = html + getRuleEnd();
			overlay.setMsg(html,"QUOTE ASSISTANT – The exterior colour chosen requires payment");
			overlay.onclose = "caprules.continueButtonClicked()";
			overlay.show();
			return false;
		}	
		return true;
	}
}

caprules.ruleExteriorRequired = function (){
	var chkdOpt ="",i=0, obj;
	with(caprules){
		for(var b=0;b<chkboxes.length;b++){
			obj = chkboxes[b]
			while(obj != null && obj.tagName!="TABLE")obj = obj.parentNode;
			if(obj.id=="optionsexterior"){//get the checked checkboxes
				chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
				if(chkboxes[b].checked) return true;
				i++
			}
		}
		if(i>0){
			var html = "<h6>You have not selected the colour for your new car</h6>"
			html = html + "<p>Please select the colour, then click continue (you may have to scroll down to see this button).</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + "<p>If you dont yet know the colour you want <a href='#' onclick='caprules.exterior=false;caprules.continueButtonClicked();return false;'><b>click here</b></a> to skip this.</p>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please select your exterior colour");
			return false;
		}	
		return true;
	}
}

caprules.havingtrouble = function(){
	var txt = "<h6>Having trouble building your car quote?</h6>"
	txt = txt + "<p>Because cars come in so many different shapes and sizes getting a quote can get quite complicated.</p>"
	txt = txt + "<p>We've built a quote assistant that tells you what you can and can't select on a car, but if you want to switch that off you can do. You can then choose the items you want and if there are any issues our sales team will resolve them at a later stage.</p>"
	txt = txt + "<p style='margin-top:30px;'><b>Would you like to continue using the quote assistant?</b></h6>"
	txt = txt + "<p><a href='#' class='btn_or_wh' onclick='caprules.runRules();return false;'>&#xA0;YES&#xA0;&#xA0;</a>&#xA0; I would like to continue using the quote assistant</p>"
	txt = txt + "<p><a href='#' class='btn_or_wh' onclick='caprules.popUpCloseClicked(true);return false;'>&#xA0;NO&#xA0;</a>&#xA0; Please switch it off</p>"
	caprules.showRulePopUp(txt,"QUOTE ASSISTANT – Having trouble");
}

caprules.ruleIO = function (rule,checkbox){
	var chkdOpt ="", i=0, ruleerr = true;
	with(caprules)with(rule){
		if(rule[7]=="0")return true; //exit if not primary
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && vrules[n][7]=="0"){//get the checked checkboxes
						if(checkbox.checked){
							chkboxes[b].parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = "&pound;0"
							checkbox.pricezero = true
							chkboxes[b].pzero = true
						}else{
							chkboxes[b].parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = chkboxes[b].price
							checkbox.pricezero = false
							chkboxes[b].pzero = false
						}
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
						if(chkboxes[b].checked){ruleerr=false;}
					}
				}
			}
		}
		if(i>0 && ruleerr && checkbox.checked){
			var html = "<h6>you have selected the following option:</h6>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + getRuleTR(checkbox);
			html = html + "</table>"
			html = html + "<h6>This option includes one of the following options without cost:</h6> "
			html = html + "<p>Please select one of the included option(s) below OR deselect the option above, then click continue.</p>"			
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please select a no cost item");
			return false;
		}	
		return true;
	}
}

caprules.ruleIN = function (rule,checkbox){
	var chkdOpt ="", i=0, ruleerr = false;
	with(caprules)with(rule){
		if(rule[7]=="0")return true; //exit if not primary
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && vrules[n][7]=="0"){//get the checked checkboxes
						if(checkbox.checked){
							chkboxes[b].parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = "&pound;0"
							checkbox.pricezero = true
							chkboxes[b].pzero = true
						}else{
							chkboxes[b].parentNode.parentNode.getElementsByTagName("TD")[2].innerHTML = chkboxes[b].price
							checkbox.pricezero = false
							chkboxes[b].pzero = false
						}
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
						if(!chkboxes[b].checked){ruleerr=true;}
					}
				}
			}
		}
		if(i>0 && ruleerr && checkbox.checked){
			var html = "<h6>You have selected the following option:</h6>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + getRuleTR(checkbox);
			html = html + "</table>"
			html = html + "<h6>This option includes the following options without cost:</h6> "
			html = html + "<p>Please select all of the included option(s) below OR deselect the option above, then click continue.</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please select the included options");
			return false;
		}	
		return true;
	}
}

caprules.ruleNW = function (rule,checkbox){
	var chkdOpt ="", i=0, ruleerr = false;
	with(caprules)with(rule){
		if(!checkbox.checked)return true;
		if(rule[7]=="0")return true; //exit if not primary
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && vrules[n][7]=="0"){//get the checked checkboxes
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
						if(chkboxes[b].checked){ruleerr=true;}
					}
				}
			}
		}
		if(i>0 && ruleerr){
			var html = "<h6>You have selected the following option:</h6>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + getRuleTR(checkbox);
			html = html + "</table>"
			html = html + "<h6>This option can not be selected at the same time as the following options:</h6> "
			html = html + "<p>Please either deselect all of the options option(s) below OR deselect the first option, then click continue.</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please choose one or the other");
			return false;
		}	
		return true;
	}
}

caprules.ruleRA = function (rule,checkbox){
	var chkdOpt ="", i=0, ruleerr = false;
	with(caprules)with(rule){
		if(!checkbox.checked)return true;
		if(rule[7]=="0")return true; //exit if not primary
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && vrules[n][7]=="0"){//get the checked checkboxes
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
						if(!chkboxes[b].checked){ruleerr=true;}
					}
				}
			}
		}
		if(i>0 && ruleerr){
			var html = "<h6>You have selected the following option:</h6>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + getRuleTR(checkbox);
			html = html + "</table>"
			html = html + "<h6>This option requires that all of the following options are also selected:</h6> "
			html = html + "<p>Please select all the option(s) below OR deselect the option above, then click continue.</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please select all the required options ");
			return false;
		}	
		return true;
	}
}

caprules.ruleRO = function (rule,checkbox){
	var chkdOpt ="", i=0;
	with(caprules)with(rule){
		if(!checkbox.checked)return true;
		if(rule[7]=="0")return true; //exit if not primary
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && vrules[n][7]=="0"){//get the checked checkboxes
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
						if(chkboxes[b].checked){i=0;return true;}
					}
				}
			}
		}
		if(i>0){
			var html = "<h6>You have selected the following option:</h6>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + getRuleTR(checkbox);
			html = html + "</table>"
			html = html + "<h6>This option requires that at least one of the following options is also selected:</h6> "
			html = html + "<p>Please select at least one of the option(s) below OR deselect the first option, then click continue.</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSITANT – Please select another option");
			return false;
		}	
		return true;
	}
}


caprules.ruleOO = function (rule,checkbox){
	var chkdOpt ="", i=0;
	with(caprules)with(rule){
		if(!checkbox.checked)return true;
		for(var n=0; n < vrules.length-1; n++){
			if(vrules[n][2]==rule[2]){ //find other checkboxes applying to this rule
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==vrules[n][6] && chkboxes[b].checked){//get the checked checkboxes
						chkdOpt = chkdOpt+ getRuleTR(chkboxes[b]);
						i++
					}
				}
			}
		}
		if(i>1){
			var html = "<h6>Only one of the following options can be selected:</h6>"
			html = html + "<p>Please select just one option, then click continue.</p>"
			html = html + '<table width="100%"  border="0" cellspacing="0" cellpadding="3">'
			html = html + chkdOpt;
			html = html + "</table>"
			html = html + getRuleEnd();
			caprules.showRulePopUp(html,"QUOTE ASSISTANT – Please choose only one");
			return false;
		}	
		return true;
	}
}

caprules.continueButtonClicked = function(){
	caprules.updateBoxes();
	caprules.runRules();
	return false;
}

caprules.updateBoxes = function(){
	with(caprules){
		var objnodes = overlay.content[0].getElementsByTagName("input");
		for(var n=0;n<objnodes.length;n++){
			if(objnodes[n].id.indexOf("oc")>-1){
				for(var b=0;b<chkboxes.length;b++){
					if(chkboxes[b].optionCode==objnodes[n].id.substr(2)){//get the checked checkboxes
						chkboxes[b].checked = objnodes[n].checked
					}
				}
			}
	   } 
	}
}

$(document).ready(function() {
    caprules.init();
});


