// JavaScript Document
var station_base_location = 'http://www.nickautomotive.com/';

function announce(first,second) {
	var screensize = $(document.body).getScrollSize();
	var bodysize = $(document.body).getSize();
	var scrolled = $(document.body).getScroll();
	var messageheight = 40;
	var messagewidth = 800;
	var popbox = new Element('div',{
		'id':'popmsg',
		'styles':{
			'background-color':'#000',
			'display':'inline',
			'position':'absolute',
			'top':((bodysize.y/2)-20)+scrolled.y+'px',
			'left':(bodysize.x/2)-(messagewidth/2)+'px',
			'width':messagewidth,
			'height':messageheight,
			'z-index':100000,
			'line-height':messageheight,
			'font-family':'Arial',
			'font-size':'20px',
			'color':'#fff',
			'text-align':'center',
			'opacity':0,
			'border':'5px solid #fff'
		}
	}).inject($(document.body),'top');
	popbox.innerHTML = first+' '+second;
	popbox.fade(1);
	var timed = setTimeout(function(){ 
			popbox.fade(0)
			var timed2 = setTimeout(function(){
				popbox.dispose();
				clearTimeout(timed2);},400);
			clearTimeout(timed); },3000);
}
function ovl(imagefile,imgtitle) {
	var screensize = $(document.body).getScrollSize();
	var bodysize = $(document.body).getSize();
	var scrolled = $(document.body).getScroll();
	var imgloaded = false;
	var Transition = new Fx.Transition(Fx.Transitions.Sine);
	var MainContainer = new Element('div',{
		'id':'maincontainer',
		'styles':{
			'width':screensize.x,
			'height':screensize.y,
			'position':'absolute',
			'top':0,
			'left':0,
			'z-index':3000,
			'background-color':'#000',
			'opacity':0,
			'cursor':'pointer'
		},
		'events':{
			'click':function(){
				MainContainer.fade(0);
				ih.dispose();
				var timed = setTimeout(function(){ MainContainer.dispose(); clearTimeout(timed); },400); 
			}
		}
	}).inject($(document.body),'top');
	var Iframe = new Element('iframe',{
		'id':'iframecontainer',
		'styles':{
			'position':'absolute',
			'top':0,
			'left':0,
			'width':screensize.x,
			'height':screensize.y,
			'z-index':3001,
			'background-color':'#000'
		},
		'frameborder':0,
		'scrolling':'no'
	}).inject(MainContainer);
	var Container = new Element('div',{
		'id':'divcontainer',
		'styles':{
			'position':'absolute',
			'top':0,
			'left':0,
			'width':screensize.x,
			'height':screensize.y,
			'z-index':3002,
			'background-color':'#000'
		}
	}).inject(MainContainer);
	MainContainer.fade(0.8);
	
	var ih = new Element('div',{
		'id':'bc_container',
		'styles':{
			'position':'absolute',
			'background-color':'#fff',
			'border':'20px solid #000',
			'top':(bodysize.y/2 - 100)+scrolled.y,
			'left':(bodysize.x/2 - 100)+scrolled.x,
			'width':200,
			'height':200,
			'z-index':31000,
			'opacity':0,
			'text-align':'center',
			'cursor':'pointer'
		},
		'events':{
			'click':function(){
				MainContainer.fireEvent('click');
			}
		}
	}).inject($(document.body));
	
	var loader = new Element('img',{ 
		'id':'loaderanim',
		'src':'js/loader.gif',
		'styles': {
			'position':'absolute',
			'top':86,
			'left':86,
			'z-index':3101
		}
	});
	ih.fade(1);
	ih.adopt(loader);
	var ImageFile = new Asset.image(imagefile, { 
		'title':imgtitle,
		'alt':imgtitle,
		'onload':function(img){ 
			$('loaderanim').dispose();
			var imgx = img.getProperty('width');
			var imgy = img.getProperty('height');
			var currentx = ih.getProperty('width');
			var currenty = ih.getProperty('height');
			var currenttop = bodysize.y/2 - 100;
			var currentleft = bodysize.x/2 - 100;
			var newtop = (bodysize.y/2 - imgy/2)+scrolled.y;
			var newleft = (bodysize.x/2 - imgx/2)+scrolled.x;
			ih.set('morph',{duration:300, transition:Transition.easeInOut });
			img.setStyles({
				'top':0,
				'left':0,
				'opacity':0,
				'z-index':3103
			});
			ih.setStyle('line-height',0);
			ih.morph({
				'height': imgy,
				'width': imgx,
				'top': newtop,
				'left': newleft
			});
			var timed = setTimeout(function(){
				ih.adopt(img);
				img.fade(1);
				clearTimeout(timed);
			},300);
		}
	});
return false;
}

function RequestModels(maketype) {
	var request = new Request({
		method:'get',
		url:'includes/requestmodels.php',
		data:'make='+maketype,
		onSuccess:function(outMsg,outXML){
			if (outMsg!= "") {
				var models = $('models');
				models.options.length = 0;
				var newo = document.createElement('option'); newo.text = 'Please select...'; newo.value = ""; try { models.add(newo,null); } catch(ex) { models.add(newo); }
				outMsg.split("|").each(function(item) {
					mo = item.split("~");
					var newo = document.createElement('option');
					newo.value = mo[0];
					newo.text = mo[1];
					try { models.add(newo,null); }
			  		catch(ex) { models.add(newo); }
				});
			}
		}
	}).send();
}

function RequestYears(modeltype) {
	var request = new Request({
		method:'get',
		url:'includes/requestyears.php',
		data:'model='+modeltype,
		onSuccess:function(outMsg,outXML){
			if (outMsg!= "") {
				var years = $('years');
				years.options.length = 0;
				var newo = document.createElement('option'); newo.text = 'Please select...'; newo.value = ""; try { years.add(newo,null); } catch(ex) { years.add(newo); }
				outMsg.split("|").each(function(item) {
					var newo = document.createElement('option');
					newo.value = item;
					newo.text = item;
					try { years.add(newo,null); }
			  		catch(ex) { years.add(newo); }
				});
			}
		}
	}).send();
}

function UpdateBrandSearch(newaction) {
	$('searchbybrand').action = newaction;
}

function AddToCart(product,productname) {
	var stripp = product.substring(1,10);
	var req = new Request({
		method:'get',
		url:'includes/js-addtocart.php',
		data:'product='+stripp,
		onSuccess:function(outMsg,outXML){
			var res = outMsg.split('|');
			$('shopper').innerHTML = res[0]+' items in cart';
			var spc = res[2].split('~');
			var gt = res[3];
			$('subtotal').innerHTML = '<strong>Subtotal: '+gt+' EUR</strong>';
			announce('<span style="color:#009900;">Success</span>','The product has been added to your cart');
		},
	}).send();
}

function UpdateCart(quant,product) {
	var seethis = new RegExp("[1-9]","g");
	if (seethis.test(quant)) {
		var req = new Request({
			method:'get',
			url:'includes/js-updatecart.php',
			data:'product='+product+'&q='+quant,
			onSuccess:function(outMsg,outXML){
				if (outMsg != "err") {
					var res = outMsg.split("|");
					$('Rtotal'+res[2]).innerHTML = res[0];
					$('grandtotal').innerHTML = res[1];
					if (res[3] > 0 || res[3] != "") {
						$('shopper').innerHTML = res[3]+' items in cart.';
						$('subtotal').innerHTML = '<strong>Subtotal: '+res[0]+' EUR</strong>';
					} else {
						$('shopper').innerHTML = 'No products in cart.';
						$('scart').style.display='none';
						$('noprod').style.display='';
						$('subtotal').innerHTML = '<strong>Subtotal: 0 EUR</strong>';
					}
				}
			}
		}).send();
	}
}

function remP(prodid) {
	var askme = confirm("Delete from Shopping Cart?");
	if (askme) {
		var req = new Request({
			method:'get',
			url:'includes/js-updatecart.php',
			data:'product='+prodid.id+'&q=0',
			onSuccess:function(outMsg,outXML){
				if (outMsg != "err") {
					$('scart').deleteRow(prodid.parentNode.parentNode.rowIndex);
					var res = outMsg.split("|");
					if (res[1] == "") { res[1] = '0'; }
					$('grandtotal').innerHTML = res[1];
					if (res[3] == "0" || res[3] == "") {
						$('shopper').innerHTML = 'No products in cart.';
						$('scart').style.display='none';
						$('noprod').style.display='';
						$('subtotal').innerHTML = '<strong>Subtotal: 0 EUR</strong>';
					} else {
						$('shopper').innerHTML = res[3]+' items in cart.';
						$('subtotal').innerHTML = '<strong>Subtotal: '+res[1]+' EUR</strong>';
					}
				}
			}
		}).send();
	}

}

function ScanForHumans(validation,publickey) {
	var req = new Request({
		method:'get',
		url:'js-humanscan.php',
		data:'validation='+validation+'&publickey='+publickey,
		onSuccess:function(outMsg,outXML){
			if (outMsg=="1") {
				var SubmitButton = new Element('input', {
					'type': 'submit',
					'value': 'Submit',
					'id':'submit'
				});
				SubmitButton.inject($('Splaceholder'));
			} else {
				if ($('myform').getElements('input[type=submit]')!="") {
					$('submit').dispose();
				}
			}
		}
	}).send();
}

function CustomerValidation() {
	var key = 0;
	var testing = true;
	var ids = new Array("name","email","phone","validation");
	var msg = new Array(
	"Please type in a name",
	"Invalid email address",
	"Please type in a phone",
	"Please prove you are a human. Copy the text in the image."
	);
	ids.each(function(item){
		if (testing) {
			var txtval = $(''+item).value;
			if (!txtval || txtval == "" || txtval == "0") {
				announce('<span style="color:#cc1300;">Error</span>',msg[key]);
				testing = false;
			}
			key++;
		}
	});
	return testing;
}

function changepic(newpic,pressid) {
	var pics = Array("pic1","pic2","pic3","pic4","pic5");
	$('mainimage').src = 'phpThumb.php?src='+newpic+'&w=300&h=300&far=C&bg=ffffff&bc=ffffff&q=90&fltr[]=wmi|images/watermark_nick.png|BC|90';
	$('mainimage').removeEvents('click');
	$('mainimage').addEvent('click',function(){ ovl('phpThumb.php?src='+newpic+'&w=800&h=600&far=C&bg=ffffff&bc=ffffff&q=90&fltr[]=wmi|images/watermark_nick1.png|BC|90',''); });
	pics.each(function(item){ if(item==pressid) { $(''+item).fade(0.5); } else { $(''+item).fade(1); } });
}
function ValidCheckout() {
	var key = 0;
	var testing = true;
	var ids = new Array("firstname","lastname","phone","city","address","zip","email");
	var msg = new Array(
	"Please type in a first name",
	"Please type in a last name",
	"Please type in a phone",
	"Please type in a city",
	"Please type in your address",
	"Please complete the postal code",
	"Invalid email address"
	);
	ids.each(function(item){
		if (testing) {
			var txtval = $(''+item).value;
			if (!txtval || txtval == "" || txtval == "0") {
				announce('<span style="color:#cc1300;">Error</span>',msg[key]);
				testing = false;
			}
			key++;
		}
	});
	return testing;
}
