window.addEvent('domready', function() {
	alignTD();
});

function setToMax(tdPElms)
{
	if (tdPElms){
		var count = tdPElms.length/4;
		var lines = count.ceil();
		
		var step=0;
		for(var i=0; i<lines; i++){
			var h=0;
			for(var ii=step; ii<4+step; ii++){
				var TD=tdPElms[ii];	
			
				if (TD){
					var tdHeight=TD.getSize().y.toInt();
					if (tdHeight>h){
						h=tdHeight;
					}	
				}
			}
			
			for(var ii1=step; ii1<4+step; ii1++){
				if (tdPElms[ii1]){
					tdPElms[ii1].setStyle('height',h);
				}
			}
			step=step+4;
		}
	}
}

function alignTD(){
	setTimeout("setToMax($$('.tdProductImage'))", 100);
	setTimeout("setToMax($$('.tdProductName'))", 1000);
}

	function open_window(link,w,h) //opens new window
	{
		var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
		newWin = window.open(link,'newWin',win);
		newWin.focus();
	}

	function open_printable_version(link) //opens new window
	{
		var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
		newWin = window.open(link,'perintableWin',win);
		newWin.focus();
	}

	function confirmDelete(id, ask, url) //confirm order delete
	{
		temp = window.confirm(ask);
		if (temp) //delete
		{
			window.location=url+id;
		}
	}
	

	function confirmUnsubscribe() //unsubscription confirmation
	{
		temp = window.confirm('Are you sure you want to terminate your account?');
		if (temp) //delete
		{
			window.location="index.php?killuser=yes";
		}
	}

	function validate() // newsletter subscription form validation
	{
		if (document.subscription_form.email.value.length<1)
		{
			alert("Please enter your email correctly");
			return false;
		}
		if (document.subscription_form.email.value == 'Email')
		{
			alert("Please enter your email correctly");
			return false;
		}
		return true;
	}
	function validate_disc() // review form verification
	{
		if (document.formD.nick.value.length<1)
		{
			alert("Please enter your nickname");
			return false;
		}

		if (document.formD.topic.value.length<1)
		{
			alert("Please enter message subject");
			return false;
		}

		return true;
	}
	function validate_search()
	{

		if (document.Sform.price1.value!="" && ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value)))
		{
			alert("Price should be a positive number");
			return false;
		}
		if (document.Sform.price2.value!="" && ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value)))
		{
			alert("Price should be a positive number");
			return false;
		}

		return true;
	}
	

		function menuOver(subItemID){				
			if (document.getElementById('sub_'+subItemID)){				
				document.getElementById('sub_'+subItemID).style.display='block';
			}
		}
		
		function menuOut(subItemID){	
			if (document.getElementById('sub_'+subItemID)){
				document.getElementById('sub_'+subItemID).style.display='none';
			}
		}
		
		function copyUrl(form){	
			var str=$('str').value.replace(/ /gi, "+");			
			form.action+=str+".html";
		}
		
	function commitFlashObject(_obj, _container){
		_output=""
		_paramoutput=""
		_src=""
		_ver=""
		for(_cO in _obj){
			_output+=_cO+"=\""+_obj[_cO]+"\" "
			_paramoutput+="<param name="+_cO+" value=\""+_obj[_cO]+"\">";
			if(_cO=="movie")_src="src=\""+_obj[_cO]+"\"";
			if(_cO=="version")_ver=_obj[_cO];
		}
		if(_ver=="")_ver="8,0,0,0"
		ihtm="<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+_ver+" "+_output+">\n"
		ihtm+=_paramoutput+"\n"
		ihtm+="<embed "+_src+" pluginspage=http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash type=application/x-shockwave-flash "+_output+">\n";
		ihtm+="</embed>\n";
		ihtm+="</object>\n";
		document.getElementById(_container).innerHTML=ihtm	
	}	
	
	function showAuthorisationForm(product){
		var leftPos=(window.getSize().x/2)-(300/2).toInt();		
		var topPos=300;
		
		var container = new Element('div',{
			'styles':{
				'position':'absolute',
				'width':'300px',				
				'border':'1px solid #000',
				'background':'#EFE4D6',
				'left':leftPos,
				'top':topPos
			}
		});
		
		var htmlCode='<div align="right" style="padding:3px 5px;"><a href="#" id="closeBtn" onclick="return false;">X</a></div><div style="padding:10px;" align="center"><b style="color:green;">Please login to leave a review. We require this for the integrity of the product reviews. Thank you.</b><br /><br /><form id="frm" action="/auth/" method="post"><table width="100%"><tr><td width="1%" nowrap="nowrap">E-mail address:</td><td><input type="hidden" value="1" name="enter"/><input type="text" name="user_login" id="ln_user_login" style="width:100%;" ></td></tr><tr><td width="1%" nowrap="nowrap" valign="top" style="padding-top:3px;">Password:</td><td><input type="password" name="user_pw" style="width:100%;"><br />* Password is case sensitive</td></tr></table><input type="hidden" name="ajax"><input type="hidden" name="prod_id" value="'+product+'"><div id="result" style="display:none;color:red;padding:10px 0;">Error! Incorrect Data</div><br /><input type="submit" value="  Login  "></form></div>';
		
		container.set('html',htmlCode);

		container.inject(document.body);
		
		$('closeBtn').addEvent('click',function(){			
			container.dispose();

		});
		
		$('frm').addEvent('submit',function(){
		
			var pars = this.toQueryString();
			
			new Request({
				url: "/", 
				onComplete: function(res){								
					var result = res.trim().split('|');					
					if (result[0]=="false"){						
						$('result').setStyle('display','block');
					}
					else if (result[0]=="true"){
						$('product_id').value=result[1];						
						$('userID').value=result[2];
						$('reviewBtn').removeProperty('onclick');
						container.dispose();
						if (result[3]=="1"){
							$('reviewForm').submit();
						}
						else{
							$('reviewBtn').removeProperty('onclick');
							$('reviewBtn').removeEvent('click');
							$('reviewBtn').addEvent('click',function(){
								alert('In order to preserve the integrity of our product reviews we only allow reviews by those who have purchased the product directly from our website with a registered user name. Thank you.');								
								$('reviewForm').setProperty('onsubmit','return false;');
							});
							alert('In order to preserve the integrity of our product reviews we only allow reviews by those who have purchased the product directly from our website with a registered user name. Thank you.');
						}
					}
				}				
			}).send(pars);
		});
	}
	
	function addToCart(form,url){
		
		var showProducts = function(form){
		
			var leftPos=(window.getSize().x/2)-(400/2).toInt();	
			var topPos=window.getScroll().y.toInt();	
			
			var overlay=new Element('div', {
				'id':'cartOverlay',
				'styles': {
					'width': '100%',	
					'height': window.getScrollSize().y,
					'position':'absolute',
					'background-color':'#000',
					'left':0,
					'top':0,		
					'z-index':10000
				}				
			});
			overlay.setOpacity(0.8);		
						
			if (!$('winContainer')){
				var windowContainer = new Element('div',{
					'id':'winContainer',
					'styles':{
						'width':'410px',
						'border':'5px solid #666666',
						'background':'#fff',
						'z-index':10001,
						'position':'absolute',
						'top':topPos,
						'left':leftPos
					}
				});
				
				var closeArea = new Element('div',{
					'align':'right',			
					'styles':{
						'background':'#d3e2bd',
						'padding':'3px 7px'
					}
				}).inject(windowContainer);
				
				var closeBtn = new Element('a',{
					'href':'#',
					'styles':{						
						'color':'#366545',
						'text-decoration':'none'
					},
					'onclick':'return false;'
				}).appendText('close X');
					
				closeBtn.inject(closeArea);
				
				
				var cartContentDiv=new Element('div',{
					'id':'cartContent',
					'styles':{
						'padding':'0'
					}
				}).inject(windowContainer);
				
				overlay.inject(document.body,'top');
				windowContainer.inject(document.body);
				windowContainer.makeDraggable();
				$('cartContent').set('html','<div align="center" style="padding:15px 0;"><img src="/images/ajax-loader.gif"></div>');
				
				//alert(code);
				
				closeBtn.addEvent('click',function(){
					windowContainer.dispose();
					overlay.dispose();
					window.removeEvents('scroll');	
					window.removeEvents('resize');
					return false;
				}.bind(this));
			}
			else{
				$('winContainer').setStyles({
					'top':0,
					'left':180
				});
			}
		}
		
		var moveEff;
		
		var move = function(){		
			var wSize=window.getSize();	
			var pos2=window.getScroll();
			var leftPos=pos2.x+((wSize.x/2)-(410/2)).toInt();
			var topPos=pos2.y+((wSize.y/2)-(400/2)).toInt();
			
			if (topPos<0) topPos=0;
			
			if (moveEff){moveEff.cancel();}
			
			moveEff=new Fx.Morph($('winContainer'),{
				duration: 1500, 
				transition: Fx.Transitions.Elastic.easeOut
			});
			
			moveEff.start({'left': leftPos,'top': topPos});	
			
		}
		
		showProducts(form);
		move();
		window.addEvent('scroll', move);
		
		var pars = $(form).toQueryString();

		new Request({
			url: url, 
			onComplete: function(res){		
				var r = res.split('||');		
				$('cartContent').set('html','');
				$('cartContent').set('html','<div style="padding:10px 3px;font-size:18px;color:#366545;">Your Shopping Cart<div style="padding-top:3px;border-bottom:2px solid #d3e2bd;"></div></div>'+r[0]);
				
					$('free_shipping').set('html', r[1]);
					$('top_cart').set('html', r[2]);


				$('keep_s').addEvent('click',function(){
					$('winContainer').dispose();
					$('cartOverlay').dispose();
				});
			}				
		}).send(pars);

	}
	
function register(form){
	if($('cust_password1').value!="" && $('cust_password2').value!=""){
		$(form).action="register.html";
		$('email').name="login";
		$(form).submit();		
	}
	else{
		$(form).submit();
	}
}

function sortProducts(sel){
	var res=sel.value;
	var form=$('sortForm1');	
		
	if (res!="new"){
		var pars=res.split('|');
		var field=pars[0];
		var sort=pars[1];	
	}
	else{
		var field="date_added";
		var sort="desc";	
	}
	var catID=$('catID').value;
	
	$('ajaxSortLoader').setStyle('visibility','visible');
	new Request({
		url: '/index.php?categoryID='+catID+'&sort='+field+'&direction='+sort+'', 
		onComplete: function(res){				
			$('prods').set('html',res);
			$('ajaxSortLoader').setStyle('visibility','hidden');
			alignTD();
		}				
	}).send('ajax=true');
}
