// JavaScript Document

var callback = ''; 	//if add_to_wishlist is clicked and if user not loged in, callback for addToWishlist() after successful login

function addToWishlist(){
	$.get(ROOT_PATH+'aj_request.php?aj=user.aj_addtowishlist','',wish_div_disp);

}
function wish_div_disp(response){
	var html="<div  id='addtowishlist_div_h'><table border='0' cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr><td align=\"left\" class=\"title_top_left\"></td>        <td class=\"title_top_middle\" align=\"left\">Add To Wishlist</td><td class=\"title_top_middle\"  align=\"right\"><a href=\"javascript:void(0)\" class=\"navlink2\" onclick=\"hideDisableDiv('addtowishlist_div'); $id('addtowishlist_div').innerHTML='';\">[x]</a></td><td align=\"left\" class=\"title_top_right\"></td></tr></table>  </div>";
	html+="<DIV  style='text-align: left; overflow:hidden; 'class='dg2'>";
	if(response == 'invalid'){
		callback = 'addToWishlist';
		show_error_login_div();
	}else{
		eval("var res = "+response);
		  
		html+= "<table width='100%'><tr><td width='80%'><span id ='wishlist_response' style=' white-space:nowrap;'></span></td><td width='20%' align='right'>&nbsp;</td><tr align='center' valign='middle'><td colspan='2'><div id='inner' style=' width:100%; overflow-x:hidden; overflow-y:auto;'><table width='100%' >";
		if(res.length>0){
			html+="<tr><th class='subheading' align='left'>Select Wishlist to add item to:</th></tr><tr><td><table>";
			for(i=0;i<res.length;i++){
				var g=res[i];
				html+="<tr><td width='10%'><input type='radio' name='wishlist_radio' value='"+ g['wishlistID'] +"' onChange=\"$id('newwish').style.display='none';\"></td><td width='90%' class='search' title='"+ g['Wishlistname'] +"'>"+ g['Wishlistname'] +"</td></tr>";
			}  
		}else{
			html+="<tr><td colspan='2'> <p><span class='search'>There Are No Wishlists In Your Account Create A New One</span></p></td></tr>";	
		}
		html+="<tr><td width='10%'><input type='radio' name='wishlist_radio' value='new' onChange=\"$id('newwish').style.display=''; $id('wishlist_txt').focus();\"></td><td width='90%' class='search' align='left'><span class='search' style='color:#5C9C00';><b>New Wishlist</b></span></td></tr></table></td></tr><tr><td align='center'><div id='newwish' style='display:none'><table width='100%' align='center' class='search'><tr><td >Enter Wishlist Name:</td><td align='left'><input type='text' class='field' id='wishlist_txt'></td></tr></table></div></td></tr></table></div></td></tr></table><DIV  style='text-align: left; overflow:hidden; padding-bottom:10px'><table width='100%' ><tr align='center' valign='bottom'><td align='right'><input type='image' src='"+ROOT_PATH+"images/go.jpg' width='26' height='19' border='0' onclick=\"addElementToWishlist();\"  /></td><td align='left'><img border='0' src='"+ROOT_PATH+"images/cancel.gif' width='46' height='20'  onclick=\"hideDisableDiv('addtowishlist_div'); $id('addtowishlist_div').innerHTML='';\" /></td></tr></table></DIV></div>";
	
	$('#addtowishlist_div').html(html);
	
	var height=res.length;
	var hite=165+((height-4)*20);
	if(hite>300)
		hite=300;
	//$id('addtowishlist_div').style.height = 
	$('#inner').css('height', hite + 'px');
	showDisableDiv('addtowishlist_div');
	}
}

function addElementToWishlist(){
	var element_Id = $('#element_page').val();
	var type = $('#element_type').val();
	var wishlistId=$('input[name=wishlist_radio]:checked').val();
	
	if(wishlistId=='new'){
		var wishlist_name = $('#wishlist_txt').val();
		if((!wishlist_name) || wishlist_name == '')	{
			alert('Enter wishlist name');
			$id('wishlist_txt').focus();
			return;
		}
		$.post(ROOT_PATH+'aj_request.php?aj=user.aj_addelementtowishlist', 'wishlist_name='+escape(wishlist_name)+'&elementId='+element_Id+'&type='+type, wish_div_result );
	}else{
		if((!wishlistId) || wishlistId==''){
			alert('Please Select a wishlist');
			return;
		}
		$.post(ROOT_PATH+'aj_request.php?aj=user.aj_addelementtowishlist','wishlistId='+wishlistId+'&elementId='+element_Id+'&type='+type,wish_div_result );
	}
}

function wish_div_result(res){
	$('#wishlist_response').html(res);
	try{
		var wishlistId=$id('response_wishlistId');	
		if($('#new_wishlist').val()=='1')
			logbook_expand_variable['wishlist']++;

		if(wishlistId){
			hideDisableDiv('addtowishlist_div');
			$('#addtowishlist_div').html('');
			logbook_expand_variable['wishlist_items']++;
			for(var i=0;i<wishlist_expand_variable.length; i++){
				if(wishlist_expand_variable[i]['wishlistID']==wishlistId.value ){
					wishlist_expand_variable[i]['num_items']++;
				}
			}
			logbook_update();
		}
	}catch(exc){}
}