/* Functions to organize a watchlist 
**
**
** sendProductToBasket()	: An article is put in the cart
** removeProductFromWatchlist()	: An article is removed from watchlist
** sendProductToWatchlist()	:	An article is put in watchlist from the shopping cart page
** readDataFromForm()		:	Helper function to read input from checkboxes in the form
*/
var addToWatchlist=function(productsId){var url="/watchlist.php?xmlHttpRequest=add&products_id="+productsId;var url4Check="/watchlist.php?xmlHttpRequest=is_in_watchlist&products_id=";var doAddToWatchlist=false;new Ajax.Request(url4Check+productsId,{method:'get',asynchronous:false,onSuccess:function(transport){var response=eval('('+transport.responseText+')');if(response.result){if(confirm(response.confirm)){doAddToWatchlist=true;}else{doAddToWatchlist=false;}}else{doAddToWatchlist=true;}}});if(doAddToWatchlist){new Ajax.Request(url,{method:'get',onSuccess:function(transport){if(transport.responseText.indexOf('Success')>-1){if(confirm("Der Artikel wurde Ihrer Einkaufsliste hinzugefügt. Wollen Sie in die Einkaufsliste schauen?")){window.location.href="/watchlist.php";}}else if(transport.responseText.indexOf('Not logged')>-1){var redirectUrl=transport.responseText.substring(transport.responseText.indexOf('http'));alert("Sie sind nicht angemeldet, bitte melden Sie sich erst an. ");window.location.href=redirectUrl;}},onFailure:function(transport){alert("Ein Fehler bei der Verbindung mit dem Server ist aufgetreten. Probieren Sie es noch einmal, bitte.");}});}}
var readDataFromForm=function(formname){var form=$(formname);var url4Check="/watchlist.php?xmlHttpRequest=is_in_basket&products_id=";var checkboxes=form.getInputs('checkbox');var params="";checkboxes.each(function(checkb){if($(checkb).getValue()!=null){var row=$(checkb).name.substring(12);var quantity=form['cart_quantity_'+row];new Ajax.Request(url4Check+checkb.value,{method:'get',asynchronous:false,onSuccess:function(transport){var response=eval('('+transport.responseText+')');if(response.result){if(confirm(response.confirm)){params+=$(checkb).getValue()+"-"+$(quantity).getValue()+"-";}else{$(checkb).checked=false;$(checkb).setValue('');}}else{params+=$(checkb).getValue()+"-"+$(quantity).getValue()+"-";}}});}});params=params.substring(0,(params.length-1));return params;}
var unsetSelectedCheckbox=function(formname){var form=$(formname);var checkboxes=form.getInputs('checkbox');checkboxes.each(function(checkb){$(checkb).checked=false});}
var sendProductToBasket=function(){var params=readDataFromForm('watchlist');if(params.length>0){var url=$("action4SendToBasket").getValue()+"&products="+params;new Ajax.Request(url,{method:'get',onSuccess:function(transport){self.alert(transport.responseText);if(transport.responseText.indexOf('Success')>-1){if(confirm(transport.responseText.substring(transport.responseText.indexOf(':')+1))){unsetSelectedCheckbox('watchlist');}else{window.location.href="/shopping_cart.php";}}else if(transport.responseText.indexOf('Not logged')>-1){var redirectUrl=transport.responseText.substring(transport.responseText.indexOf('http'));alert("Sie sind nicht angemeldet, bitte melden Sie sich erst an. ");window.location.href=redirectUrl;}else{alert("Rückgabewert :"+transport.responseText);}},onFailure:function(transport){alert("Ein Fehler bei der Verbindung mit dem Server ist aufgetreten. Probieren Sie es noch einmal, bitte.");}});}else{window.location.href="/shopping_cart.php";}}
var removeArticles=function(){var form=$('watchlist');var checkboxes=form.getInputs('checkbox');var params="";checkboxes.each(function(checkb){if($(checkb).getValue()!=null){var row=$(checkb).name.substring(12);var quantity=form['cart_quantity_'+row];params+=$(checkb).getValue()+"-"+$(quantity).getValue()+"-";}});params=params.substring(0,(params.length-1));if(params.length>0){var url=$("action4RemoveFromWatchlist").getValue()+"&products="+params;window.location.href=url;}else{alertBox();}}
var sendProductToWatchlist=function(form,newAction,url){var doAddToWatchlist='';var choice,checkbx;var url4Check="/watchlist.php?xmlHttpRequest=is_in_watchlist&products_id=";var form=$(form);try{checkbx=form.getInputs('checkbox','cart_to_watchlist[]');var quantity=form.getInputs('text','cart_quantity[]');for(var i=0;i<checkbx.length;i++){if(checkbx[i].checked==true){new Ajax.Request(url4Check+checkbx[i].value,{method:'get',asynchronous:false,onSuccess:function(transport){var response=eval('('+transport.responseText+')');if(response.result){if(confirm(response.confirm)){doAddToWatchlist+=checkbx[i].value+':'+quantity[i].value+',';}else{$(checkbx[i]).checked=false;$(checkbx[i]).setValue('');}}else{doAddToWatchlist+=checkbx[i].value+':'+quantity[i].value+',';}}});}};if(doAddToWatchlist.length>0){if(confirm("Wollen Sie weitere Artikel aus dem Warenkorb auswählen?")){choice='cart';}else{choice='watchlist';}
var hidden=form.getInputs('hidden','wheretogo');hidden[0].value=choice;hidden=form.getInputs('hidden','products_ids');hidden[0].value=doAddToWatchlist.substr(0,(doAddToWatchlist.length-1));form.action=newAction;form.submit();}else{window.location.href=url;}}catch(e){window.location.href=url;}}
var alertBox=function(){alert("Bitte Artikel auswählen.");}
