$(window).addEvent('domready', function(){initUserControlPanel();});

function initUserControlPanel(){
	if($('rememberPassword')) initFormFields('rememberPassword');
	initWishlistTable();
}

function loadUserContent(nameFile, nameDiv, callBack, ajaxLoading) {
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	else loadContent(nameFile, nameDiv, callBack, ajaxLoading);
}

function checkSessionInUserOrder(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/userControlPanel.cfm'; Shadowbox.close(); return false;}
	return true;
}

function checkSession()
{
	var xmlObj = createXMLRequest();
	
	xmlObj.open('GET', shop.url + '/ajaxTemplates/users/userControlPanel/checkSession.cfm?'+getUniqueId(), false);
	xmlObj.send(null);
	if(xmlObj.responseText.indexOf('0') != -1)return false;
	return true;
}

function showMessageDeleteAccount(formAction){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return false;}
	shop.cuteQuestion.addMessage('', languageSheet.lblDeleteAccount, languageSheet.lblYes, languageSheet.lblNo, 1, "yes", formAction, 0,0);
}

/*FUNCTIONS IN WISHLIST USER CONTROL PANEL*/
function initWishlistTable(){
	if($('listWishlistDetailsId')) $('listWishlistDetailsId').value = "";
	if($('listProductsIdToEmail')) $('listProductsIdToEmail').value = "";
	if($('listProductsId')) $('listProductsId').value = "";
	$$('.checkWishlistDetail').each(function(el){el.checked = false;});
	
}

function initWishlistToEmail(obj){
	$('informationBox').style.visibility = 'visible';
	initFormFields("wishlistToEmail");
}

function findInArray(array, stringToSearch){
	for (var i = 0; i < array.length; i++){
		if(array[i] == stringToSearch) return i;
	}
	return -1;
}

function generateListProductsId(productId){
	var content = $('listProductsId').value;

	if(content.length > 0)var arrContent = content.split(',');
	else var arrContent = new Array();
	
	var pos = findInArray(arrContent, productId);
	if(pos >= 0)arrContent.splice(pos, 1);
	else arrContent.push(productId);
	
	content = arrContent.join(",");
	$('listProductsId').value = content;
	$('listProductsIdToEmail').value = content;

}

function emptyListListProductsId(){
	$('listProductsId').value = "";
	$('listProductsIdToEmail').value = "";

}

function generateListWishlistDetailsId(wishlistDetailId){
	var content = $('listWishlistDetailsId').value;

	if(content.length > 0)var arrContent = content.split(',');
	else var arrContent = new Array();
	
	var pos = findInArray(arrContent, wishlistDetailId);
	if(pos >= 0)arrContent.splice(pos, 1);
	else arrContent.push(wishlistDetailId);
	
	content = arrContent.join(",");
	$('listWishlistDetailsId').value = content;
}

function emptyListWishlistDetailsId(){
	$('listWishlistDetailsId').value = "";
}


function addProducts(form){
	$('productList').value = $('listProductsId').value;
//	loadContent(shop.url + '/ajaxTemplates/addProducts.cfm?listProductId='+content, 'wishlistTable', 'reloadMiniBaskets()','');
	return Spry.Utils.submitForm(form, reloadMiniBaskets );
}


function deleteWishList(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	var content = $('listWishlistDetailsId').value;
	
	var arrContent = content.split(',');
	content = arrContent.join(',');
	if(content.length>0){
		loadContent(shop.url + '/ajaxTemplates/users/userControlPanel/wishlistDelete.cfm?listWishlistDetailsId='+content, 'wishlistTable', "initWishlistTable()",'');
	}
	else{
		shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblSelectProducts,0,0);
	}

}

function generateEmail(){
	if(checkSession() == false){window.location.href = shop.url + '/ecommerce/users/user.cfm'; return;}
	var content = $('listProductsIdToEmail').value;
	
	/*Shadowbox.open({
			player:     'ajax',
			content:    shop.url + '/ajaxTemplates/wishlistToEmail.cfm?listProductsId='+content,
			width:			470,
			height:			375
	}, {onFinish: function() {initWishlistToEmail();}}
	);	*/
	var arrContent = content.split(',');
	content = arrContent.join(',');
	if(content.length>0){
		loadContent(shop.url + '/ajaxTemplates/users/userControlPanel/wishlistToEmail.cfm?listProductsId='+content, 'informationBox', 'initWishlistToEmail();', '');
	}
	else{
			shop.cuteAlert.addMessage(languageSheet.lblError, languageSheet.lblSelectProducts,0,0);
	}
}

function selectAllProductsWishlist(){
	$$('.checkWishlistDetail').each(
																	function(el){
																					 var arrId = el.id.split("_");
																					 if(el.checked == true){
																						 	el.checked = false;
																						 	emptyListWishlistDetailsId();
																							emptyListListProductsId();
																					 }
																					 else{
																						 generateListWishlistDetailsId(arrId[0]); 
																						 generateListProductsId(arrId[1]); 
																						 el.checked = true;
																					 }
																					 changeClass($('divWishlistDetail'+arrId[0]), 'selectRow', 'selectedRow');
																	});
}

/*END FUNCTIONS IN WISHLIST USER CONTROL PANEL*/

