
var api_url = "http://order.artido.de/manager/tools/webservices.php";
var api_key = "O0IvI.3etnq42O0IvI.3etnq42bj239";

function load_products() {
	var pulldown_only = jQuery("#pulldown_only").val();
	jQuery.getJSON(
		api_url+"?aws=?",
		{
			key: api_key,
			task: "get_products",
			pulldown_only: pulldown_only
			
		},
		function (data) {
			var options;
			jQuery.each(data, function(optionIdx, option) {
					if (option.selected == 1) {
						var sel= ' selected="selected"';
					} else { var sel='';}
					
					options += '<option value="'+option.code+'"'+sel+'>'+option.name+'</option>';
			});

			jQuery("#artido_products").html('<select style="width: 170px;" id="produkttyp" name="produkttyp" onchange="load_formats();">'+options+'</select>');
			
			if(typeof preselect_produkttyp == 'function') {
				preselect_produkttyp();
			} 
			
			load_formats();
		}
	);
}


function load_formats() {
	var product = jQuery("#produkttyp").val();
  var pulldown_only = jQuery("#pulldown_only").val();
  
  if (product.substring(0,4) == 'http') {
  	if (document.location.href != product) {
			document.location.href=product;
		}
	} else {
		jQuery("#artido_formats").html('<br /><div style="text-align:center;"><img src="http://www.artido.de/uploads/quickorder/ajax-loader.gif" width="43" height="11" alt="lädt..." /></div>');
	
		jQuery.getJSON(
			api_url+"?aws=?",
			{
				key: api_key,
				task: "get_formats",
				produkttyp: product,
				pulldown_only: pulldown_only
			},
			function (data) {
				var formats_html = '';
				var select_started = false;
				var lastoptgroup = '';
				jQuery.each(data, function(formatIdx, format) {
					if (format.special == 1) {
						// auflisten
						formats_html += '<input type="radio" name="form_id" value="' + format.form_id+ '" />&nbsp;<span class="input_start">' + format.form_bezeichnung + ' - ' + format.form_preis + '</span><br />';				 
					} else {
						// selectbox
						
						if (select_started==false) {
							// radiobutton ausgeben und select starten
							formats_html += '<input type="radio" name="form_id" value="selectbox" />&nbsp;';
							formats_html += '<select name="form_id2" id="form_id2" onclick="radioselect_select();" onchange="radioselect_select();">';
							formats_html += '<option> - weitere Formate - ';
							formats_html += '<optgroup label="' + format.form_gruppe_bezeichnung + '">';
							lastoptgroup = format.form_gruppe_bezeichnung;
							select_started=true;
						}
						
						if (lastoptgroup != format.form_gruppe_bezeichnung) {
							formats_html += '</optgroup><optgroup label="' + format.form_gruppe_bezeichnung + '">'
						}
						
						formats_html += '<option value="' + format.form_id+ '">' +  format.form_bezeichnung + ' - ' + format.form_preis + '</option>';
						 
						lastoptgroup = format.form_gruppe_bezeichnung;
	
					
					}
				});
				
				// ende der selectbox ausgeben
				formats_html += "</optgroup></select>";
					
				jQuery("#artido_formats").html(formats_html);
				// erste radiobox aktivieren
				jQuery("#QUICKORDER input[type='radio']:first").attr("checked","checked");
			}
		);
	}
}

function get_lieferzeit_by_produkttyp(produkttyp,ziel) {
                        jQuery.getJSON(
                                api_url+"?aws=?",
                                {
                                        key: api_key,
                                        task: "get_lieferzeit",
                                        produkttyp: produkttyp
                                },
                                function (data) {
                                        jQuery(ziel).html(data);
                                }
                        );
}

function radioselect_select() {
	jQuery("#QUICKORDER input[type='radio']:last").attr("checked","checked");
}


function startupload() {
		jQuery("#userfile, #upload_button").css('display','none');
		/*jQuery('#artido_products select, #artido_formats input, #artido_formats select').attr("disabled","disabled");*/
		UP.start();
		jQuery("#QUICKORDER").submit();
}

jQuery(document).ready(load_products);



var status_aktval = 0;
var status_zielval = 0;
var status_animator;

function artido_progress(val) {
	status_zielval = val;
	if (!status_animator) {
		status_animator = window.setInterval("artido_progress_animate()", 25);
	}
}

function artido_progress_animate() {
	if ( status_aktval < status_zielval) {
		status_aktval++;

		if (status_aktval > 100) {
			status_aktval = 100;
			var prozent = '...verarbeite...';
		} else {
			var prozent = status_aktval+"%";
		}
		
		jQuery('#artido_progress').progressbar('option', 'value', status_aktval);
		jQuery('#artido_status').html(prozent);

	} else {

		window.clearInterval(status_animator);
		status_animator = false;

	}
}


var UP = function() {
    
    /* private variables */
  
    var startTime = null;
    var p_val = 0;
  	var use_fake_status = true;
    
    var progress = function(val) {
    	if (val == 1) {
				jQuery('#artido_progress').progressbar();
			}
			
			if (val == null) {
				var akt = jQuery('#artido_progress').progressbar('option', 'value');
				var randadd = Math.random();
				randadd = Math.round(randadd);				 	
				val = akt + randadd;
			}
			
			if (p_val > val) {
				val++;	
			}
			p_val = val;
			
			artido_progress(val);
		}
    
    return {
        start: function() {
           startTime = new Date().getTime();
					 progress(1);
           this.requestInfo();
        },
        stop: function(files) {
           if (typeof files == 'undefined' || files) {
                var secs = (new Date().getTime() - startTime)/1000;
                progress(100);
           }
					 startTime = null;
        },
        requestInfo: function() {
        				document.getElementById("ifr").src="/uploads/quickorder/uploadinfo.php?ID="+jQuery('#UPLOAD_IDENTIFIER').val()+"&"+new Date().getTime();
                if (use_fake_status) {progress();}
								window.setTimeout("UP.requestInfo()",1000);      
        },
        updateInfo: function(uploaded, total, estimatedSeconds) {
            if (startTime) {
                if (uploaded) {
                    var progresspercent = Math.floor(uploaded/total*100);
                		use_fake_status = false;
										progress(progresspercent);
                }
            }
        }
        
        
    }

}()

