
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 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 UP = function() {
    
    /* private variables */
  
    var startTime = null;
  
    
    var progress = function(val) {
    	if (val == 1) {
				jQuery('#artido_progress').progressbar();
			}
			if (val == null) {
				var akt = jQuery('#artido_progress').progressbar('option', 'value');
				val = akt + 1;
			}
			
			jQuery('#artido_progress').progressbar('option', 'value', val);
			jQuery('#artido_status').html(val+"%");
		}
    
    return {
        start: function() {
           startTime = new Date();
					 progress(1);
           this.requestInfo();
        },
        stop: function(files) {
           if (typeof files == 'undefined' || files) {
                var secs = (new Date() - startTime)/1000;
                progress(100);
           }
					 startTime = null;
        },
        requestInfo: function() {
                document.getElementById("ifr").src="http://www.artido.de/uploads/quickorder/uploadinfo.php?ID="+jQuery('#UPLOAD_IDENTIFIER').val()+"&"+new Date();
                window.setTimeout("UP.requestInfo()",1000);      
        },
        updateInfo: function(uploaded, total, estimatedSeconds) {
            if (startTime) {
                if (uploaded) {
                    var progresspercent = Math.floor(uploaded/total*100);
                		progress(progresspercent);
                } else {
                    progress();
                }
            }
        }
        
        
    }

}()
