function viewAuthWindow(){
	var main_auth = document.getElementById("main_authorize");
	if(main_auth){
		if(main_auth.style.display == "block"){
			main_auth.style.display = "none"
		}else{
			var auth_fields = main_auth.getElementsByTagName("INPUT");
			if(auth_fields.length > 0){
				for(i=0; i<auth_fields.length; i++){
					if((auth_fields[i].type == "text" || auth_fields[i].type == "password") && (auth_fields[i].name == "login" || auth_fields[i].name == "password")){
						auth_fields[i].value = "";
					}
				}
			}
			var inf_text = document.getElementById("info_text");
			if(inf_text){
				infoText('Please input login and password', 1);
			}
			main_auth.style.display = "block";
		}			
	}
}
function checkAuthWindow(){
	var main_auth = document.getElementById("main_authorize");
	var req_string = "";
	var correct_text = 0;
	if(main_auth){
		var auth_fields = main_auth.getElementsByTagName("INPUT");
		if(auth_fields.length > 0){
			for(i=0; i<auth_fields.length; i++){
				if((auth_fields[i].type == "text" || auth_fields[i].type == "password") && (auth_fields[i].name == "login" || auth_fields[i].name == "password")){
					if(trimString(auth_fields[i].value).length > 0){
						correct_text += 1;
						req_string += auth_fields[i].name + "=" + auth_fields[i].value;
						if(correct_text == 1){ req_string += "&"; }
					}
				}
			}
			if(correct_text == 2){
				infoText("Check data", 1);			
				sinh_ajax(req_string);
			}else{
				infoText("Invalid data", 2);
			}
		}
	}	
}
function infoText(data, color){
	var inf_text = document.getElementById("info_text");
	if(inf_text && !isNaN(parseInt(color)) && data){
		if(color == 1){inf_text.style.color = "black";}
		if(color == 2){inf_text.style.color = "red";}
		if(color == 3){inf_text.style.color = "green";}				
		inf_text.innerHTML = data;
	}					
}
var fck = "";
function checkReturnData(get_data){
 if(get_data &&(get_data == "login_error" || get_data == "login_ok")){
  if(get_data == "login_error"){infoText("Invalid login", 2);}
  if(get_data == "login_ok"){actionOnLoginCorrect();}  
 }else{
 	if(get_data){
		var main_txt = document.getElementById("main_text");
		if(main_txt){
			var parent_main_txt = main_txt.parentNode;
	   		if(parent_main_txt){
	   			var old_id = main_txt.id;
				delete FCKeditorAPI.Instances[fck.InstanceName];	   			
	   			parent_main_txt.removeChild(main_txt);
	   			main_txt = document.createElement("DIV");
	   			main_txt.id = old_id;
				main_txt.innerHTML = get_data;
				parent_main_txt.appendChild(main_txt);	   			
				var a_button = document.getElementById("action_button");
				if(a_button){
					a_button.value = "Edit";
					a_button.onclick = function(){
		  		  		viewAuthWindow();
					}     
				}
		  	}
		}
  	}
 }
}
function actionOnLoginCorrect(){
	infoText("Login correct", 3);
	viewAuthWindow();
	loadEditor('main_text');
}
function trimString(sInString){
	return sInString.replace(/(^\s+)|(\s+$)/g, '');
}
function getXmlHttp(){
  var xmlhttp;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
}
function sinh_ajax(req){
      var xmlhttp = getXmlHttp(); 
      var req_str = 'req.php';
         xmlhttp.open('POST', req_str, false); 
         xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
         xmlhttp.send(req); 
	     if(xmlhttp.status == 200) { 
              return_content = xmlhttp.responseText; 
              if (return_content.length>0) {
              		checkReturnData(return_content);
              } 
         }	
}
function loadEditor(div){
	var main_txt = document.getElementById("main_text");
	var a_button = document.getElementById("action_button");
	if(main_txt && a_button){
		var div = document.getElementById(div);
		 fck = new FCKeditor("myFCKeditor");
		fck.Value = main_txt.innerHTML;
		div.innerHTML = fck.CreateHtml();
		a_button.value = "Save";
		a_button.onclick = function(){
       		var editor = FCKeditorAPI.GetInstance("myFCKeditor");
        	var value = editor.GetHTML(); 
			if(value){			
				sinh_ajax("editor_html="+value);
			}
		}
	}
}