var base = "http://www.pcbtrain.co.uk/";

function doLoading(){
	//set the loading icon.
	$("step").style.display = "none";
	$("loading").style.display="block";
}

function doFlash(){
	//var myFx = new Fx.Style('opac2', 'opacity').start(0,1);
	var myFx = new Fx.Style('price_table', 'opacity').start(0,1);	
}

function addPcb(pcb){
	if(pcb.length > 0){
		doLoading();
		var addPcbTrain = new Ajax(base +'quote.php?addPcb=' + pcb + '&ajax=1', {method:'get', update:$('ajax')}).request();
		addPcbTrain.addEvent('onComplete', function(){
			doLoading();
			window.location.href = base +'quote-and-order-pcb/';
		});
	}
}

function setExchange(rate){
	if(rate > 0){
		doLoading();
		var addExchange = new Ajax(base +'quote.php?addExchange=' + rate + '&ajax=1', {method:'get', update:$('ajax')}).request();
		addExchange.addEvent('onComplete', function(){
			TB_init();
		});
	}
}

function doConv(){
	var x = document.getElementById("x").value;
	var y = document.getElementById("y").value;
	var tryConv = new Ajax(base +'quote.php?act=conv&x='+x+'&y='+y+'&ajax=1', {method:'get', update:$('ajax')}).request();
	tryConv.addEvent('onComplete', function (){
		doFlash();
		TB_init();
	});
}

function setDay(day){
	if(day.length > 0 && day > 0){
		var settingDay = new Ajax(base +'quote.php?act=scheduled_day&value=' + day +'&ajax=1', {method:'get', update:$('ajax')}).request();
		settingDay.addEvent('onComplete', function (){
			doFlash();
			TB_init();
		});
	}
}

function setQuantity(qty){
	if(qty.length > 0 && qty > 0){
		window.location.href = base+'quote.php?act=quantity&value=' + qty;
	}
}

function startAgain(){
	var goClear = new Ajax(base +'quote.php?start=again&ajax=1', {method:'get', update:$('ajax')}).request();
	goClear.addEvent('onComplete', function(){
		TB_init();
		var goClear2 = new Ajax(base +'quote.php?start=again&ajax=1', {method:'get', update:$('ajax')}).request();
		goClear2.addEvent('onComplete', function(){
			TB_init();
		});
	});
}

function changeOptions(quantity, scheduled_day,et){
	if(quantity.length > 0 && scheduled_day.length >0 && et.length >0 ){
		window.location.href = base +'quote.php?act=update&quantity='+quantity +'&scheduled_day='+scheduled_day + '&et='+et;
	}
}

function doDelivery(){
	var myFx = new Fx.Style('delivery_table', 'opacity').start(0,1);	
}

function addDelivery(option){
	var operator = $(option);
	var url = base + 'quote.php?ajax=1&act=setDelivery&pk_option=' + operator.value;
	if(url.length > 0){
		var sendDelivery = new Ajax(url, {method:'get', update:$("ajax")}).request();
		sendDelivery.addEvent('onComplete', function (){
			doDelivery();			
			TB_init();
		});
	}
}

function goFrm(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if(keycode == 13){
		doConv();
		return false;
  } else {
		return true;
	}
}

function quoteOptions(o){
	if(o.length > 0){
		$("log_show").style.display = "";
		switch(o){
			case "save" :
				$("log").innerHTML = "We are currently saving your quote....";
				break;
			case "email" :
				$("log").innerHTML = "We are currently emailing your quote....";
				break;
		}
	}
	var action = new Ajax(base +'quote.php?ajax=1&quote_options='+o, {method:'get', update:$("ajax")}).request();
	action.addEvent('onComplete', function (){
		TB_init();
		$("log_show").style.display = "";
		if(o == "save"){
			//$("log").innerHTML = "Your quote has been saved. This quote will expire in 14 days.";
		} else if(o == "email"){
			//$("email").innerHTML = "Your quote has been emailed.";
		}
	});
}

function setInfo(){
	var pcb_ref = escape($("pcb_ref").value);
	var quote_emails = $("quote_emails").value;
	var pcb_options = $("pcb_options").value;
	if(pcb_ref.length > 0 && quote_emails.length > 0){
		document.getElementById("pcb_option_errors").innerHTML = "";
		var save = new Ajax(base +'quote.php?ajax=1&pcb_options='+pcb_options+'&pcb_ref=' + pcb_ref + '&emails=' + quote_emails, {method:'get', update:$("ajax")}).request();
		save.addEvent('onComplete', function(){
			TB_init();
			document.getElementById("pcb_option_errors").innerHTML = "";
		});
	} else {
		document.getElementById("pcb_option_errors").innerHTML = "* Please enter Your PCB ref and Quote Emails.";
	}
}

function goAssembly(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if(keycode == 13){
		SendAssembly();
		return false;
  } else {
		return true;
	}
}

function SendAssembly(){
	var cirqty = $("asscirqty").value;
	var ass = $("asscomp").value;
	var uniqc = $("uniquecomp").value;
	var bga = $("bgacomp").value;
	
	var url = base + 'quote.php?ajax=1&ass=y&asscirqty=' + cirqty + '&asscomp=' + ass + '&uniquecomp='+uniqc + '&bgacomp='+bga;
	var UpdateAssembly = new Ajax(url, {method:'get', update:$("ajax")}).request();
	UpdateAssembly.addEvent('onComplete', function(){
		TB_init();	
	});
}

function updateAssembly(day,qty){
	if(day.length > 0 && qty.length > 0){
		var url = base + 'quote.php?ajax=1&update=assembly&day=' + day + '&qty='+qty;
		var updater = new Ajax(url, {method:'get', update:$("ajax")}).request();
		updater.addEvent('onComplete', function(){
			var myFx = new Fx.Style('price_table', 'opacity').start(0,1);	
			TB_init();
		});
	} else {
		alert("System Error, Please try again.");
	}
}

function loadQuote(id){
	if(id > 0){
		if(confirm("This will clear any current quotes you have loaded, do you want to continue?")){
			window.location.href = base + "login.php?loadQuote=" + id;
		}
	}
}