// JavaScript Document
// process embeded form
function buy_it_nowSubmit() {
	
	(function($){
		var $olay = $('<div />').appendTo(document.body).css({zIndex:1000, position:"fixed", height:'100%', width:'100%', background:'#333', left:0, top:0, opacity:0.75, display:'block'});
		var $obox = $('<div />').appendTo(document.body).css({"-moz-box-shadow":'0px 0px 30px #333', "-webkit-box-shadow":'0px 0px 30px #333',zIndex:1001, position:"fixed", height:'450px', width:'750px', background:'white', padding:'10px', display:'block'});
		var closer =  function(){
			$olay.add($obox).remove();
			//window.location.reload();
			return false;
		};
		$olay.bind('click', closer);
		$obox.css({
			left: $(window).width()/2 - $obox.innerWidth()/2,
			top: $(window).height()/2 - $obox.innerHeight()/2
		});
		$('<a href="#" style="position:absolute; top:12px; right:25px; background:white; padding:6px;">Close Window</a>').appendTo($obox).bind('click', closer);
		var aa = 'price='+encodeURI(document.getElementById('buy_it_now').innerHTML)+'&fname='+encodeURI(document.getElementById('first').value)+'&lname='+encodeURI(document.getElementById('last').value)+'&email='+encodeURI(document.getElementById('email').value)+'&category='+encodeURI(document.getElementById('category').value)+'&manufacturer='+encodeURI(document.getElementById('manufacturer').value)+'&brand='+encodeURI(document.getElementById('brand_formbuy').value)+'&model='+encodeURI(document.getElementById('model_formbuy').value)+'&stock='+encodeURI(document.getElementById('stock_formbuy').value);
		$('<iframe style="width:100%; height:100%;" src="http://www.uvsconsole.com/clients/jama/buy_it_now.php?'+aa+'"></iframe>').appendTo($obox);

	})(jQuery);

}
function depositSubmit() {
	
	(function($){
		var $olay = $('<div />').appendTo(document.body).css({zIndex:1000, position:"fixed", height:'100%', width:'100%', background:'#333', left:0, top:0, opacity:0.75, display:'block'});
		var $obox = $('<div />').appendTo(document.body).css({"-moz-box-shadow":'0px 0px 30px #333', "-webkit-box-shadow":'0px 0px 30px #333',zIndex:1001, position:"fixed", height:'450px', width:'750px', background:'white', padding:'10px', display:'block'});
		var closer =  function(){
			$olay.add($obox).remove();
			//window.location.reload();
			return false;
		};
		$olay.bind('click', closer);
		$obox.css({
			left: $(window).width()/2 - $obox.innerWidth()/2,
			top: $(window).height()/2 - $obox.innerHeight()/2
		});
		$('<a href="#" style="position:absolute; top:12px; right:25px; background:white; padding:6px;">Close Window</a>').appendTo($obox).bind('click', closer);
		var aa = 'price='+encodeURI(document.getElementById('deposit_amount').innerHTML)+'&manufacturer='+encodeURI(document.getElementById('manufacturer').value)+'&brand='+encodeURI(document.getElementById('brand_form').value)+'&model='+encodeURI(document.getElementById('model_form').value)+'&stock='+encodeURI(document.getElementById('stock_form').value);
		$('<iframe style="width:100%; height:100%;"  src="http://www.uvsconsole.com/clients/jama/deposit.php?'+aa+'"></iframe>').appendTo($obox);
	})(jQuery);

}
function purchaseSubmit() {
	
	(function($){
		var $olay = $('<div />').appendTo(document.body).css({zIndex:1000, position:"fixed", height:'100%', width:'100%', background:'#333', left:0, top:0, opacity:0.75, display:'block'});
		var $obox = $('<div />').appendTo(document.body).css({"-moz-box-shadow":'0px 0px 30px #333', "-webkit-box-shadow":'0px 0px 30px #333',zIndex:1001, position:"fixed", height:'450px', width:'750px', background:'white', padding:'10px', display:'block'});
		var closer =  function(){
			$olay.add($obox).remove();
			//window.location.reload();
			return false;
		};
		$olay.bind('click', closer);
		$obox.css({
			left: $(window).width()/2 - $obox.innerWidth()/2,
			top: $(window).height()/2 - $obox.innerHeight()/2
		});
		$('<a href="#" style="position:absolute; top:12px; right:25px; background:white; padding:6px;">Close Window</a>').appendTo($obox).bind('click', closer);
		var aa = 'price='+encodeURI(document.getElementById('totalprice').innerHTML)+'&manufacturer='+encodeURI(document.getElementById('manufacturer').value)+'&brand='+encodeURI(document.getElementById('brand_form').value)+'&model='+encodeURI(document.getElementById('model_form').value)+'&stock='+encodeURI(document.getElementById('stock_form').value);
		$('<iframe style="width:100%; height:100%;"  src="http://www.uvsconsole.com/clients/jama/purchase.php?'+aa+'"></iframe>').appendTo($obox);
	})(jQuery);

}



// display media tabs
function mediaTabs(listid, selected) {
	
	// call helper to display the appropriate media tabs
	new Ajax.Updater("mediatabs", "../../../../helpers/helpers.inventory/helper.inventory.tabs.php", { method:"post", parameters: { listid:listid, selected:selected }});
}

// toggle the media display on the listing details page
function mediaMode(listid, mode) {
	
	// call helper to update media display based on selected mode
	new Ajax.Updater("mediadisplay", "../../../../helpers/helpers.inventory/helper.inventory." + mode + ".php", { method:"post", parameters: { listid:listid },
		evalScripts:true,
		onLoading:function() {
			
			// display the loading message
			$("mediadisplay").update("<div style='width:400px;height:300px;'>Loading...</div>");
			
		},
		onComplete:function() {
			
			// update the media tabs
			mediaTabs(listid, mode);
		}
	});
}

// toggle the current video being displayed
function videoSwitch(listid, playnow) {
	
	// call helper to update media display based on selected mode
	new Ajax.Updater("mediadisplay", "../../../../helpers/helpers.inventory/helper.inventory.videos.php", { method:"post", parameters: { listid:listid, playnow:playnow }});
}

// toggle the current virtual tour being displayed
function tourSwitch(listid, playnow) {
	
	// call helper to update media display based on selected mode
	new Ajax.Updater("mediadisplay", "../../../../../helpers/helpers.inventory/helper.inventory.tours.php", { method:"post", parameters: { listid:listid, playnow:playnow }});
}

// generate brochure download link
function brochureLink(brochure) {

	// call helper to fetch brochure link
	new Ajax.Updater("brochure", "../../../../helpers/helpers.inventory/helper.inventory.brochure.php", { method:"post", parameters: { brochure:brochure }});
}

// calculate monthly payment
function calculate() {
	
	// collect the information entered in the form
	var rate   = parseFloat($("rate").value) / 100;
	var months = parseInt($("months").value);
	var price  = $("price").value.sub(',', '');
	var trade  = $("tradein").value.sub(/[,$]/, '', 2);
	
	// convert price and trade values to integers
	if(price) price = parseInt(price);
	if(trade) trade = parseInt(trade);
	
	// continue if the required fields have been completed
	if(rate != null && months > 0 && price > 0 && price > trade) {
		
		// calculate the monthly payment
		price = price - trade;
		var payment = (price * (rate / 12)) / (1 - Math.pow((1 + rate / 12), -months));
		payment = payment.toFixed(2);
		
		// update the monthly payment display
		$("paymentwrap").update(payment);
	}
}

// expand or contract embeded wrapper
function embedExpand(wrapper) {
	
	// get the current height of the wrapper
	var height = $(wrapper + "wrapper").getHeight();
	
	// expand wrapper
	if(height == 0) {
		
		// determine the height of the form
		var expandto = $(wrapper + "inner").getHeight();
		
		// expand the wrapper
		new Effect.Morph(wrapper + "wrapper", {
			style:"height:" + expandto + "px",
			duration:0.4,
			afterFinish:function() {
				
				// fade in the contact wrapper
				new Effect.Appear($(wrapper + "inner"), { duration:0.2 });
			}
		});
	}

	// collapse wrapper
	else {

		// fade out the wrapper contents
		new Effect.Fade($(wrapper + "inner"), {
			duration:0.2,
			afterFinish:function() {
				
				// collapse the wrapper
				new Effect.Morph(wrapper + "wrapper", { style:"height:0px", duration:0.4 });
			}
		});
	}
}

// process embeded form
function embedSubmit(form) {
	
	// verify that the required information has been provided
	//var check = embedCheck(form + "form");
	
	// continue if the required information has been provided
	if(formCheck(form + "form", false) != false) {
		
		// hide the submit link
		$(form + "submit").update("Working...");
		
		// call helper to process the form
		new Ajax.Request("../../../../helpers/helpers.embeded/helper.embeded." + form + ".php", { method:"post", parameters: $(form + "form").serialize(true),
			onComplete:function(transport) {
				
				// store helper response
				var response = transport.responseText;
				
				// continue if the form was processed successfully
				if(response.startsWith("SUCCESS")) {
					
					// display confirmation alert
					alert("Thank you. Your message has been sent successfully.");
					
					// display the submit link
					$(form + "submit").update("<a href='#' onclick='embedSubmit(\"" + form + "\");return false;'>Submit</a>");
					
					// collapse wrapper
					embedExpand(form);
				}
				
				// display error message
				else {
					
					// display error mesasge
					alert(response);
										
					// display the submit link
					$(form + "submit").update("<a href='#' onclick='embedSubmit(\"" + form + "\");return false;'>Submit</a>");
				}
			}
		});
	}
}

// process comparison request
function compare() {
	
	// collect the selected units
	var selected = $("compare").serialize();
	
	// parse the serialized string
	selected = selected.split("&");
	
	// initialize the parameters string
	var parameters = "";
	
	// process the parse string
	selected.each(function(item) {
		
		// parse the string
		item = item.split("=");
		
		// store the unit id
		parameters = parameters + item[1] + ",";
	});
	
	// continue if the correct number of units has been selected
	if(selected.length > 1 && selected.length < 4) {
		
		// forward the user to the comparison page
		window.location = "compare.php?selected=" + parameters
	}
	
	// display error message
	else {
		alert("Please select no more than 3 units to compare.");
	}
}

// toggle highlight of rental option
function rentalOption(id) {
	
	// continue if the option is selected
	if($("option" + id).checked == true) {
		
		// highlight the selected option
		$(id).addClassName("highlight");
	}
	
	else {
		
		// remove highlight from option
		$(id).removeClassName("highlight");
	}
}
