var Soap1 = {
    createEnvelope: function(action, ns, parameters)
    {
        var soap = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Body>';
        soap += '<' + action + ' xmlns="' + ns + '">';
        soap += Soap.__parseParameters(parameters);
        soap += '</' + action + '></soap12:Body></soap12:Envelope>';
        return soap;
    },
    
    __parseParameters: function(parameters)
    {
        var params = "";
        if (typeof parameters == 'object')
        {
            // check if we were provided an array or an object
            if (typeof parameters.push == 'function')
            {
                for (var i = 0, length = parameters.length; i < length; i += 2)
                {
                    params += "<" + parameters[i] + ">" + parameters[i+1] + "</" + parameters[i] + ">";
                }
            }
            else
            {
                a$H(parameters).each(
                    function(pair)
                    {
                        params += "<" + pair.key + ">" + pair.value + "</" + pair.key + ">";
                    });
            }
        }

        return params;
    }
}

function getField1(fieldType,fieldTitle) {  
    var docTags = document.getElementsByTagName(fieldType);  
    for (var i=0; i < docTags.length; i++) {  
        if (docTags[i].title == fieldTitle) {  
            return docTags[i]  
        }  
    }  
}  


function posljiOceno(element,ocena,skupaj,ocen,guid)
{
	//select list
	var seznam = "{4FC62AEF-8BC9-44D9-910F-75C35FBFC12B}";
	if (guid != '' && guid != null) {
		seznam = '{' + guid + '}';
	}

	//recalculation 
	var novaOcena = skupaj + ocena;
	
	
	var novaOcenaStr = (novaOcena/(ocen+1)).toFixed(3)+'';
	novaOcenaStr = novaOcenaStr.replace('.', ',');

	// create xml that will create a new list item
    var batch = '<Batch OnError="Continue"><Method ID="2" Cmd="Update"><Field Name="ID">'+element+'</Field><Field Name="Ocena">'+novaOcena+'</Field><Field Name="Ocen">'+(ocen +1)+'</Field></Method></Batch>';

    // build parameter object
    var parameters = 
    {
        listName: seznam,
        updates: batch       // xml created earlier
    }
    // create soap envelope
    var soap = Soap1.createEnvelope(
        "UpdateListItems",
        "http://schemas.microsoft.com/sharepoint/soap/",
        parameters);
    // call web service
    new Ajax.Request(
        "http://www.dostop.si/_vti_bin/lists.asmx",
        {
            method: "post",
            contentType: "application/soap+xml",
            postBody: soap,
            onSuccess: function(transport) 
            	{ 
            	
            		disableRatings();
            		var y = getField1('div','stevilo_ocen');
            		if(y!=null){
            			y.innerHTML = novaOcenaStr + ' (' + (ocen+1) + ' ocen)'; 
            		}
	            	
	            	var z = getField1('div','ocena_active');
	            	if(z!=null){
	            		z.style.width =  (Math.floor(novaOcena/(ocen+1)*21))+'px';
	            		
	            	}
           	},
            onFailure: function(transport) { alert("Prislo je do napake. Prosim poskusi znova."); } // znotraj alerta še + transport.responseText
        });
}
