//loads a JSON response from the server for specified image and request type. When the response arrives callback function
//passed as an argument is executed. The format of the JSON response depends on the request type.
//
//inReq - string, PS request type, for example 'req=ctx'
//inImg - string, image name with optional modifiers after '?'
//inCallback - callback function, will be executed when the successful response arrives from the server. JSON object
//			will be passed to the callback function as a sole argument.
//inErrCallback - callback function, will be executed when the error response arrives from the server. JSON object
//			will be passed to the callback function as a sole argument. this JSON object will contains a single field 
//			'message' carrying server error.
function sjGetResponse(inReq, inCallback, inErrCallback) {
	var id = sjHashCode(inReq);
	inReq += '&id=' + id;
	if (typeof inCallback != 'undefined'){
		sjCallbacks[id] = inCallback;
	}
	if (typeof inErrCallback != 'undefined'){
		sjErrCallbacks[id] = inErrCallback;
	}
    var oScript = document.getElementById('sjScript_'+id);
    if (oScript) {
		document.getElementById('scriptHolder').removeChild(oScript);
    }
    oScript = document.createElement('script');
    oScript.id = 'sjScript_'+id;
    oScript.src = inReq;
    document.getElementById('scriptHolder').appendChild(oScript);
}


//private functions and variables

var sjCallbacks=new Object();
var sjErrCallbacks=new Object();

function s7jsonResponse(inArg, inId) {
	sjCallbacks[inId](inArg);
}

function s7jsonError(inArg, inId) {
	if (typeof sjErrCallbacks[inId] != 'undefined'){
		sjErrCallbacks[inId](inArg);
	}else{
		alert(inArg.message);
	}
}

function sjHashCode(d) {//unix style
	if (!d || d=="") return 1;
	var h=0,g=0;
	for (var i=d.length-1;i>=0;i--) {
		var c=parseInt(d.charCodeAt(i));
		h=((h << 6) & 0xfffffff) + c + (c << 14);
		if ((g=h & 0xfe00000)!=0) h=(h ^ (g >> 21));
	}
	return h;
}


