/*
    * 0 (uninitialized)
    * 1 (loading)
    * 2 (loaded)
    * 3 (interactive)
    * 4 (complete)

*/
/*
  _i GETS the object associated with some id, and returns it
*/
function _i(id){var _q = d.getElementById(id);return _q;}function _ip(id){var _q = pd.getElementById(id);return _q;}

//
var chngdte = 0;

function GetXmlHttpObject(){
	var objXMLHttp=null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	    alert('Your browser does not support Ajax. Please upgrade');
	    return false;
	}
	return objXMLHttp;
};

/* ajax_add_site.php */
 function AjaxPostForm(parameters,cstype) {
   http_request = false;
      if(cstype<=20){
	      var url = 'ajax.php';
      }else{
	      // ???        var url = 'ajax.php';
      }
	  //alert(parameters);
	  http_request = GetXmlHttpObject();
      http_request.open('POST', url, true);

      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
	  /*
	  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
	 */
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");

	    http_request.onreadystatechange  = function() { alertContents(cstype,http_request); };

	  http_request.send(parameters);
	  /*http_request.onreadystatechange = alertContents(cstype);*/
}

function alertContents(cstype){
	result = http_request.responseText;
	//alert (http_request.readyState);
    if (http_request.readyState == 4) {
		  if(cstype==1){
			 //alert('Data retrieved successfully');
			}
			else if(cstype==2){
				//alert('cstype2');
			}
			else if(cstype==3){
				//alert('cstype3');
			}
			else if(cstype==4){
			}
			else if(cstype==5){
			}
			else if(cstype==21){
				//alert('aaa');
			}
			else if(cstype==25){
				//alert('trugna');
			}

      result = http_request.responseText;
			if(cstype==1){
			  answer = result.split("|!|");
			  var comment = 'comment_'+answer[1]+'';
			  var button = 'butt_'+answer[1]+'';
			  d.getElementById(comment).innerHTML = answer[0];
			  d.getElementById(button).innerHTML = answer[2];
			  //d.getElementById('subm_6').style.display = 'block' ;
			}else if(cstype== 2){
			  answer = result.split("|!|");
			  if(answer[0]==1){
			    var comment = 'comment_'+answer[2]+'';
  			  var button  = 'butt_'+answer[2]+'';
  			  var subm    = 'subm_'+answer[2]+'';
  			  d.getElementById(comment).innerHTML = answer[1];
  			  d.getElementById(button).innerHTML = answer[3];
  			  d.getElementById(subm).innerHTML = answer[4];
			  }
			  else{
			    alert(answer[1]);
			    var bjname =  _i('mobjid').innerHTML;
  			  LoadPopUpData(result,'Incoming SMS Message data',cstype);
			  }
			}else if(cstype== 3){
			  //alert(result);
			  answer = result.split("|!|");

			  var id = answer[2];
			  var info_id = "d_"+answer[2];
			  var chk = "s_"+answer[2];
			  if (answer[0]==1) {
  			  _i(info_id).innerHTML = answer[1];
  			  _i(chk).innerHTML = 1;
			  //  _i(id).style.display='block';
  			  window.setTimeout('UnTip()', 3000);
  		      TagToTip(id, BORDERWIDTH, 0, BGCOLOR, '');
		   /* xpos = _i('MouseX').value;
  		    ypos = _i('MouseY').value;
  		    xpos = parseInt(xpos) + 20;
  		    ypos = parseInt(ypos) + 20;
  		    //alert(_i(id).currentStyle.height);
  		    //alert(_i(id).offsetHeight);
  		    if(ypos>500){
  		    	ypos = parseInt(ypos) - 40 - parseInt(_i(id).offsetHeight);
  		    }
    		  _i(id).style.position='absolute';
    		  _i(id).style.top=ypos;
    		  _i(id).style.left=xpos;
			  */
			  }
			  else{
			  }

			}else if(cstype== 4){
               /* pagination */
			   answer = result.split("|!!!|");
			   var comm = "";
			   var cid, uid, display_image, comment_date, comment_body , screen_name;
			   for (var i=10; i < answer.length; i++)
			   { 
				   answer[i] = answer[i].split("|!|");
			   }
			   var previous_first_comment, next_first_comment, aid, number_of_comments, pagination_text,  all_comments, current_uid, previous, next,lang;
			   previous_first_comment   = answer[0];
			   next_first_comment       = answer[1];
			   aid                      = answer[2];
			   number_of_comments       = answer[3];
			   pagination_text          = answer[4];
			   previous                 = answer[5];
			   next                     = answer[6];
			   all_comments             = answer[7];
			   lang                     = answer[8];
			   current_uid              = answer[9];
               for (i=10; i < answer.length; i++)
			   { 
				cid           = answer[i][1];  
				display_image = answer[i][0];  
				uid           = answer[i][2]; 
				comment_date  = answer[i][3];
                comment_body  = answer[i][4];
                screen_name   = answer[i][5];
				

                comm = comm + '<TABLE width="560" border="0" style="margin:-3px;padding:0px;"><TR><TD width="100%" style="padding:0px;"><div id="title">&nbsp;<div id="inntext" style="display: block;">';
                comm = comm + '<div class="clearfix"><div id="image" style="float:left; width:75px">';  
			    comm = comm + '<img src="' + display_image + '" width="70">';
			    comm = comm + '</div><div id="nickname" style="float:left; color: #027AC6; padding:10 2 10 2px; font-size:18px;">' + screen_name + '</div>';
				comm = comm + '<div id="time_' + cid + '" style="float:left; font-weight: bold; padding:15 2 15 2px; font-size: 11px;"> - ' + comment_date + '</div>';   
			    if (answer[i][7] == "edit") 
			    {  
				comm += '<div id="butt_'+ cid + '" style="float:right; font-weight:bold; padding:14px; font-size:14px; padding:11 2 11 2px;"><input type="submit" class="buttons" value=" Edit " style="float:left; margin:0px;" onclick="javascript:d.form_'+ cid + '.submit();"></div>';
                comm += '<div id="subm_'+ cid + '" style="display:none;"><FORM METHOD="POST" name="form_'+ cid + '" ACTION="javascript:getSiteForm(d.form_'+ cid + ',\'1\');">';
                comm += '<input type="hidden" name="comment" value="'+ comment_body + '"><input type="hidden" name="user" value="'+ uid + '"><input type="hidden" name="cid" value="'+ cid + '">';
                comm += '<input type="hidden" name="action" value="1">';
                comm += '</form>';
                comm += '</div>';
			    }
				comm = comm + '</div><br><div id="comment_' + cid + '" style=" padding:6px; font-size:11px; width:500px;">' + comment_body + '</div></div><div id="bottom">&nbsp;</div></div></TD></TR></TABLE>';
			   }
			   document.getElementById('comments').innerHTML = comm;

			   if (parseInt(all_comments) > parseInt(number_of_comments)){
			      var pagination = "";
			      // if there is a previous page load button
			      if (answer[0] != -1) 
			      {
                   pagination += "<a href='#nogo'  onclick=Pagination(" + current_uid + "," + aid + "," + previous_first_comment  + "," + number_of_comments + ",'" + lang + "')>" + previous + "</a>";
			      }
			      pagination += pagination_text;
			      // if there is a next page load button
			      if (next_first_comment  != -1)
			      {
				    pagination += "<a href='#nogo' onclick=Pagination(" + current_uid + "," + aid + "," + next_first_comment  + "," + number_of_comments + ",'" + lang + "')>" + next + "</a>";	 
			      }
                  document.getElementById("top_pagination").innerHTML    = pagination;
                  document.getElementById("bottom_pagination").innerHTML = pagination; 
			   }
			   /* end of pagination */
			/* edit folders*/
			} else if (cstype == 5) {
                 answer = result.split("|!|");
                 var folder_id, folder, slashed_folder;
				 folder_id       = answer[0];
                 folder         = answer[1];
				 document.getElementById("text_" + folder_id).innerHTML = folder;
                 document.getElementById("text_" + folder_id).style.display = 'block';
                 document.getElementById("input_" + folder_id).value = folder;
                 document.getElementById("input_" + folder_id).style.display = 'none';
                 document.getElementById("action_" + folder_id).style.display = 'block';
                 document.getElementById("save_button_" + folder_id).style.display = 'none';      
			   
			} else if (cstype == 6){
			   /* pagination */
			   answer = result.split("|!!!|");
          
			   var inbox = "";               
			   var sid,j,sender,aid,title,onmouse,publishers,time_formatted,comments,thead;  		
			   var previous_first_content, next_first_content, aid, number_of_contents, pagination_text,  all_contents, current_uid, previous, next,lang;
			   previous_first_content   = answer[0];
			   next_first_content       = answer[1];
			   archived                 = answer[2];
               search_string_to_add     = escape(answer[3]);
 			   number_of_contents       = answer[4];
			   pagination_text          = answer[5];
			   previous                 = answer[6];
			   next                     = answer[7];
			   all_contents             = answer[8];
			   lang                     = answer[9];
			   current_uid              = answer[10];
			   thead                    = answer[11];
			   for (var i=12; i < answer.length; i++) { 
				 answer[i] = answer[i].split("|!|");
			   }
			   var j=0;
			   for (var i=12; i < answer.length; i++) { 
				 sid            = answer[i][0];  
	           	 sender         = answer[i][1];  
				 aid            = answer[i][2]; 
				 content_files  = answer[i][3];
                 title          = answer[i][4];
                 onmouse        = answer[i][5];
                 publishers     = answer[i][6];
                 time_formatted = answer[i][7];
                 comments       = answer[i][8];
				 
				 inbox += "<tr bgcolor='#F5F5F5' id='row_" + sid + "'>";
	             inbox += "<td width='25'><input type='checkbox' name='selected_prs" + j + "' value='" + sid + "' style='border:none;background:none;' onclick=highlight(this,'row_" + sid + "') /></td>";
	             inbox += "<td valign='top' width='100'>" + sender;
                 inbox += "</td><td valign='top'><a href='vc.php?aid=" + aid + "&sid=" + sid + "' style='text-decoration:none; font-size:11px;'>";
			     inbox += title + "</a></td><td valign='top' align='center'>" + content_files;
                 inbox += "</td><td valign='top' " + onmouse + " style='cursor:help'>" + publishers + "</td>";
                 inbox += "<td valign='top' align='center'><span >" + comments + "</span></td>";
                 inbox += "<td valign='top' align='center' width='80'>" + time_formatted + "</td></tr>";
                 j++;
			   }  
	            inbox = thead + "<tbody>" + inbox + "</tbody></table>";
				document.getElementById('contents').innerHTML = inbox;
			     if (parseInt(all_contents) > parseInt(number_of_contents)){
			        var pagination = "";
			        // if there is a previous page load button
					if (answer[0] != -1) 
			        {
                       pagination += "<a href='#nogo'  onclick=InboxPagination('" + current_uid + "','" + archived + "','" + search_string_to_add + "','" + previous_first_content + "','" + number_of_contents + "','" + lang + "')>" + previous + "</a>";
			        }
			        pagination += pagination_text;
			        // if there is a next page load button
					if (next_first_content  != -1)
			        { 
				       pagination += "<a href='#nogo' onclick='InboxPagination(\"" + current_uid + "\",\"" + archived + "\",\"" + search_string_to_add + "\",\"" + next_first_content + "\",\"" + number_of_contents + "\",\"" + lang + "\")'>" + next + "</a>";	 
			        
					}
                    document.getElementById("bottom_pagination").innerHTML = pagination; 
			     }
			}else if (cstype==21) {
			  d.getElementById('info').innerHTML = result;
			  d.getElementById('info').style.display = 'block';
				if (result.length>60) {
				  d.get_info.submit();
				}
			}else if (cstype==22) {
				d.getElementById('st1').className  = 'previous';
				d.getElementById('st2').className  = 'this';
				d.getElementById('first').innerHTML = result;
				  d.get_code.submit();
			}else if (cstype==23) {
			  d.getElementById('codes').style.display = 'block';
				d.getElementById('codes').innerHTML = result;
			}else if (cstype==24) {
				d.getElementById('st2').className  = 'previous';
				d.getElementById('st3').className  = 'this';
				d.getElementById('first').innerHTML = result;
			}
   }
}

 // priema obekta na formata za submit, i server scripta deto shte se vika formobj== document.formname cstype==id na action
  function getSiteForm(formobj,cstype) {
    //alert(cstype);
    //alert(formobj);

     // get incoming sms message info
    if(cstype==1){

    }
    if(cstype==21){

    }
    // vzimame dannite ot formata vuv vid gotov za izprashtane
    var parameters = GetFormElem(formobj);

    // vikame AJAX komponennta
    AjaxPostForm(parameters,cstype);
 }

/*
     GetFormElem(fmnm)  - obhojda elementite na formata i gi vruishta kato gotov URL encoded string ot tipa x=7&y=4 etc..
  // fmnm - tova e obekta na formata  podava se s document.formname pri vikaneto na funckiata vuv formata: document.formname
*/
function GetFormElem(fmnm){
var parameters = '';
var i = "";
for(i=0;i<fmnm.elements.length;i++){
		var tp       = fmnm.elements[i].type;
		if(tp=='radio'){
	      var nm     =  fmnm.elements[i].name;
		  var vl     =  fmnm.elements[i].value;
		  parameters += nm+"=" + encodeURIComponent(vl)+'&' ;
		 }else if(tp=='checkbox'){
			 if(fmnm.elements[i].checked==true){
				 var nm     = fmnm.elements[i].name;
				 var vl     = fmnm.elements[i].value;
				 parameters += nm+"=" + encodeURIComponent(vl)+'&' ;
			 }
		}else{
		  var nm     = fmnm.elements[i].name;
		  var vl     = fmnm.elements[i].value;
		  parameters += nm+"=" + encodeURIComponent(vl)+'&' ;
		}
}
parameters = parameters.substring(0, parameters.length-1);
   //alert('parametri:'+parameters);
  // _i('testdv').innerHTML=parameters;
  if(chngdte!=0){
	  parameters = parameters+'&changedate='+encodeURIComponent(chngdte);
	  //alert(parameters);
  }
   return parameters;
}

function Pagination(uid,aid,first_comment_to_display,number_of_comments,lang) {
 var parameters = "lang=" + lang + "&uid=" + uid + "&aid=" + aid + "&first_comment_to_display=" + first_comment_to_display + "&number_of_comments=" + number_of_comments + "&action=4";
 AjaxPostForm(parameters,4);
}

function InboxPagination(uid,archived,search_string_to_add,first_content_to_display,number_of_contents,lang) {
 var parameters = "lang=" + lang + "&uid=" + uid + "&archived=" + archived + "&search_string_to_add=" + search_string_to_add + "&first_content_to_display=" + first_content_to_display + "&number_of_contents=" + number_of_contents + "&action=6";
 AjaxPostForm(parameters,6);
}

