// JavaScript Document
var ajax = new sack();
var ajax2 = new sack();
var ajax3 = new sack();
var ajax4 = new sack();


function whenLoaded(){
	var e = $('loading_cos'); 
		e.innerHTML = '';
	$('loading_cos').style.display = "none";
}

function whenLoading(){
	var e = $('loading_cos'); 
	$('loading_cos').style.display = "block";
	e.innerHTML = '<div style="width:215px; position:relative; z-index:3"><div style="padding:3px; font-size:11px; font-weight:bold; color:#fff; background-color:#C30; position:relative; z-index:1000; margin:0px; margin-left:1px; text-align:center; top:-10px ">Se incarca...</div></div>';
}

function whenInteractive(){
	var e = $('divCos'); 
	e.innerHTML = "<p>getting data...</p>";
}

function whenCompleted(){
	showCos()
}

function whenCompleted2(){
	document.location.href = 'comanda.html';
}

function alertUser(){
	alert("Produsul ales a fost adaugat in cos!")
}



function showCos(){

	ajax3.requestFile = 		'/cos.html'
	ajax3.element = 			'divCos';

	ajax3.onLoading = 		whenLoading;
	ajax3.onLoaded = 		whenLoaded; 

	//ajax.onCompletion = 	whenCompleted;
	ajax3.runAJAX();
}

function addCos(id, tip, coment, culori, patern,f){
	
	
	unhighlightErrors(f, sErrorFields)
	sErrorTexts = '';
	sErrorFields = '';
	if(f.patern.value.length == 0) { addError('Te rugam sa alegi patern-ul dorit.', 'patern') }
	if(f.color.value.length == 0) { addError('Te rugam sa alegi culorile cristalelor dorite.', 'color') }

	if(sErrorTexts.length > 0) {
		highlightErrors(f, sErrorFields)
		alert('S-au detectat urmatoarele probleme:\n\n' + sErrorTexts)
		return false

	}
	else {
		ajax.setVar("id", id); 
		ajax.setVar("tip", tip); 	
		ajax.setVar("coment", coment); 	
		ajax.setVar("culori", culori); 	
		ajax.setVar("patern", patern); 	
	
		ajax.requestFile = 		'/cos.html'
		ajax.element = 			'divCos';
	
		ajax.onLoading = 		whenLoading;
		ajax.onLoaded = 		whenLoaded; 
		ajax.onCompletion = 	alertUser;
	
		ajax.runAJAX();

		return true
	}
	
}



function deleteCos(id){
	ajax2.setVar("id", id); 
	ajax2.setVar("cantitate", 0); 
	ajax2.setVar("delete", "ok");

	ajax2.requestFile = 		'/cos.html'
	ajax2.element = 			'divCos';

	ajax2.onLoading = 		whenLoading;
	ajax2.onLoaded = 		whenLoaded; 

	ajax2.onCompletion = 	whenCompleted;

	ajax2.runAJAX();
}
	
	
function deleteCos2(id){
	ajax2.setVar("id", id); 
	ajax2.setVar("cantitate", 0); 
	ajax2.setVar("delete", "ok");

	ajax2.requestFile = 		'/cos.html'
	ajax2.element = 			'divCos';

	ajax2.onLoading = 		whenLoading;
	ajax2.onLoaded = 		whenLoaded; 

	
	ajax2.onCompletion = 	whenCompleted2;
	
	ajax2.runAJAX();
}

function empty(){
	
	ajax4.setVar("delete", "all");

	ajax4.requestFile = 		'/cos.html'
	ajax4.element = 			'divCos';

	ajax4.onLoading = 		whenLoading;
	ajax4.onLoaded = 		whenLoaded; 

	ajax4.onCompletion = 	whenCompleted;
	ajax4.runAJAX();
}


