dojo.declare("codex.Shop1Dialog",dijit.Dialog,{
 
    _setup: function(){
            this.inherited(arguments);
        
            var cancelButton = dojo.byId("cancel_order");
            dojo.connect(cancelButton, "onclick", this, this.cancelDialog);
         
            var startButton = dojo.byId("save_order");
            
            dojo.connect(startButton, "onclick", this, this.saveOrder);
        
    }, 
 
    show: function () {
        var nodes = dojo.query(".tx_ddcodexshop1matrix_products_input");

        var quantity = 0;
        for(var i = 0; i < nodes.length; i++){

            if (nodes[i].value.length > 0) {
                if (checkNr(nodes[i].value)) {
                    quantity = quantity + parseInt(nodes[i].value);
                } else {
                    quantity = 0;
                    break;
                }
            }
            
            // TODO: Check File Type
            
        }

        if (quantity > 0) {
            this.inherited(arguments);
        } else {
            this.cancelDialog();
        }
    },
 
    cancelDialog: function() {
        this.hide();
    },
    
    saveOrder: function() {

        var nodes = dojo.query(".tx_ddcodexshop1matrix_products_input");
        var user = dojo.byId("tx_ddcodexshop1matrix_pi1_dialog_user");
		var pageid = dojo.byId("tx_ddcodexshop1matrix_pi1_dialog_pageid").value;
		
        var html_wrapper = dojo.byId("tx_ddcodexshop1matrix_pi1_dialog_wrapper");
        html_wrapper.innerHTML = '<img src="fileadmin/css/loading.gif" />';

		
        var quantity = '';
        var article = '';
        var files = '';
		var variant ='';
		
        var upload = false;

        for(var i = 0; i < nodes.length; i++){

		var temp_q = dojo.byId("article_" + nodes[i].id.substr(8,nodes[i].id.length));
		var temp_f = dojo.byId("upload" + nodes[i].id.substr(7,nodes[i].id.length));
		var temp_v = dojo.byId("variant_" + nodes[i].id.substr(8,nodes[i].id.length));

				if (nodes[i].value.length > 0) {                
                    if (article.length > 0) {
                        quantity = quantity + ',' + nodes[i].value;
                        article = article + ',' + temp_q.value;
    
                       	if((typeof temp_f === "undefined") || (temp_f == null)){
                            files = files + ',';
                        } else {
                            files = files + ',' + temp_f.value;
                            upload = true;
                        }
					 
						if((typeof temp_v === "undefined") || (temp_v == null)){
                            variant = variant + ',';
                        } else {
                            variant = variant + ',' + temp_v.value;
                        }
    
                    } else {
                        article = temp_q.value;
                        quantity = nodes[i].value;
                       	if((typeof temp_f === "undefined") || (temp_f == null)){
                        } else {
                            files = temp_f.value;
                            upload = true;
                        }
						if((typeof temp_v === "undefined") || (temp_v == null)){
                        } else {
                            variant = temp_v.value;
                        }

                    }
                }
        }

//        dojo.io.iframe.send({
//            url: "index.php?eID=tx_ddcodexshop1matrix_pi1_upload_shoppingcart&id="+pageid,
//            method: "post",
//            handleAs: "html",
//            form: dojo.byId('tx_ddcodexshop1matrix_pi1_form'),
//            handle: function(data,ioArgs){
//                var url = 'index.php';
//                var pars = 'eID=tx_ddcodexshop1matrix_pi1_save_shoppingcart&id=' + pageid + '&quantity=' + quantity + '&article=' + article + '&variant=' + variant + '&file=' + files +  '&user=' + user.value; 
//                var myAjax = new Ajax (url, {method: 'get', data: pars, onComplete: function(answer) {
//
//                        var html_wrapper = dojo.byId("tx_ddcodexshop1matrix_pi1_dialog_wrapper");
//                        html_wrapper.innerHTML = answer;
//                }}).request();
//            }
//        });
        dojo.io.iframe.send({
            url: "index.php?eID=tx_ddcodexshop1matrix_pi1_upload_shoppingcart&id="+pageid,
            handle: function(data,ioArgs){
                var url = 'index.php';
                var pars = 'eID=tx_ddcodexshop1matrix_pi1_save_shoppingcart&id=' + pageid + '&quantity=' + quantity + '&article=' + article + '&variant=' + variant + '&file=' + files +  '&user=' + user.value; 
                var myAjax = new Ajax (url, {method: 'get', data: pars, onComplete: function(answer) {

                        var html_wrapper = dojo.byId("tx_ddcodexshop1matrix_pi1_dialog_wrapper");
                        html_wrapper.innerHTML = answer;
                }}).request();
            }
        });
    }

});

// STYLING FILE INPUTS 1.0 | Shaun Inman <http://www.shauninman.com/> | 2007-09-07
if (!window.SI) { var SI = {}; };
SI.Files =
{
	htmlClass : 'SI-FILES-STYLIZED',
	fileClass : 'file',
	wrapClass : 'cdxprodupload',
	
	fini : false,
	able : false,
	init : function()
	{
		this.fini = true;
		
		var ie = 0 //@cc_on + @_jscript_version
		if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; } // no support for opacity or the DOM
		this.able = true;
		
		var html = document.getElementsByTagName('html')[0];
		html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
	},
	
	stylize : function(elem)
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		elem.parentNode.file = elem;
		elem.parentNode.onmousemove = function(e)
		{
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
			{
				e.pageX = e.clientX + document.documentElement.scrollLeft;
				e.pageY = e.clientY + document.documentElement.scrollTop;
			};

			var ox = oy = 0;
			var elem = this;
			if (elem.offsetParent)
			{
				ox = elem.offsetLeft;
				oy = elem.offsetTop;
				while (elem = elem.offsetParent)
				{
					ox += elem.offsetLeft;
					oy += elem.offsetTop;
				};
			};

			var x = e.pageX - ox;
			var y = e.pageY - oy;
			var w = this.file.offsetWidth;
			var h = this.file.offsetHeight;

			this.file.style.top		= y - (h / 2)  + 'px';
			this.file.style.left	= x - (w - 30) + 'px';

		};
	},
	
	stylizeById : function(id)
	{
		this.stylize(document.getElementById(id));
	},
	
	stylizeAll : function()
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };
		
		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			var input = inputs[i];
			if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
			{
				this.stylize(input);
			};
		};
	}
};
