var popupExec 		= 0;
var popupId   		= 0;
var popupPrm  		= new Array();
var popupMenuExec 	= 0;
var popupMenuId 	= 0;
var posrev			= 0;
var xnewspopupId 	= 0;


function createRequestObject(){
	var xmlHttp;
	try{
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function ajaxSaveTag(tagId){
	var xmlHttp = createRequestObject();
	var tagName = document.getElementById('editBox'+tagId).value;
	if (!xmlHttp) {alert('You cannot use this feature without AJAX support');return 0;}
	xmlHttp.onreadystatechange = function(){
		var rec;
		if (typeof(xmlHttp.responseText)=="unknown")
			rec = "";
		else
			rec = xmlHttp.responseText;
		if (rec.substr(0,7)=='expired'){
			alert('Your session has expired. You need to log in again');
			return 0;
		}else if (rec=='-1'){
			return 0;
		}
		else if (rec.substr(0,3)=='id:'){
			document.getElementById('tagLabel'+tagId).innerHTML = tagName;
			hideEdit(tagId);
			alert(lngDSaved);
			return 1;
		}
		else return 1;
	}
	var content		= "tagName="+encodeURI(tagName);
	xmlHttp.open("POST","ajax/saveTag.php?id="+encodeURI(tagId));
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", content.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(content);
}

function ajaxSaveDraft(article,headline,category,tags)
{
 var xmlHttp = createRequestObject();
 if (!xmlHttp) {alert('You cannot use this feature without AJAX support');return 0;}
 xmlHttp.onreadystatechange = function()
 {

	var rec;
	if (typeof(xmlHttp.responseText)=="unknown")
	{
		rec = "";
	}
	else
	{
		rec = xmlHttp.responseText;
	}
	//----------
	document.getElementById('divDraftButton').style.display='';
	document.getElementById('divSaveDraft').style.display='none';
   if (rec.substr(0,7)=='expired')
   {
    alert('Your session has expired. You need to log in again');
    return 0;
   }else if (rec=='-1')
   {
     return 0;
   }
   else if (rec.substr(0,3)=='id:')
   {
    if (rec.substr(3)!='0'){
		draftId = rec.substr(3);
		alert(lngDraftSaved);
	}
	return 1;
   }
   else return 1;
 }
 var content = "headline="+encodeURI(headline);
 content =  content+"&category="+encodeURI(category);
 content =  content+"&tags="+encodeURI(tags);
 content =  content+"&content="+encodeURI(article);



 //alert(content);
 document.getElementById('divSaveDraft').style.display='';
 document.getElementById('divDraftButton').style.display='none';
 xmlHttp.open("POST","ajax/saveDraft.php?id="+encodeURI(draftId));
 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 xmlHttp.setRequestHeader("Content-length", content.length);
 xmlHttp.setRequestHeader("Connection", "close");
 xmlHttp.send(content);
}

function validateURL(url){
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
	if (!v.test(url)) return 0; else return 1;
}

String.prototype.htmlEntities = function () {
   return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
};

function changeEditor(editor)
{
 var date = new Date();
 date.setTime(date.getTime()+(30*24*60*60*1000));
 var expires = date.toGMTString();
 document.cookie =
  'news_editor='+editor+'; expires='+expires+'; UTC; path=/';
 document.location.reload();
}

function ShowHint(id,hint)
{
 document.getElementById('hint_'+id).innerText=hint;
 document.getElementById('hint_'+id).textContent=hint;
}

function ShowHintBox(id)
{
 document.getElementById(id).innerText 		= 	hint;
 document.getElementById(id).textContent	=	hint;
 document.getElementById(id).style			= "display: table";
}

function HideHintBox(id)
{
	
}

function ShowHint2(hint)
{
 document.addnewsfrm.hintbox2.value=hint;
}
function fShowHint(hint)
{
 document.getElementById("hintbox").innerHTML=hint;
}

function CheckBlankFields1()
{
 if (document.setupform.displayname.value=='' || document.setupform.accountname.value=='' || document.setupform.password.value=='' || document.setupform.emailaddress.value=='') {return false;} else {return true;}
}

function getOffsetTop(elm) {

  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
  mOffsetTop = mOffsetTop+elm.offsetHeight;
  return mOffsetTop;
}

function getOffsetLeft(elm) {

  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetLeft;
}


function hidePopupMenu(){
	if (popupMenuId==0) return 0;
	document.getElementById(popupMenuId).style.display='none';
	popupMenuId = 0;
}
	

function popupMenu(id,pid){
	if (popupMenuId!=0) hidePopupMenu();
	var elem  = document.getElementById(id);
	var pelem  = document.getElementById(pid);

	var left = getOffsetLeft(pelem);
	var top  = getOffsetTop(pelem);

	elem.style.display  		= 'block';
	elem.style.left     		= left+'px';
	elem.style.top      		= top+'px';
	
	popupMenuId    				= id;

	document.body.onclick = function (){
		if (!popupMenuExec){
			if (popupMenuId!=0){
				var elem  = document.getElementById(popupMenuId);
				if (elem.style.display != 'none') {hidePopupMenu();}
			}
		}else popupMenuExec = 0;
	}
	popupMenuExec = 1;
	
}


function appendTag(target,tag)
{
   if (document.getElementById(target).value.indexOf(tag)==-1)
   {
	if (document.getElementById(target).value != '') tag = ', '+tag;
  	document.getElementById(target).value = document.getElementById(target).value + tag;
   }
}

function getFullDocSize(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}

/*
function doHandleWindow(page)
{
   var dim = getFullDocSize();
   win = document.createElement("IFRAME");
   win.setAttribute('id','subcontentFrame');
   win.setAttribute('src',page+'?'+Math.round(Math.random(1000)));
   win.setAttribute('scrolling','no');
   win.className = 'subcontentIframe';
   document.getElementById('bgBlocker').style.height = dim[1]; 
   document.getElementById('bgBlocker').style.display = 'block';
   document.getElementById('bgBlocker').style.position = 'absolute';
   document.getElementById('bgBlocker').style.width = '100%'; 
   document.body.appendChild(win);
}
*/




function destroyHandleWindow(){
   win = document.getElementById('subwin');
   if (win == null) return 0;
   win.style.display 	= 'none';
   document.getElementById('bgBlocker').style.display = 'none';
   document.body.removeChild(win);
   xnewspopupId			= 0;

}


function beginLoadWindow(){
	div	= document.createElement("div");
	
	var dim	  = getFullDocSize();
	var pleft = Math.round(((dim[0]/2) - 60));
	div.style.left = pleft+'px';
	var ptop  = Math.round(((dim[1]/2) - 15));
	div.style.top  = ptop+'px';  
	
	div.className	=	"loadingBox";
	div.setAttribute("id","loadingBox");
	img	= document.createElement("img");
	img.src = skinUrl + 'img/loader2.gif';
	img.border = 0;
	div.appendChild(img);
	document.body.appendChild(div);
}

function endLoadWindow(){
	//Block the rest of the page
	var dim = getFullDocSize();
	if (document.getElementById('bgBlocker')){
		document.getElementById('bgBlocker').style.height = dim[1]+100+'px'; 
		document.getElementById('bgBlocker').style.display = 'block';
	/*document.getElementById('bgBlocker').style.position = 'absolute';
	document.getElementById('bgBlocker').style.width = '100%'; 
	document.getElementById('bgBlocker').className  = 'bgBlocker';*/ 
	}
	div	= document.getElementById("loadingBox");
	div.style.display = 'none';
	document.getElementById('subwin').style.display		= 'block';
	document.getElementById('subwin').style.visibility	= 'visible';
	
	document.body.removeChild(div);
}

function doHandleWindow(targetUrl,id){

	
	beginLoadWindow();
	win = document.createElement("IFRAME");
	win.setAttribute('id','subwin');
	
	win.setAttribute('src',targetUrl);
	win.setAttribute('scrolling','no');
	win.setAttribute('border','0');
	win.className		= 'subcontentIframe';
	xnewspopupId = id;
	document.body.appendChild(win);
//	win.style.display	= 'none';
	
}


function popupFrame(page,wheight,wwidth,id,pid)
{
 if (xnewspopupId !=0) destroyPopup();     xnewspopupId = 0;
   win = document.createElement("IFRAME");
   win.setAttribute('id',id);
   win.setAttribute('src',page);
   win.setAttribute('scrolling','no');
   win.className = 'subcontentIframe2';
   var pelem  = document.getElementById(pid);
   var left = getOffsetLeft(pelem);
   var top  = getOffsetTop(pelem);

   win.style.left = left+'px';
   win.style.top = top+'px';

   if (wheight) win.style.height = wheight+'px';
   if (wwidth) win.style.width = wwidth+'px';

	if (posrev){
		win.style.left = left - wwidth;
		win.style.top = top - wheight;
	}
	
	
   document.body.onclick = function ()
   {

  	if (!popupExec)
  	{
        if (xnewspopupId !=0)
  	{
  	 var win  = document.getElementById(xnewspopupId);
  	 if (win.style.display != 'none') {destroyPopup();}
  	}
  	}else popupExec = 0;
   }
   xnewspopupId = id;
   popupExec  = 1;
   document.body.appendChild(win);
}

function popupColors(id,pid)
{
 popupFrame(skinUrl+'color_selector.html',180,210,id,pid);return 0;
}

function destroyPopup()
{
   win = document.getElementById(xnewspopupId);
   win.style.display = 'none';
   document.body.removeChild(win);
   popupExec = 0;
   xnewspopupId = 0;


}


function countCharacters(target,disp,max){
	target	 = document.getElementById(target);
	document.getElementById(disp).innerHTML = max-target.value.length;
	
}




