/* Copyright (c) 2007 Affno (Pvt) Ltd, . All rights reserved.

 This software is the confidential and proprietary information of        
 Affno  ("Confidential Information").  You shall not disclose such 
 Confidential Information and shall use it only in accordance with
 the terms of the license agreement you entered into with Affno.

Module Name		: ODLWEB1
File Name		: menu.js
Description		: Odel shop ajax bit
Created By		: Bjorn de Lima
Created Date	: 12-April-2007
Modified By		: 
Modified Date	: 
Version			: 1.00.000
*/


//new one works with mootools

var similarItemsViewed = 0;
var currentTab="browseTab";
var win;

function hideDetail(){
	obj2 = document.getElementById('hover');
	
	var inv = new Fx.Style(obj2,'opacity').set(1);
	var fx = new Fx.Styles(obj2,{duration:200,
						   transition: Fx.Transitions.linear,
						   onComplete: function(){
							  obj2.style.display = "none";
							  var inv2 = new Fx.Style(obj2,'opacity').set(1);
							  var inv2 = new Fx.Style(obj2,'padding-top').set(0);
							   }} ); 
	fx.start({
		 'opacity' : 0,
		 'padding-top' : [0,20]
		});

	}

function showDetail(obj,addto){
	var d = new Date();
	$('hover').style.display = 'none';
	pos = findPos(obj);	
	
	if (addto){
	url = "/shop/view_mini/"+obj.id+"/"+d.getTime();
	}
	else{
	url = "/shop/view_mini_noadd/"+obj.id+"/"+d.getTime();	
		}
	ajx = new Ajax(url, {
				
				method: 'get',
				
				onRequest: function(){
						//alert(pos[0]);
						$('hover').style.left=parseInt(pos[0]+50)+"px";
						if (pos[0]>700){$('hover').style.left=parseInt(pos[0]-100)+"px"; } // edge of the page
						$('hover').style.top=parseInt(pos[1]+50)+"px";
						$('hover').style.display = "block";
						$('hover').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:3px;border:1px solid #333333;'>Loading..</div>";
						} ,
				
				onComplete: function(){	
						$('hover').innerHTML = ajx.response.text;
					   }		
				   });
	ajx.request();
	
}

//show registry gift vouchers
function showVouchers(obj,addto){
	var d = new Date();
	$('hover').style.display = 'none';
	pos = findPos(obj);	
	
	url = "/shop/regisry_giftvoucher/"+obj.id+"/"+d.getTime();	
		
	ajx = new Ajax(url, {
				
				method: 'get',
				
				onRequest: function(){
						//alert(pos[0]);
						$('hover').style.left=parseInt(pos[0]+50)+"px";
						if (pos[0]>700){$('hover').style.left=parseInt(pos[0]-100)+"px"; } // edge of the page
						$('hover').style.top=parseInt(pos[1]+50)+"px";
						$('hover').style.display = "block";
						$('hover').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:3px;border:1px solid #333333;'>Loading..</div>";
						} ,
				
				onComplete: function(){	
						$('hover').innerHTML = ajx.response.text;
					   }		
				   });
	ajx.request();
	
}

function showWishlistDetail(obj){
		var d = new Date();
	$('hover').style.display = 'none';
	pos = findPos(obj);	
	if (pos[0]>760){$('hover').style.left=parseInt(pos[0]-100)+"px"; } // edge of the page

	url = "/wishlist/view_prod_mini/rowid/"+obj.id+"/"+d.getTime();

	ajx = new Ajax(url, {
				
				method: 'get',
				
				onRequest: function(){
						$('hover').style.left=parseInt(pos[0]+50)+"px";
						$('hover').style.top=parseInt(pos[1]+50)+"px";
						$('hover').style.display = "block";
						$('hover').innerHTML = "<div style='background-color:#ffffff;color:#ff2200;padding:3px;border:1px solid #333333;'>Loading..</div>";
						} ,
				
				onComplete: function(){	
						$('hover').innerHTML = ajx.response.text;
					   }		
				   });
	ajx.request();
	
	}
	
function rateWishlistItem(rowid,rate,obj){
	var d = new Date();
	
	url = "/wishlist/set_ilike/rowid/"+rowid+"/ilike/"+rate+"/"+d.getTime();

	ajx = new Ajax(url, {method: 'get',
				   onComplete: function(){	
						$('img'+rowid).src = "/images/star"+rate+".gif";
					   }	
				   });
	ajx.request();

	}


// gets and shows simmilar products in details view with ajax
function displaySimilarItems(prod,dept,offset){
	similarItemsViewed = parseInt(similarItemsViewed)+parseInt(offset);
	if (similarItemsViewed==0){ $('prevButton').style.display = 'none';}
	else { $('prevButton').style.display = 'inline'; }
	var d = new Date();
	url = "/shop/view_similar/prod/"+prod+"/dept/"+dept+"/limit/3/offset/"+parseInt(3*similarItemsViewed)+"/"+d.getTime();
	ajx = new Ajax(url, {
				
				method: 'get',
				
				onRequest: function(){
					$('similarItems').innerHTML = "<span  style='margin:10px;background-color:#bb1100; color:#ffffff;padding:3px;font-size:12px;'> Loading..</span>";
						} ,
				
				onComplete: function(){	
						app = new Fx.Styles('similarItems',{duration:300,transition: Fx.Transitions.linear});
						$('similarItems').innerHTML = ajx.response.text;
						app.start({
								  'padding-top' : [0,6]
								  });
						
						}				
				   });
	ajx.request();

	}
	
// sets value for hidden filed and submits form for single item removal
function removeItem(id){
	
	$('remove').value = id;
	document.removeForm.submit();
	
	}	
	
function viewCart(){
	var d = new Date();
	url = "/cart/mini_view/"+d.getTime();
	var ajx = new Ajax(url, {
					   
				   method : 'get',
					
					onRequest: function(){
					$('myCart').innerHTML = "<span  style='background-color:#bb2200; color:#ffffff;padding:3px;font-size:12px;'> Loading..</span>";
						} ,
						
					onComplete : function(){
						 $('myCart').innerHTML = ajx.response.text;					   
					   }
				   });
	ajx.request();
	
	}


function addToCart(){
	var d = new Date();
	qstring = $('addForm').toQueryString();

	url = "/cart/add/"+d.getTime();
	hideDetail();
	var ajx = new Ajax(url, {
				   method : 'post',
				   data : qstring,
				   onComplete : function(){
						viewCart();	
						
					   }
				   });
	ajx.request();
	}
	
function resetBorder(obj){
	for (i=0;i<obj.parentNode.childNodes.length;i++){
					obj.parentNode.childNodes[i].style.borderColor = "#999999";
					}
	
	}	

function showColorOptions(data){
	
	obj = $('coloroptions');
	for (i=0;i<data.length-1;i++){
	
		if ((i==0) || (data[i].color!=data[i-1].color)){
		el = new Element('a',{
				 'events': {
				'click': function(){			
				showSizeOptions(data,this.id);
				$('colorOption').value = this.id;
				resetBorder(this);
				this.style.borderColor = "#FF9900";
				return false;
				}
				}
				});
		el.className = "optionbox";
		el.id=data[i].color;

		el.setHTML("<img src='/images/pixel.gif' border=0px width=10px height=10px style='border:1px solid #666666;background-color:"+data[i].color+"'/>");
		obj.adopt(el); 
		//alert(data[i].size);
		}
	}

	}

/*function showSizeOptions(data,color){
	obj = $('sizeoptions');	
	document.getElementById('sizelabel').style.display = "block";
	obj.innerHTML ="";
	
	for (i=0;i<data.length-1;i++){
		if (data[i].color==color){
		el = new Element('a',{
				'events': {
				'click': function(){
				resetBorder(this);
				$('sizeOption').value = this.id;
				this.style.borderColor = "#FF9900";
				$('quantityselect').style.display = "block";
				$('addtocart').disabled = false;
				return false;
				}
				}
				});
		
		el.className = "optionbox";
		el.id=data[i].size;
		el.setHTML(data[i].size);
		obj.adopt(el); 
		//alert(data[i].color);
		}
	}
	}
*/

function showSizeOptions(data,color){
	function isNumber(str) {
	pattern= /^[0-9]+$/
		if (!str.match(pattern)) return false
			else return true;
	}
	
	temp = [];
	obj = $('sizeoptions');	
	document.getElementById('sizelabel').style.display = "block";
	obj.innerHTML ="";
	
	for (i=0;i<data.length;i++){
		if (data[i].color==color){
		temp.push(data[i].size);
		}
	}
	
	if (!isNumber(temp[0])) {
	newsizes = [];
	checkfor = ['XXS','XS','S','M','L','XL','XXL'];
	for(i=0;i<checkfor.length;i++){
	if (temp.contains(checkfor[i])){
		newsizes.push(checkfor[i]);
		temp.remove(checkfor[i]);
		}
	}
	newsizes.merge(temp);
	temp= newsizes;
	
	}
	
	for(i=0;i<temp.length;i++){
	el = new Element('a',{
				'events': {
				'click': function(){
				resetBorder(this);
	
				$('sizeOption').value = this.id;
				this.style.borderColor = "#FF9900";
				$('quantityselect').style.display = "block";
				$('addtocart').disabled = false;
				return false;
				}
				}
				});
		
		el.className = "optionbox";
		el.id=temp[i];
		el.setHTML(temp[i]);
		
		obj.adopt(el); 
		//alert(data[i].color);
		}
	}

	

function showTab(str){
	
	var arr =  new Array('finderTab', 'searchTab','cartTab');	
	for (i=0;i<arr.length;i++){
		$(arr[i]).style.display = 'none';
		//var inv = new Fx.Style(arr[i], 'opacity').set(0);
		}
	//var ap = new Fx.Style(str, 'opacity').start(0,1);
	$(str).style.display = 'block';
	Cookie.set("currentTab", str, {duration: false, path: '/'});
}
	
function showZoomable(obj){
	pos = findPos(obj);
	obj2 = document.getElementById('zoom');
	obj2.style.left=parseInt(pos[0])+"px";
	obj2.style.top=parseInt(pos[1]+5)+"px";
	obj2.style.display = "block";
	
}

// common popup window
function openPopup(url){
	win = window.open(url,'pop',"width=400,height=680");
	win.focus();

}	

function validateBasicSearch(){
	if ($('keyword').value.length<1){ 
	alert("Please enter a valid search term"); 
	$('keyword').focus();	
	$('keyword').style.backgroundColor = "#FFFFCC";
	
	return false; }
	else { 
	return true;}
	
	}

// Populates categories drop down upon change of the department
function validateAdvancedSearch(){

	if ($('keywordadv').value.length<1){ 
	alert("Please enter a valid search term"); 
	$('keywordadv').focus();	
	$('keywordadv').style.backgroundColor = "#FFFFCC";
	return false; }
	else { 
	return true;}
	
	}
	
	
// Populates categories drop down upon change of the department in gift finder tab
function populateCategories(obj){
	obj2 = $('categorySelect');
	obj2.length = 1;
	if (obj.value!=0){
	var arr = eval("array"+obj.value);
	var arrText = eval("arrayText"+obj.value);
	
	for (i=0;i<arr.length-1;i++){
	opt = document.createElement('option');
  	opt.text=arrText[i];
	opt.value = arr[i];
	try
    {
    obj2.add(opt,null); // standards compliant
    }
  catch(ex)
    {
    obj2.add(opt); // IE only
    }
	}
	}
	else {
	opt = document.createElement('option');
  	opt.text="< ---";
	opt.value = 0;
	try
    {
    obj2.add(opt,null); // standards compliant
    }
  catch(ex)
    {
    obj2.add(opt); // IE only
    }
		}
}	

function bridalHide(obj){
	if (obj.value=="Bridal"){
		$('other').style.display = "none";
		$('bridal').style.display = "block";
		}
	else{
	$('other').style.display = "block";
	$('bridal').style.display = "none";
	}
}


function showPointer(x,y){
	$('pointer').style.position = 'absolute';
	$('pointer').style.left = x-90+"px";
	$('pointer').style.top = y+"px";
	$('pointer').style.display = "block";
	}
	
function hidePointer(){
	
	}

// validates add to cart 
function validateCartAdd(obj){
	if ( !$('colorOption').value ){
		pos = findPos($('colorlabel'));
		showPointer(pos[0],pos[1]);
		return false;
		}
	if ( !$('sizeOption').value ){
		//alert('Please Select a size');
		pos = findPos($('sizelabel'));
		showPointer(pos[0],pos[1]);
		return false;
		}	
	return true;
	}

function populateCat(obj,selectedvalue){

obj2 = $(obj);
obj2.length=0;
for (i=0;i<arr.length;i++){
	if(arr[i]!=selectedvalue){
	opt = document.createElement('option');
  	opt.text=arrText[i];
	opt.value = arr[i];
	try
    {
    obj2.add(opt,null); // standards compliant
    }
  catch(ex)
    {
    obj2.add(opt); // IE only
    }
	}
	}	
}
function repop(obj){
	populateCat('sub',obj.value);
	}
	
function checkform(){
	
	strdept = document.selectAssoc.departm.value;
		if (strdept==""){
			alert('Please select a Department');
			document.selectAssoc.departm.focus();
		return false;
		}
		
	strcat = document.selectAssoc.cat.value;
		if (strcat=="0"){
			alert('Please select a category');
			document.selectAssoc.cat.focus();
		return false;
		}
		
			
	}