var ajaxm = {};

/**
 * Sends an AJAX request to the server to retrieve a list of videos or the video
 * player/metadata. Sends the request to the specified filePath on the same
 * host, passing the specified params, and filling the specified resultDivName
 * with the resutls upon success.
 * 
 * @param {String}
 *            filePath The path to which the request should be sent
 * @param {String}
 *            params The URL encoded POST params
 * @param {String}
 *            resultDivName The name of the DIV used to hold the results
 */
ajaxm.sendRequest = function(filePath, params, resultDivName, flag) {
	if (window.XMLHttpRequest) {
		var xmlhr = new XMLHttpRequest();
	} else {
		var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}

	xmlhr.open('POST', filePath, true);
	xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

	xmlhr.onreadystatechange = function() {

		var resultDiv = document.getElementById(resultDivName);
		if (xmlhr.readyState == 1) {
			resultDiv.innerHTML = '<img src="images/ajax-loader.gif">';
		} else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
			if (xmlhr.responseText) {
				resultDiv.innerHTML = xmlhr.responseText;
			}
		} else if (xmlhr.readyState == 4) {
			alert('Invalid response received - Status: ' + xmlhr.status);
		}
	}
	xmlhr.send(params);
}


ajaxm.sendRequestS = function(filePath, params, resultDivName, flag) {
	if (window.XMLHttpRequest) {
		var xmlhr = new XMLHttpRequest();
	} else {
		var xmlhr = new ActiveXObject('MSXML2.XMLHTTP.3.0');
	}

	xmlhr.open('POST', filePath, true);
	xmlhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

	xmlhr.onreadystatechange = function() {

		var resultDiv = document.getElementById(resultDivName);
		if (xmlhr.readyState == 1) {
			resultDiv.innerHTML = '<img src="images/ajax-loader.gif">';
		} else if (xmlhr.readyState == 4 && xmlhr.status == 200) {
			if (xmlhr.responseText) {
				location.href= '/Koszyk';
			}
		} else if (xmlhr.readyState == 4) {
			alert('Invalid response received - Status: ' + xmlhr.status);
		}
	}
	xmlhr.send(params);
}

ajaxm.ReminderMe = function()
{
    var timeperiod = document.getElementById("remindlenght").value; 
    var timemail = document.getElementById("remindmail");

    var remindform = document.getElementById("remindform");
    var remindmsg = document.getElementById("remindmsg");

   // alert(remindform.style.height + ' ' + remindform.height + ' ' +
	// remindform.offsetHeight + ' ' + remindform);




    if(timemail.value == '')
    {
       return alert("Należy podac adres email");
    }
    else
    {
        var params = 'queryType=remidme&period=' + timeperiod + '&mail=' + timemail.value;
        var filePath = 'ajax/remider.php';
        ajaxm.sendRequest(filePath, params, 'remindmsg', false);
        timemail.value = '';
        return alert("Dodano przypomnienie");
    }
}


ajaxm.DeliveryNotice = function(zlecenie,telefon,osoba,firma)
{
    var params = 'queryType=remider&zlecenie=' + zlecenie + '&telefon=' + telefon + '&osoba=' + osoba + '&firma=' + firma;

    var filePath = 'ajax/remider.php';
    ajaxm.sendRequest(filePath, params, 'sendmsg', false);

    alert("Wiadomość została wysłana");
    closeMessage();
    document.getElementById("send_" + zlecenie).style.display = 'none';
}

ajaxm.WyslijFundacje = function() {
    var body = document.getElementById("aamsg");
    if(body.value == '') {
        return alert("Musisz podać treść zgłoszenia przed wysłaniem");
    }
    var params = 'queryType=send&mail=' + body.value;
    var filePath = 'ajax/modal.ajax.php';
    ajaxm.sendRequest(filePath, params, 'mmsg', true);
    setTimeout("document.getElementById(\"mmsg\").innerHTML = \"&nbsp;\";",3000);
    setTimeout("document.getElementById(\"aamsg\").value = \"\";",3000);
}


ajaxm.KoszykZapisz = function(val,pos,id,tradecart) {

    var params = 'queryType=save&val=' + val + '&id=' + id + '&tradecart=' + tradecart;
    var filePath = 'ajax/tradecart.php';
    ajaxm.sendRequestS(filePath, params, pos , true);

}

ajaxm.KoszykZapisz2 = function(val,pos,id,tradecart) {

    var params = 'queryType=save&val=' + val + '&id=' + id + '&tradecart=' + tradecart;
    var filePath = 'ajax/tradecart.php';
    ajaxm.sendRequest(filePath, params, pos , true);

}

function checkEnter(event,val,pos,id,tradecart)
{
    if (event.keyCode == 13) {
        var nowy = document.getElementById("ilosc_" + id + "_" + tradecart);
        var star = document.getElementById("hid_ilosc_" + id + "_" + tradecart);

        if(nowy.value != star.value)
        {
        	star.value = nowy.value;
        	
        	var total = '0';
            var layers = document.getElementsByTagName("input");
            
            for (var i = 0; i < layers.length; i++) {
        		if(layers[i].id.match("hid_cena_") && layers[i].type == 'hidden') {
        			total = parseInt(total) + (parseInt(layers[i].value) * parseInt(document.getElementById("hid_ilosc_" + layers[i].id.substring(9)).value));
        		}

            }
            alert(total);
        	document.getElementById("suma_" + id + "_" + tradecart).innerHTML = (document.getElementById("hid_cena_" + id + "_" + tradecart).value * nowy.value).toFixed(2) + ' zł';
        	document.getElementById("totalprice").value = total.toFixed(2);
        	document.getElementById("totalprice_dis").innerHTML = total.toFixed(2) + ' zł';

            ajaxm.KoszykZapisz(val,pos,id,tradecart);  
        }
    } 
}

function offmouse(pos,id,tradecart) {
    var nowy = document.getElementById("ilosc_" + id + "_" + tradecart);
    var star = document.getElementById("hid_ilosc_" + id + "_" + tradecart);

    if(nowy.value != star.value)
    {
    	star.value = nowy.value;
    	
    	var total = '0';
        var layers = document.getElementsByTagName("input");
        
        for (var i = 0; i < layers.length; i++) {
    		if(layers[i].id.match("hid_cena_") && layers[i].type == 'hidden') {
    			total = parseInt(total) + (parseInt(layers[i].value) * parseInt(document.getElementById("hid_ilosc_" + layers[i].id.substring(9)).value));
    		}

        }
        
    	document.getElementById("suma_" + id + "_" + tradecart).innerHTML = (document.getElementById("hid_cena_" + id + "_" + tradecart).value * nowy.value).toFixed(2) + ' zł';
    	
    	document.getElementById("totalprice").value = total.toFixed(2);
    	document.getElementById("totalprice_dis").innerHTML = total.toFixed(2) + ' zł';

        ajaxm.KoszykZapisz2(nowy.value,pos,id,tradecart);
    }
}


function unload(){

    var layers = document.getElementsByTagName("input");
    var newc = new Array();
    var oldc = new Array();
    var newn = new Array();
    var liczn = 0;
    var liczo = 0;

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

	if(layers[i].id.match("ilosc_") && layers[i].type == 'text')
	{
            newn[liczn] = layers[i].id;
            newc[liczn] = layers[i].value;
            liczn++;
	}

	if(layers[i].id.match("hid_ilosc_") && layers[i].type == 'hidden')
	{
            oldc[liczo] = layers[i].value;
            liczo++;
	}
    }

    for(var t = 0; t < newc.length;t++)
    {
        if(newc[t] != oldc[t])
        {
            var sp = newn[t].split("_");
            ajaxm.KoszykZapisz(newc[t],'loader_' + sp[1] + '_' + sp[2],sp[1],sp[2]);
            alert("Zmieniono liczb� telefon�w, zapisywanie nowych stan�w");
        }
        else
        {
          // alert("ilosci sa takie same" + newc[t] + ' != ' + oldc[t]);
        }
    }
}

