
/**
	
**/

		function getTradeinValue(val) {
			if (val.selectValue) {
				val = val.selectValue;
			}
		
			jQuery.getJSON("/trade-in",{ajax:1,value:val},getTradeinValueCallback); 
			$("#trade_worth").fadeOut(100);
		}	 

	

		function getTradeinValueCallback(item) {
			$("#phoneImage").attr("src",item.image_url);
			$("#phoneValue").html(item.display_price);
			$("#phoneName").html(item.manufacturer + ' ' + item.product_name);
			$("#trade_worth_content").show();
			$("#trade_worth").fadeIn(100);
	//		$("#trade_address").fadeIn(100);
			$("#phoneId").attr("value",item.product_id);
			
		}

		function getPhones(make) {
			
			jQuery.getJSON("/trade-in",{ajax:1,manufacturer:make},getPhonesCallback);
		}

		function getPhonesCallback(items) {
			//add values
			$("#model").html("<option>Select model</option>");
		
			for (x in items) {
			//	alert(item[0] + ' ' + item.product_name);
				$("#model").append("<option value='" + items[x].manufacturer + ' ' + items[x].product_name + "'>" + items[x].manufacturer + ' ' + items[x].product_name + "</option>");				
			}
		}
	
		function submitForm(form) {
			var formItems = $("#" + form + " input");

			
			
			jQuery.post("/trade-in?ajax=1",$(formItems).serialize(),getFormResult);
			

			$("#" + form + " input").attr("disabled","1");
			
		}

		
		
		function getFormResult(result) {
			$("#phoneForm input").attr("disabled","");

			$("#trade_formstatus").html("");
			$("#phoneForm input").removeClass("trade_error");
			$("#phoneForm label").removeClass("trade_error");
			//alert(result.match("Done"));
			
			if (result.match("Done")=="Done") {
				$("#select_phone_form input").attr("disabled","1");
				$("#select_phone_form select").attr("disabled","1");

				$("#trade_success").fadeIn(100);
				$("#trade_address").fadeOut(100);
				
			}
			else {
				result = eval("(" + result + ")");
				var string = "";
				$("#trade_formstatus").html("Oops! It looks like you've missed something...");
				
				for (x in result) {
					$("#" + x).attr("title",result[x]); 
					$("#" + x).addClass("trade_error");
					$("#" + x).bind("mouseenter", function() {
						showFormError(this);
						
					})
					$("#" + x).bind("keydown", function() {
						$(this).removeClass("trade_error");
						$(this).unbind("mouseenter");
					});
								
					
					$("#" + x).bind("mouseleave", function() {
						$("#trade_formError").html("");
						$("#trade_formError").hide();
					})
				}

 
				
			}

			
		}

		function showFormError(element) {
			$("#trade_formError").html($(element).attr("title"));
			var pos = $(element).offset();
			$("#trade_formError").css("top",pos.top + 23 +"px");
			$("#trade_formError").css("left",pos.left + "px");
		
			$("#trade_formError").show();
		}
		
		function load(phonesList) {
			
		        	$("#listofphones").autocompleteArray(phonesList ,
		        		 {
		        		 delay:10,
		        		 minChars:1,
		        		 matchSubset:1,
		        		 matchContains:1,
		        		 autoFill:true,
		        		 maxItemsToShow:20,
		        		 onItemSelect:getTradeinValue
		        		 } );
		        		 
		        	$("#phoneForm input:text").attr("rel",function() {
		        		return $(this).attr("value");
		        	});
		        	
		        	$("#phoneForm input:text").attr("title",function() {
		        		return $(this).attr("value");
		        	});
		        	
		        	//Clear the contents on click
		        		$("#phoneForm input:text").bind("focus", function() {
		        			//	$(this).attr("rel",$(this).attr("value"));
		        			if (this)
		        			if ($(this).attr("value")==$(this).attr("rel")) {	
		        				$(this).attr("value","");
		        				$(this).css("color","#000000");

		        			}		        		
		        		
		        		}	
		        		);
		        		
		        		//Restore the contents on mouseout
		        		//if they're blank
		        		$("#phoneForm input:text").bind("blur", function() {
		        			$("#phoneForm input:text").each(function() {
	        					if ($(this).attr("value")=="") {	        					
	        						$(this).attr("value", $(this).attr("rel"));
			        				$(this).css("color","#afa17f");

	        					}	
	        						
		        			});
		        		});
			
			
		}


		