// for the window
//done window

// Bucur Marius Ovidiu - the script for the shopping list ....

var fatiContAcumPentruSms = '<a title="Cont nou" href="/comunitate/contnou">Fă-ţi un cont acum!</a>';
var adaugaIngredienteInListaTa = "Eşti în grabă? Nu ai un pix la îndemână? Trimite-ţi un SMS! Simplu.GRATUIT. " ;
adaugaIngredienteInListaTa += "Dă click pe ingredientele dorite şi adaugă-le la lista de cumpărături." ;
adaugaIngredienteInListaTa+= "<div id=\"sms_sending_button\"></div>";
var noIngredient = "<li><small>Nu ai niciun ingredient adăugat!<br/><a href=\"/retete\" title=\"Lista reţetelor\">Adaugă acum un ingredient</a></small></li>";
var vect = new Array();




Event.observe( window , 'load' , initShoppingList); // in checkrights

var i_a_u = 0;

function initShoppingList(s) {
	i_a_u = s;
	if($( 'shopping_list_ul' )){
		$( 'shopping_list_ul' ).innerHTML = createConditionSL(s) ;
		if(!s) $('sms_sending_button').innerHTML=fatiContAcumPentruSms;
	}
}









function createSemicolonList() {
	window_str = ""  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( i != 0 )
		window_str += ";" ;
		window_str += vect[ i ] ;
		if( cookieVal ) window_str += "-" + cookieVal ;

	}

	return window_str ;

}

function createSemicolonDetails() {
	window_str = ""  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		aux = readCookie( vect[ i ] ) ;
		if( !aux ) aux = "" ;
		window_str += ";" + aux + "" ;

	}

	return window_str ;

}




var ai = "<img src = \"/images/ajax-loader.gif\" />"     ;



function createS_L() {

	window_str = "<li>" + adaugaIngredienteInListaTa + "</li>"  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + " <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>    </li>" ;

	}
	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;
	return window_str ;

	//-----------------------------------------------------------------

}

function createConditionSL() {

	window_str = "<li>" + adaugaIngredienteInListaTa + "</li>"  ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + " <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>    </li>" ;

	}

	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;

	else {
		window_str += noIngredient + createFakeSmsButton();
	}

	return window_str ;

	//-----------------------------------------------------------------
}

function getIngredientsCart(){
	window_str = "";
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
		vect = myCookie.split( "," ) ;
	else vect = "" ;
	
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + " <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>    </li>" ;
	}
	if(vect.length)
		window_str += createSmsButton() ;
	else {
		window_str += noIngredient + createFakeSmsButton();
	}
	return window_str;
}

function createSmsButton() {
	str = "<li>" ;
	str += "<br clear = \"all\"/>";
	str += "<input class=\"formated_input\" id = \"send_sms_now\" value = \" Trimite-ţi un SMS\" type = \"button\" onclick = \"trySendSms();\" />" ;
	str += '<input onclick="window.location=\'/ajutor#sms\'" type="button" value="?"style=';
	str += '"color:white;font-weight:bold;margin-left:10px;background:#992512;border:3px #992512 solid"id="ineedhlp">';
	str += "<div id = \"cart_messages\" ></div></li>" ;
	return str ;
}

function createFakeSmsButton() {
	str = "<li>" ;
	str += "<br clear = \"all\"/>";
	str += "<input style=\"background:#b2e35a;border:2px solid #b2e35a\" class=\"formated_input\" id = \"send_sms_now\" value = \" Trimite-ţi un SMS\" type = \"button\" />" ;
	str += '<input onclick="window.location=\'/ajutor#sms\'" type="button" value="?"style=';
	str += '"color:white;font-weight:bold;margin-left:10px;background:#992512;border:3px #992512 solid"id="ineedhlp">';
	str += "<div id = \"cart_messages\" ></div></li>" ;
	return str ;
}

function check_ph( a ) {
	for(i=0;i<a.length;i++)
	if( parseInt(a.charAt(i))!=a.charAt(i)){
		$('nc_phone').value="format gresit";
		setTimeout( function(){ $('nc_phone').value=""; } , 1000 );
		return -1 ;
	}
	if(a.length != 10) {
		$('nc_phone').value="format gresit";
		setTimeout( function(){ $('nc_phone').value=""; } , 1000 );
		return -1 ;
	}
	else return 1 ;

}

function confirm_button_nc(tre){
	var a = 0 ;
	$('nc_confirm_result').innerHTML=ai;
	if( check_ph( $( 'nc_phone' ).value ) < 0 ) { $('nc_confirm_result').innerHTML=""; return false ; }
	else {
		new Ajax.Request('/user/confirm-mobile/format/json/', {
			asynchronous: false,
			method: 'post',
			parameters:{
				mobile : $('nc_phone').value,
				tre : tre
			},
			onSuccess: function(transport) {
				var myJson=transport.responseJSON;
				if( myJson.response ) {
					$('nc_confirm_result').innerHTML = transport.responseJSON.response ;
					//$('dialog'+(idx-1)+'_content').style.height="172px";
				}
				else {
					//$('dialog'+(idx-1)+'_content').style.height="172px";
					$('nc_confirm').innerHTML=nc_old_phone(myJson.u_phone);
				}
				if(tre) {
					if( myJson.u_phone ){
						general_phone=myJson.u_phone;
						$('nc_confirm').innerHTML=nc_old_phone(myJson.u_phone);
					}
					else {
						if( myJson.response )
						$('nc_confirm_result').innerHTML = transport.responseJSON.response ;
					}
				}
			}

		});

	}
}

function nc_new_phone(){
	var str = "<div id=\"nc_confirm\">Introduceti numarul de telefon:<br/>";
	str+="<input type=\"text\" id=\"nc_phone\" style=\"color:black;width:81px;\" /><br/><br/>";
	str+="<input type=\"button\" onclick=\"confirm_button_nc(1);\" id=\"confirm_button\" value=\"trimite\" />";
	str+="<div id=\"nc_confirm_result\"></div>";
	return str;
}

function confirm_code_nc() {
	$('nc_confirm_result').innerHTML=ai;
	new Ajax.Request('/user/confirm-code/format/json/', {
		asynchronous: false,
		method: 'post',
		parameters:{
			mobile : $( 'send_code' ).value
		},
		onSuccess: function(transport) {
			myJson=transport.responseJSON;
			if( myJson.corect ) {
				$('nc_confirm').innerHTML = myJson.response ;
				$('nc_confirm_result').innerHTML = '';
				$('cart_messages').innerHTML='<a href="/retete" title="Toate retetele">Fă click pe ingredientele dorite</a> şi adaugă-le în lista de cumpărături. Apasă <strong>Trimite SMS</strong> şi primeşti instant un SMS. GRATUIT!';
				return;
			}
			$('nc_confirm_result').innerHTML = myJson.response ;
			//$('dialog'+(idx-1)+'_content').style.height="172px";
		}
	});
}

function nc_choose_another() {
	$( 'nc_confirm' ).innerHTML = nc_new_phone();
}


function nc_old_phone(phone){
	var str = "<form style=\"padding:10px\"><div id=\"nc_confirm\"><h2>Confirmă-ţi numărul:</h2>";
	str+="Verifică numărul de telefon şi apasă butonul trimite. Introdu în căsuţă codul primit prin SMS.";
	str+="<input disabled type=\"text\" value=\""+phone+"\"id=\"nc_phone\"style=\"color:black;width:70px;\" />";
	str+="<br/><input type=\"button\" onclick=\"confirm_button_nc(0);return false;\" value=\" >> SMS de confirmare\"/>";
	str+="<br/><br/>Codul de confirmare: <input type=\"text\"id=\"send_code\"style=\"color:black;width:40px;\" />";
	str+="<br/><input type=\"button\" onclick=\"confirm_code_nc();\"id=\"confirm_button\"value=\"Confirmă\"/>"
	str+="<br/> ";
	str+="<a href=\"javascript:nc_choose_another();\">Schimbă numărul de telefon!</a>";
	str+="<div id=\"nc_confirm_result\" style=\"color:#FF0000;font-weight:bold;\"></div></form>";
	return str;
}



var general_phone = 0;


function nc_confirm(phone) {
	if( general_phone ) {
		createWindow(nc_old_phone(general_phone),250,250,"Confirmaţi-vă numărul");
		return;
	}
	if(phone) {
		createWindow(nc_old_phone(phone),250,250,"Confirmaţi-vă numărul");
	}
	else {
		createWindow(nc_bew_phone(),250,250,"Confirmaţi-vă numărul");
	}
}

function trySendSms() {
	glue = createSemicolonList()     ;
	alt  = createSemicolonDetails()  ;
	new Ajax.Request( '/user/send-sms/format/json' ,{
		method: 'post',
		parameters:{
			list : glue
		},
		onSuccess: function(transport){
			var res = transport.responseJSON;
			$( 'cart_messages' ).innerHTML = transport.responseJSON.response ;
			if( res.result == "-3" ) {
				if(res.confirm_tries){ // daca a mai trimis un mesaj de confirmare
					var phone=res.u_phone;
				}
				else {
					var phone=null;
				}
				var f = document.createElement('div');
				f.id="not_confirmed";var nc_s="";nc_s+="<a  href=\"javascript:nc_confirm('"+phone+"');\" >";
				nc_s+="Confirmati-va numarul de telefon acum!</a>";
				f.innerHTML=nc_s;
				$('cart_messages').appendChild(f);
			}
		}
	});

}



function eraseIngredIn( str ) {

	eraseCookie( str )   ;
	aux = str + "," ;
	b = readCookie( "ingredCart" )   ;
	modiied = b.replace( aux , "" ) ;
	if( b != modiied )
	b = modiied ;
	else
	b = b.replace( str , "" ) ;
	if( b[ b.length - 1 ] == "," ) b = b.substr( 0 , b.length - 1 ) ;
	createCookie( "ingredCart" , b ) ;
	createS() ;
}


//---------------------------------------------------------


function createS() {
	window_str = "";
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal ) cookieVal = "" ;
		window_str += "<li>" + vect[ i ] + "  <a title = \"sterge ingredient\" href = \"#\" onclick = \" eraseIngredIn( \'" + vect[ i ] + "\'  ) ; return false ;  \" > <img src = \"/images/del.gif\" border = \"0\"    > </a>   </li>" ;

	}
	if(vect.length&&i_a_u)
	window_str += createSmsButton() ;
	else {
		window_str += noIngredient + createFakeSmsButton();
		$('send_sms_now').onclick = "return false;";
	}

	$( 'shopping_list_ul' ).innerHTML = "<li>"+adaugaIngredienteInListaTa+"</li>" + window_str ;
	if($('admin_cart')) {
		$('admin_cart').innerHTML = window_str;
	}
	if(!i_a_u) {
		$('sms_sending_button').innerHTML=fatiContAcumPentruSms;
	}





}

//--------------------------------------------------------


function addToCart( ing , id_of_ing ) {

	justacookie = readCookie( 'ingredCart' );
	if( vect.indexOf( ing ) == -1 ) {
		if( !justacookie ) justacookie = ing ;
		else
		justacookie += "," + ing   ;

		str = "<ul><li>" ;
		str+= " <h4>Adaugă ingredientul <h3>"   +     ing     + "</h3> </h4> <p> " ;
		button = ' <input class = \"ingredient_content\" type = "text" onclick = " this.value = \'\' ; "  value = "Introduceţi cantitatea" id = "input' + ing + '" onkeyup = " createCookie( \'' + ing + '\' , this.value , 1 ) ; " >   <input type = "button" value = "ok" onclick = "	createCookie( \'ingredCart\' , \'' + justacookie + '\' , 1 );createS();Windows.close( \'dialog' + idx + '\' , event ) ;   " /> ' ;
		//button += '<input type = "button" value = "nu" onclick = "createS();Windows.close( \'dialog' + idx + '\' , event ) ;     "   /> </p>      </div>     '   ;
		str += button  ;
		str+="</li></ul>" ;
	}

	else str = "  <ul> <li> <h4>  Ingredientul " + ing   + " exista deja." + '  <input style = " margin-left : 3em ; " type = "button" value = "ok"onclick = "Windows.close( \'dialog' + idx + '\',event);createS();"</h4>'+'</li></ul>' ;



	str += "<div style = ' background : white ; ' > <ul> "     ;
	window_str = " <ul> " ;
	myCookie = readCookie ( 'ingredCart' )          ;
	if( myCookie )
	vect = myCookie.split( "," ) ;
	else vect = "" ;
	for( i = 0 ; i < vect.length ; i++ ) {
		if( vect[ i ] )
		cookieVal = readCookie( vect[ i ] ) ;
		if( !cookieVal )
		cookieVal = "Introduceţi cantitatea"         ;

		str+= "<li>" + vect[ i ] + "</li> <input class = \"ingredient_content\" type = \"text\" id = \"input" + vect[ i ] +   "\" " + " value = \"" + cookieVal + "\"  onkeyup = \" createCookie( '"+ vect[ i ] +"' , this.value , 1 ) ; \" " ;
		window_str += "<li>" + vect[ i ] + "</li>" ;

	}

	str+= " </ul>  " ;

	window_str += "</ul> " ;

	if( vect.indexOf( ing ) == -1 ) {
		if( !myCookie ) myCookie = ing ;
		else
		myCookie += "," + ing   ;



		//document.write( str ) ;

	}


	// ( 1 )  derizoriu
	//createLi = createS() ;
	//if( createLi )
	//$( 'shopping_list_ul' ).innerHTML = createS()   ;
	// ( 2 ) se poate sterge ( 1 ) - ( 2 )
	//str += createSmsButton() ;
	showWin ( pos_x , pos_y , str , 350 , 300 ) ;


}

function max10(t) {
	if(t.value.length>10) return false;
}

