/* Braincache JS Core, part of project Dauphine */

function jumpto(pageloc) {
	if (pageloc!="") {
		parent.location='/'+pageloc;
	}
}

function switch_currency(currency_id) {
	var request = new Request({
		method:'get',
		url:'/dauphine/js-switch-currency.php',
		data:'newid='+currency_id+'&loc='+parent.location,
		onSuccess:function(resHTM,resXML){
			jumpto(resHTM);
			//alert(resHTM);
		}
	}).send();
}

function switch_country(country_id) {
	var request = new Request({
		method:'get',
		url:'/dauphine/js-switch-countries.php',
		data:'newid='+country_id+'&loc='+parent.location,
		onSuccess:function(resHTM,resXML){
			jumpto(resHTM);
		}
	}).send();
}

// Multibox simulation
// Written by Robert 'telemichus' B.
// Braincache JS Labs
// 29.07.2008
// Upd 03.10.2008 - Close button
// ----------------------------------
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.Quart);
	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.6);
	
	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 description = new Element('div',{
				'id':'imagedescription',
				'html':imgtitle,
				'styles':{
					'width':Number(imgx)+30,
					'height':'15px',
					'line-height':15,
					'border':'5px solid #000',
					'background-color':'#000',
					'color':'#fff',
					'font-family':'Arial',
					'font-size':'11px',
					'font-weight':'bold',
					'position':'absolute',
					'top':Number(imgy),
					'left':-20,
					'z-index':3104
				}
			});
			var timed = setTimeout(function(){
				var closer = new Element('img',{
					'id':'imgcloser',
					'src':'js/close.png',
					'styles': {
						'position':'absolute',
						'top':-10,
						'left':Number(imgx)-13,
						'z-index':3105
					}
				});
				ih.adopt(img);
				img.fade(1);
				ih.adopt(description);
				ih.adopt(closer);
				clearTimeout(timed);
			},300);
		}
	});
return false;
}

function filter_data(searchpattern,maxrows) {
	var a = 1;
	while ( a <= maxrows  ) {
		var dt = $("td"+a).innerHTML.toLowerCase();
		var sp = searchpattern.toLowerCase();
		var tr = "tr"+a;
		if (sp != "") {
			if (dt.match(""+sp) == null) {
				$(tr).style.display = 'none';
			} else {
				$(tr).style.display = '';
			}
		} else {
			$(tr).style.display = '';
		}
		a = a + 1;
	}
}

/* Technica Import ;) */
function AddToCart(product) {
	var stripp = product.substring(1,10);
	//alert(stripp);
	var request = new Request({
		method:'get',
		url:'dauphine/js-addtocart.php',
		data:'product='+stripp,
		onSuccess: function(outMsg,outXML) {
			var res = outMsg.split('|');
			$('shopper').innerHTML = '('+res[0]+')';
			if(res[0]>0) {
				$('checker').style.display = '';
			}
			announce('<span style="color:#060">Success</span>','Product added to your cart.');
		}
	}).send();
}

function UpdateCart(quant,product) {
	var seethis = new RegExp("[1-9]","g");
	if (seethis.test(quant)) {
		var request = new Request({
			method:'get',
			url:'dauphine/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]+')';
						$('checker').style.display = '';
					} else {
						$('shopper').innerHTML = '';
						$('checker').style.display='none';
						$('scart').style.display='none';
						$('noprod').style.display='';
					}
				}
			}
		}).send();
	}
}

function remP(prodid) {
	var askme = confirm("Delete from Shopping Cart?");
	if (askme) {
		$('scart').deleteRow(prodid.parentNode.parentNode.rowIndex);
		var request = new Request({
			method:'get',
			url:'dauphine/js-updatecart.php',
			data:'product='+prodid.id+'&q=0',
			onSuccess:function(outMsg,outXML) {
				if (outMsg != "err") {
					announce('<span style="color:#060">Success</span>','Product removed from cart');
					var res = outMsg.split("|");
					if (res[1] == "") { res[1] = '0'; }
					$('grandtotal').innerHTML = res[1];
					if (res[3] == "0" || res[3] == "") {
						$('shopper').innerHTML = '';
						$('scart').style.display='none';
						$('checker').style.display='none';
						$('noprod').style.display='';
					} else {
						$('shopper').innerHTML = '('+res[3]+')';
						$('checker').style.display='';
					}
				}
			}
		}).send();
	}

}

function IWish(product) {
	var stripp = product.substring(1,10);
	var request = new Request({
		method:'get',
		url:'dauphine/js-addtowish.php',
		data:'product='+stripp,
		onSuccess:function(outMsg,outXML) {
			announce('<span style="color:#060">Success</span>','The product has been added to your wish list');
			$('wisher').innerHTML = '('+outMsg+')';
		}
	}).send();
}

function GotBoredOf(prodid) {
	var stripp = prodid.substring(1,10);
	var askme = confirm("Remove from Wish List?");
	if (askme) {
		$('tr'+stripp).dispose();
		var request = new Request({
			method:'get',
			url:'dauphine/js-updatewish.php',
			data:'product='+stripp,
			onSuccess:function(outMsg,outXML) {
				alert(outMsg);
				var res = outMsg.split("|");
				$('gtotal').innerHTML = res[0];
				if (res[1] != "0") {
					$('wisher').innerHTML = '('+res[1]+')';
				} else {
					$('wisher').innerHTML = '';
					$('wlist').style.display='none';
					$('noprod').style.display='';
				}
			}
		}).send();
	}
}

function IWishToBuy(prod) {
	AddToCart(prod.id);
	GotBoredOf(prod.id);
}

function ValidateCheckout() {
	var name = $('name');
	var surname = $('surname');
	var address = $('address');
	var district = $('district');
	var city = $('city');
	var phone = $('phone');
	var email = $('email');
	name.style.backgroundColor = '#fff';
	surname.style.backgroundColor = '#fff';
	address.style.backgroundColor = '#fff';
	district.style.backgroundColor = '#fff';
	city.style.backgroundColor = '#fff';
	phone.style.backgroundColor = '#fff';
	email.style.backgroundColor = '#fff';
	if (name.value=='') {
		name.style.backgroundColor = '#cc9999';
		name.focus();
		announce('<span style="color:#cc0000">Error</span>','Name should be completed.');
		return false;
	}
	if (surname.value=='') {
		surname.style.backgroundColor = '#cc9999';
		surname.focus();
		announce('<span style="color:#cc0000">Error</span>','Surname should be completed.');
		return false;
	}
	if (address.value=='') {
		address.style.backgroundColor = '#cc9999';
		address.focus();
		announce('<span style="color:#cc0000">Error</span>','Your address should be completed.');
		return false;
	}
	if (district.value=='') {
		district.style.backgroundColor = '#cc9999';
		district.focus();
		announce('<span style="color:#cc0000">Error</span>','Your district name should be completed.');
		return false;
	}
	if (city.value=='') {
		city.style.backgroundColor = '#cc9999';
		city.focus();
		announce('<span style="color:#cc0000">Error</span>','Your city should be completed.');
		return false;
	}
	if (phone.value=='') {
		phone.style.backgroundColor = '#cc9999';
		phone.focus();
		announce('<span style="color:#cc0000">Error</span>','Your phone number should be completed.');
		return false;
	}
	if (email.value=='') {
		email.style.backgroundColor = '#cc9999';
		email.focus();
		announce('<span style="color:#cc0000">Error</span>','Your email should be completed.');
		return false;
	}
}

function validateemail() {
	var email = $('email');
	if (email.value=='') {
		email.style.backgroundColor = '#cc9999';
		email.focus();
		announce('<span style="color:#cc0000">Error</span>','Your email address could not be empty');
		return false;
	}
	return true;
}
function announce(first,second) {
	var screensize = $(document.body).getScrollSize();
	var bodysize = $(document.body).getSize();
	var scrolled = $(document.body).getScroll();
	var messageheight = 40;
	var messagewidth = 600;
	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);
}
	

