function mark_phone_for_destroy(element){
	$(element).next('.should_destroy').value = 1;
	$(element).up('.phone_item').hide();
}

function mark_phone_for_destroy_now(element){
	$(element).up('.phone_item').remove();
}

function mark_instantmessage_for_destroy(element){
	$(element).next('.should_destroy').value = 1;
	$(element).up('.instantmessage_item').hide();
}

function mark_instantmessage_for_destroy_now(element){
	$(element).up('.instantmessage_item').remove();
}

function mark_product_for_destroy(element){
	$(element).next('.should_destroy').value = 1;
	$(element).up('.product').hide();
}

function force_update_price(item){
	focused = "discount";
	update_price(item);
}

function force_update_color_price(item){
	focused = "colors";
	update_color_price(item);
}

function update_color_price(item){
	if(focused == "colors"){
		preco = $(item + "_extra_color_price").value;
		preco = parseFloat(preco);
		cores = $("quote_product_attributes_"+ item +"_colors").value;
		cores = parseInt(cores);
		qtd = $("quote_product_attributes_"+ item + "_quantity").value;
		qtd = parseInt(qtd);
		milheiros = Math.ceil(qtd/1000);
		if(cores > 1){
			cores = cores -1;
			cores = cores * milheiros;
			novo = cores * preco;
		}else{
			novo = 0;
		}
		novo = novo.toFixed(2);		
		$("quote_product_attributes_"+ item  +"_extra_color_total_price").value = novo;
		//$(item  +"_extra_color_total_price").value = novo;
		focused = "discount";
		update_price(item);			
	}
}

function update_price(item){
	if(focused == "discount"){
		preco = $("quote_product_attributes_"+ item + "_sugested_price").value;
		preco = parseFloat(preco);		
		qtd = $("quote_product_attributes_"+ item + "_quantity").value;
		qtd = parseInt(qtd);
		add_color = $("quote_product_attributes_"+ item  +"_extra_color_total_price").value;
		add_color = parseFloat(add_color);		
		novo = preco * qtd;
		novo = novo + add_color;
		novo = novo.toFixed(2);
		$("quote_product_attributes_"+ item  +"_cost").value = novo;
		$(item  +"_cost").value = novo;
		//focused = "discount";
		//update_full_price(item);
		focused = "colors";
		check_color_price(item);
	}
}

function check_color_price(item){
	if(focused == "colors"){
		preco = $(item + "_extra_color_price").value;
		preco = parseFloat(preco);
		cores = $("quote_product_attributes_"+ item +"_colors").value;
		cores = parseInt(cores);
		qtd = $("quote_product_attributes_"+ item + "_quantity").value;
		qtd = parseInt(qtd);
		milheiros = Math.ceil(qtd/1000);
		if(cores > 1){
			cores = cores -1;
			cores = cores * milheiros;
			novo = cores * preco;
		}else{
			novo = 0;
		}
		novo = novo.toFixed(2);		
		$("quote_product_attributes_"+ item  +"_extra_color_total_price").value = novo;
		//$(item  +"_extra_color_total_price").value = novo;
		focused = "discount";
		update_full_price(item);		
	}
}

function update_full_price(item){
	if(focused == "discount"){
		preco = $("quote_product_attributes_"+ item + "_cost").value;
		preco = parseFloat(preco);
		modif = $("quote_product_attributes_"+ item  +"_discount").value;
		modif = parseFloat(modif);
		if(modif > 0){
			novo = (modif/100) * preco;
			novo = preco + novo;
		}else if(modif < 0){
			novo = ((modif *-1)/100) * preco;
			novo = preco - novo;
		}else{
			novo = preco;
		} 
		novo = novo.toFixed(2);
		$("quote_product_attributes_"+ item  +"_total_cost").value = novo;
	}
}

function update_discount(item){
	if(focused == "cost"){
		preco = $("quote_product_attributes_"+ item + "_cost").value;
		preco = parseFloat(preco);
		modif = $("quote_product_attributes_"+ item  +"_total_cost").value;
		modif = parseFloat(modif);
		diff  = preco - modif;
		if(diff < 0){
			diff = diff * -1;
		}
		diff = (diff * 100) / preco;
		diff = diff.toFixed(2);
		if(modif < preco){
			diff = diff * -1;
		}
		$("quote_product_attributes_"+ item  +"_discount").value = diff;			
	}
}

function found_client(id,name,company,email,phone){
	$("quote_client_id").value = id;
	$("quote_name").value = name;
	$("quote_company").value = company;	
	$("quote_email").value = email;	
	$("quote_phone").value = phone;
	$("visible_email").value = email;
	$("client_search_results").replace('<div id="client_search_results" style="position:relative;top:-12px;">');
	$("choose-client").hide();
}

function find_product(item){
	$("choose-product").toggle();
	$("product_search_results").toggle();
}

function change_product(item){
	product_to_change = item;
}

function found_product(id,name,image){
	product_id = id;
	product_nm = name;
	product_img = image;
	insert_new_product();
}

function update_product(){
	$(product_to_change  +"_product_name").value = product_nm;
	$("quote_product_attributes_"+ product_to_change  +"_product_id").value = parseFloat(product_id);
	if(product_img != "NA"){
		$(product_to_change  +"_product_image").replace('<td><img src="'+ product_img +'" width="36" height="36" /></td>');
	}else{
		$(product_to_change  +"_product_image").replace('<td><div class="not-available">n/a</div></td>');		
	}
	$("choose-product").hide();
	$("product_search_results").replace('<div id="product_search_results" style="display:none;position:relative;top:-12px;">');
}

function set_modifier(){
	mod = $("global_modifier").value;
	mod = parseFloat(mod);
	lista.each(function(s) {
		$("quote_product_attributes_"+ s  +"_discount").value = mod;
		force_update_price(s);
	});
}

function set_new_color_price(){
	mod = $("new_color_price").value;
	mod = parseFloat(mod);
	lista.each(function(s) {		
		$("quote_product_attributes_"+ s  +"_extra_color_price").value = mod;
		$(s +"_extra_color_price").value = mod;
		item = s;
		preco = $(item + "_extra_color_price").value;
		preco = parseFloat(preco);
		cores = $("quote_product_attributes_"+ item +"_colors").value;
		cores = parseInt(cores);
		qtd = $("quote_product_attributes_"+ item + "_quantity").value;
		qtd = parseInt(qtd);
		milheiros = Math.ceil(qtd/1000);
		if(cores > 1){
			cores = cores -1;
			cores = cores * milheiros;
			novo = cores * preco;
		}else{
			novo = 0;
		}
		novo = novo.toFixed(2);		
		$("quote_product_attributes_"+ item  +"_extra_color_total_price").value = novo;
		focused = "discount";
		update_price(item);
	});	
}

function toggle_quote_comments(item){
	$(item + "_comments").toggle();
}

function remove_quote_product(item){
	$(item + "_comments").remove();
	$(item + "_product").remove();
}

function change_preview_color(){
	cor = $("category_color").value;
	cor = "#" + cor;
	$("preview_color").setStyle({backgroundColor:cor});	
}

/*
	VALIDAÇÃO DE FORMULÁRIOS
*/

function mailcheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
    	return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	return true					
}

function positive_number(value){
	if(parseInt(c) > 0){
		return true
	}else{		
		return false;	
	}
}

function positive_number_list(checkStr){
	values = checkStr.split(",");
	var values_len = values.length;
	var erro = 0;
	for(i=0; i<values_len; i++){
		value = values[i];
		if(parseInt(value) > 0){
			erro += 0;
		}else{
			erro += 1 
		}
	}
	if(erro>0){
		return false;
	}else{
		return true;
	}
}

function positive_number_list_new(checkStr){
	status = true;
	values = checkStr.split(",");
	var values_len = values.length;
	for(i=0; i<values_len; i++){
		value = values[i];		
		if(/^-?\d+$/.test(value)){
			//valor valido
		}else{
			return false	
		}
	}
	return (status);
}

function check_my_form(){ 
	q = $("quantities").value;
	c = $("colors").value;
	if(!positive_number_list(q)){
		alert("As quantidades devem ser números separados por vírgula.");
		return false;
	}
	if(!positive_number(c)){
		$("colors").value = 0;
		//alert("A quantidade de cores deve ser um número.");
		//return false;		
	}else{
		$("colors").value = parseInt(c);
	}	
}

function add_to_cart(id){
	$("product_id").value = id;
	$("pop-up").show();
	$("pop-up-background").show();
}

function close_add_to_cart(){
	$("pop-up").hide();
	$("pop-up-background").hide();
}

function close_confirmation(){
	$("pop-up-confirm").hide();
	$("pop-up-background").hide();
}



