/*--------------------------------------------------------*/
/*-----USED FOR EVALUATION--------------------------------*/
/*--------------------------------------------------------*/

var eval_place;
var eval_id;
var site_id;
var user_id;
var fathers_id;
var ev_ph;
var sp_id;
var next_cat;
var lang;

var eval_state = 1; // 1 - incarcam rezultatele; 2 - trimitem rezultatele
var go_next_eval = true;

function recalculate_fields(p_sp_id,p_next_cat,p_site_id)
{

	
	try
	{
	for (var j=1;j<=6;j++)
	{
	    
	    //alert(p_sp_id+""+p_next_cat+'sl'+j);
	    var t=document.getElementById(p_sp_id+""+p_next_cat+'sl'+j);
	    var r="t"+p_site_id+""+j;
	    var w=document.getElementById(r).lang;
	    calculate_score(w,t,r,1,p_site_id);
   	}
   	}
   	catch(err)
   	{
   	}

}

/*--------------------------------------------------------*/
/*set data returned from a url to a div element*/
function handleHttpResponse_eval(){
    if(http_eval.readyState == 4){ 
	if (eval_state == 1){
            document.getElementById(eval_place).innerHTML = http_eval.responseText;
    	    recalculate_fields(sp_id,next_cat,site_id);	
	} else if (eval_state == 2){
    	    document.getElementById(eval_place).innerHTML = "DONE";
	}
	go_next_eval = true;
    }
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData_eval(url,place,parameters,spid,ncat,siteid){
    go_next_eval = false;
    eval_state = 1;

    eval_place = place;
    sp_id = spid;
    next_cat = ncat;
    site_id = siteid;

    http_eval.open("POST", url, true); 
    http_eval.onreadystatechange = handleHttpResponse_eval;
    http_eval.send(parameters);
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData_POST_eval(url,place,parameters){
    go_next_eval = false;
    eval_state = 2;

    http_eval.open("POST", url, true);
    http_eval.onreadystatechange = handleHttpResponse_eval;
    http_eval.send(parameters);
}

/*--------------------------------------------------------*/
function send_form_eval(id,place,sid,uid,fid,evph,spid,ncat,lg) {
    if (go_next_eval){
	go_next_eval = false;
	eval_state = 2;

        eval_id = id;
	eval_place = place;
        site_id = sid;
	user_id = uid;
        fathers_id = fid;
	ev_ph = evph;
        sp_id = spid;
	next_cat = ncat;
        lang = lg;

	var post_string = "action=vote&redirect=1";
        for (var i=0;i<total_array.length;i++){
	    post_string += "&points="+encodeURI(total_array[i]);
        }

//alert(post_string);

	document.getElementById(place).innerHTML = "<p><img src='/img/load.gif' style='border:none;'>&nbsp;Sending the results...";
    
        getData_POST_eval("/cgi/vote.cgi",place,post_string);
    }
}


/*--------------------------------------------------------*/
/*-----USED FOR COMMENT-----------------------------------*/
/*--------------------------------------------------------*/

var comment_place;
var comment_id;

var comment_state = 1; // 1 - incarcam rezultatele; 2 - trimitem rezultatele
var go_next_comment = true;

/*--------------------------------------------------------*/
/*set data returned from a url to a div element*/
function handleHttpResponse_comment(){
    if(http_comment.readyState == 4){
	if (comment_state == 1){
    	    document.getElementById(comment_place).innerHTML = http_comment.responseText;
	} else if (comment_state == 2){
    	    document.getElementById(comment_place).innerHTML = "DONE";
	}
	go_next_comment = true;
    }
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData_comment(url,place,parameters){
    go_next_comment = false;
    comment_state = 1;

    comment_place = place;
    
    http_comment.open("POST", url, true); 
    http_comment.onreadystatechange = handleHttpResponse_comment;
    http_comment.send(parameters);
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData_POST_comment(url,place,parameters){
    go_next_comment = false;
    comment_state = 2;

    comment_place = place;

    http_comment.open("POST", url, true); 
    http_comment.onreadystatechange = handleHttpResponse_comment;
    http_comment.send(parameters);
}

/*--------------------------------------------------------*/
function send_form_comment(id,place,form) {
    if (go_next_comment){
	go_next_comment = false;
	comment_state = 2;
    
	var post_string = "action=obj_add&type=Comment&FatherID="+id+"&AddDate="+encodeURI("0000-00-00 00:00:00");
	post_string += "&ModDate="+encodeURI("0000-00-00 00:00:00")+"&Type=Comment&NbObjs=0&IsTrashed=0&Owner=";
	post_string+=document.getElementById("owner"+id).value+"&Comment=";
	post_string+=encodeURI(document.getElementById("textarea"+id).value);

	comment_id = id;
    
	document.getElementById(place).innerHTML = "<p><img src='/img/load.gif' style='border:none;'>&nbsp;Sending the comment...";
    
	getData_POST_comment("/cgi/add.cgi",place,post_string);
    }
}

/*--------------------------------------------------------*/
/*-----USED FOR POLL--------------------------------------*/
/*--------------------------------------------------------*/

var div_place;

/*--------------------------------------------------------*/
/*set data returned from a url to a div element*/
function handleHttpResponse(){
    if(http.readyState == 4){ 
        document.getElementById(div_place).innerHTML = http.responseText; 
    }
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData(url,place){
    div_place = place;
    http.open("GET", url, true); 
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}

/*--------------------------------------------------------*/
/*get content for a specific url*/
function getData_POST(url,place,parameters){
    div_place = place;
    http.open("POST", url, true); 
    http.onreadystatechange = handleHttpResponse;
    http.send(parameters);
}

/*--------------------------------------------------------*/
/*transforms*/
function send_form(id,place,form) {

    var post_string = '';

    for (var i = 0; i < form.length; i++ ){
	var e = form.elements[i]; //parcurgem fiecare element
	if ( e.type == 'hidden' ) { post_string += e.name+'='+encodeURI(e.value)+'&';}
	if ( ( ( e.type == 'radio' ) || ( e.type == 'checkbox' ) ) && ( e.checked  ) ){
	    post_string += e.name+'='+encodeURI(e.value)+'&';	
	}
	if ( ( e.type == 'select-one' ) || ( e.type == 'select-multiple' ) ){
	    for (var j = 0; j < e.options.length; j++){
		var o = e.options[j];
		if ( (o.selected) && (o.value != '')) { post_string += e.name+'='+encodeURI(o.value)+'&'; }
	    }
	}
	if ( ( e.type == 'text' ) || ( e.type == 'textarea' ) ) {
	    if ( e.value != '' ) { post_string += e.name+'='+encodeURI(e.value)+'&'; } 
	}
    }

    document.getElementById(place).innerHTML = "<table border=0 class='Frame' id='Poll'><tr><th><a><table class='Loading'><tr><td><img src='/img/load.gif'></td><td valign=center>Loading...</td></tr></table></a></th></tr><tr><td id='bottom'><a>&nbsp;</a></td></tr></table>";

    getData_POST("/cgi/poll.cgi",place,post_string);
}


/*--------------------------------------------------------*/
/*----NEW AJAX OBJECT----------------------------------------------------*/
/*--------------------------------------------------------*/
/*instantiate a XMLHttpRequest object in order to implement AJAX concept*/
function getHTTPObject(){

    var http_request = false;
    if(window.XMLHttpRequest){ // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
    }else if(window.ActiveXObject){ // IE
	try{ http_request = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
	    try{ http_request = new ActiveXObject("Microsoft.XMLHTTP");
	    }catch(e){}
	}
    } else if(window.createRequest){ //other browsers
	try { http_request = window.createRequest();
	} catch (e) {}
    }

    return http_request;
}

var http = getHTTPObject(); // We create the HTTP Object for poll

var http_comment = getHTTPObject(); // We create the HTTP Object for comment

var http_eval = getHTTPObject(); // We create the HTTP Object for evaluation

