var splashWin, autoCloseTimeoutHandle, ontopIntervalHandle;
var _template_id = null;
var calendarSelectedInfo = "";
var calendarSelectedCell, calendarSelectedCellClass;

function launchSplashWin(contentType, windowId, contentString, width, height, left, top, autoCloseTime) 
{
	var c = encodeURI(contentString).replace(/\+/g, '%2B');
	launchSplashWin2(contentType, windowId, c, width, height, left, top, autoCloseTime, 1, 0) 	
}

function launchSplashWin2(contentType, windowId, contentString, base_width, base_height, left, top, autoCloseTime, scroll, resizable) 
{
	var status_offset = 40;
	
	if(base_height == -1 || base_width == -1)
	{
		w_proportion = 800 / 1024;
		h_proportion = 700 / 768;
		
		x = screen.width;
		y = screen.height;
		
		width = x * w_proportion;
		height = y * h_proportion;
	}
	else
	{
		width = base_width;
		height = base_height+status_offset;
	}
	
	var w = window.screen.width;
	var h = window.screen.height;
	var l = (left != null) ? left : (w-width)/2;
	var t = (top != null) ? top : (h-height)/2;
	
	if(contentString.indexOf('?') == -1)
		contentString += "?";
	else
		contentString += "&";
	contentString += "newpopup=false";
	
	var uri = (contentType.toLowerCase() == "uri") ? contentString : "";
	
	splashWin = window.open(uri, windowId, 'width='+width+',height='+height+',top='+t+',left='+l+',fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+scroll+',resizable='+resizable);
	splashWin.opener = this;
	
	//splashWin.blur();	// Hide while updating
	//window.focus();
	//splashWin.focus();
}

function ParentUpdateWOFrame()
{
	try
	{
		window.opener.top._NavigatorContent.location = window.opener.top._NavigatorContent.location; 
	}
	catch(Exception)
	{
		window.opener.location.reload(true);
	}
	
	alert('Os dados foram guardados com sucesso!'); 
	self.close();
}

function updateSelectedId(radio)
{
	_template_id = radio.value;	
}

function EnableControl(control_name)
{
	var control = FindControl(control_name);
	control.disabled = false;
}

function buildURL(openType, width, height)
{
	var cat = getQueryVariable('target');
	var url = "";
	
	if(openType == "js")
		url = "javascript:launchSplashWin('uri','build','ArticleRender.aspx?reason=New&cat="+cat+"&template="+_template_id+"',"+width+","+height+",null,null)";
	else
		url = 'ArticleRender.aspx?reason=New&cat='+cat+'&template='+_template_id;
	
	
	window.opener.location = url;
	self.close();
}

function getQueryVariable(variable) 
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++)
	{
		var pair = vars[i].split("=");
    
		if (pair[0] == variable)
		 {
			 return pair[1];
		}
	}
}

function CalendarExpiryOnClick()
{
  if( divCalendarExpiry.style.display == "none")
    divCalendarExpiry.style.display = "";
  else
    divCalendarExpiry.style.display = "none";
  return false;
}

function setCalendarExpiryDate(date)
{
	//alert ('date = ' + date);
	document.getElementById('Static_ExpiryData').value = date;
	CalendarExpiryOnClick();
}

function SetComboBoxSelectedIndex(cell_id, dropdownlist)
{
	var current_id = "";
	
		 
		 var control  = FindControl("_selected_combos");
		 
		 if(control != null)
		 {
				var current_data = control.value;
				current_data += cell_id+"&"+dropdownlist.selectedIndex+";";
				control.value = current_data;
		 }
		
}

// Finds in the current page, the control that contains the given string. If the control is found, returns the control, else returns null
function FindControl(control_name)
{

	for(var f=0; f < document.forms.length;f++)
	{
		for(var i=0; i < document.forms[f].elements.length; i++)
		{
			current_id = document.forms[f].elements[i].id;
			 
			if(current_id.indexOf(control_name) != -1)
				return document.forms[f].elements[i];
		}
	}
	
	return null;
}

//Finds the controls with the given substring , and sends it to the parent.
function FindControlInParent(control_name)
{
	var open = window.opener;
	var parent = self.parent.document;
	if (open)
		parent = open.document;
		
	//alert(parent);
	for(var f=0; f < parent.forms.length;f++)
	{
	for(var i=0; i < parent.forms[f].elements.length; i++)
	{
		 current_id = parent.forms[f].elements[i].id;
		 
		 if(current_id.indexOf(control_name) != -1)
			return parent.forms[f].elements[i];
	}
	}
	
	return null;
}

function FindControlInSelf(control_name)
{
	var open = window.opener;
	var parent = self.parent.document;
	if (open)
		parent = open.document;
	
	for(var f=0; f < parent.forms.length;f++)
	{
		for(var i=0; i < parent.forms[f].elements.length; i++)
		{
			current_id = parent.forms[f].elements[i].id;
				
			if(current_id.indexOf(control_name) != -1)
				return parent.forms[f].elements[i];
		}
	}
	
	return null;
}

function ClearControl(control)
{
	ctrl = FindControl(control);
	//alert(control + "," + ctrl);
	if (ctrl)
		ctrl.value = "";
		
}

function SendMultiToParent(control, hidctrl)
{
	SetValueInParent(control, "", true);
	SetValueInParent(hidctrl, "", true);
	for (i=0; ; i++)
	{
		check = FindControl('lookCB' + i);
		if (!check)
		{
			break;
		}
		else
		{
			if (check.checked)
			{
				hid = FindControl('hidCB' + i);
				SetValueInParent(control, hid.value, false);
				SetValueInParent(hidctrl, hid.value, false);
			}
		}	
	}
}

function LoadDefaults(control)
{
	ctrl = FindControlInParent(control);
	if (ctrl)
	{
		str = ctrl.value;
		var vars = str.split(";");
		for (var i=0;i<vars.length;i++)
		{
			for (j=0; ; j++)
			{
				hid = FindControl('hidCB' + j);
				if (!hid)
				{
					break;
				}
				else
				{
					//alert(hid.value.toLowerCase() + "==" + vars[i].toLowerCase());
					if (hid.value.toLowerCase() == vars[i].toLowerCase())
					{
						check = FindControl('lookCB' + j);
						check.checked = true;
						break;
					}
				}	
			}
		}
	}
}

function SetValueInParent(control, val)
{
	ctrl = FindControlInParent(control);
	
	if(ctrl)
		ctrl.value += val+"&";	
}

function SetValueInParent(control, val, overwrite)
{
	ctrl = FindControlInParent(control);
	
	if(ctrl)
	{
		if(overwrite == true || ctrl.value == "")
			ctrl.value = val;
		else
			ctrl.value += ";" + val;
	}
}

function SetValueInSelf(control, val)
{
	ctrl = FindControlInSelf(control);
	
	if(ctrl)
		ctrl.value += val+"&";	
}

function SetValueInSelf(control, val, overwrite)
{
	ctrl = FindControlInSelf(control);
	
	if(ctrl)
	{
		if(overwrite == true || ctrl.value == "")
			ctrl.value = val;
		else
			ctrl.value += ";" + val;
	}
}

function SetValueInControl(control, val)
{
	SetValueInControl(control, val, true);
}

function SetValueInControl(control, val, overwrite)
{
	ctrl = FindControl(control);
	
	if(overwrite == true || ctrl.value == "")
		ctrl.value = val;
	else
		ctrl.value +=  ";" + val;
}

function SetOptionInParent(control, val,text, newpopup)
{
	if (!newpopup)
	{
		ctrl = FindControlInParent(control);
		opt = window.opener.document.createElement("option");
	}
	else
	{
		ctrl = FindControlInSelf(control);
		opt = self.parent.document.createElement("option");
	}
	
	opt.text =  text;
	opt.value = val;
	ctrl.options.add(opt);
}

function SetNewLineInParent(control, val,text)
{
	ctrl = FindControlInParent(control);
	ctrl.value += val+"\n";
}

//Control1- The control where the selected item will be removed (mandatory)
//Control2-  The second controls that will be updated.
function DeleteSelectedItems(control1, control2)
{
	var _control  = FindControl(control1);

	for(var i=0; i < _control.options.length; i++)
	{
		if(_control.options[i].selected)
		{			
			var deleted_files = FindControl(control2);
			
			if(deleted_files != null)
			{
				deleted_files.value += _control.options[i].value+"&";
				_control.remove(i);
			}
		}
	}
}

//Set the selected  file url in tha parent box. The box in tha parent must have in it's id the substring ImageName.
function SetFileUrl(control_name)
{
	var control = FindControl(control_name);
	var destination = FindControlInParent("ImageName");
	
	var url = control.value;
	destination.value = url;
}

function teste()
{
	alert("teste");
}

function SendArticleBack(type)
{
	topten = FindControl("DropDownListTenLatest");	
	article = document.forms[0].id_style;
	
	if(article.value != "")
	{
		SetValueInParent(type, 'ArticleViewer&target='+document.forms[0].id_style.value,true);
		self.close();
	}
	else if(topten.value != "[ Seleccione o Artigo ]")
	{
		SetValueInParent(type, 'ArticleViewer&target='+topten.value,true);
		self.close();
	}
	else
	{
		alert("Por favor seleccione um artigo!");
	}	
}


//Refresh the tree view, positioning selected node.
function refreshTree(id)
{
	 var url =  top.NavigatorExplorer.location.href;
	 var index = url.indexOf("cat");
	 
	 if(index != -1)
	 {
		var new_url = url.substring(0,index)
		url = new_url+"&cat="+id;
	 }
	 else
	 {
		url +="&cat="+id;
	 }
	 
	 top.NavigatorExplorer.location.href = url;
}


function HighlightRow(row)
{
	row.className = "HighLightRow";
}

function NoHighlightRow(row)
{
	if((row.rowIndex % 2) == 0)
	{
		//PAIR ROW INDEX
		row.className = "NoHighLightRowPair";
	}
	else
	{
		row.className = "NoHighLightRowOdd";
		}
}

function ClearSearchText(box)
{
	box.value = '';
}

function PutSearchText(box, text)
{
	if(box.value == '')
		box.value = text;
}

function ValidateEmail(control)
{	
	mailcontrol = FindControl(control);
	
	return isValidEmail(mailcontrol.value);	
}

function isValidEmail(who) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

function SetLoginFocus(control)
{
	box = FindControl(control);
	
	box.focus();
}

function ShowInfo(control, text)
{
	c = document.getElementById(control);
	if (text == "")
	{
		c.innerHTML = calendarSelectedInfo;
	}
	else
	{
		c.innerHTML = text;
	}
}

function calendarCellClick(control, divcontrol)
{
	if (calendarSelectedCell != null)
	{
		calendarSelectedCell.className = calendarSelectedCellClass;
	}
	
	// Finds the control
	//c = document.getElementById(control);
	c = FindControl(control);
	if (c != null)
	{
		// Stores previous values
		calendarSelectedCell = c;
		calendarSelectedCellClass = c.className;
		// Sets new values
		c.className = "SelectedCalendarCell";
	}
	
	c2 = document.getElementById(divcontrol);
	// Stores the contents of the DIV
	if (c2 != null)
	{
		calendarSelectedInfo = c2.innerHTML;
	}
}

function StartTimer()
{
	setTimeout("RefreshData()", 60000);
}

function RefreshData()
{
	document.forms[0].submit();
}

function ToggleCSS(el, new_css)
{		
	if (el) {
		el.className = new_css;
	}
	
	return false;
}

function HideShowElement(element, dsp)
{
	el = document.getElementById(element);
	
	if(el && el.style)
		el.style.display = dsp;
}

function UpdateRelatedLookup(related_lookup, me, val)
{	
	LookupTable.ChangeRelatedLookup(related_lookup, me, val, UpdateRelatedLookup_CallBack)
}

function UpdateRelatedLookup_CallBack(response)
{	
}

function InsertHTMLOnEditor(name, link, newwindow)
{
	FCKAddNewArticleLink.Insert(name, link, newwindow);
	return true;
}

function InsertRepositoryFileLinkOnEditor(Type, FileName, DirName)
{
	FCKAddRepositoryFileLink.Insert(Type, FileName, DirName);
	return true;
}

function redirect(url)
{
	window.location =url;
}

function ShowLookup(what)
{
	ShowLookup(what, -1, -1, -1, -1);
}

function ShowLookup(what, width, height)
{
	ShowLookup(what, width, height, -1, -1);
}

function ShowLookup(what, width, height, left, top)
{
	HideCombos();
	var c = encodeURI(what).replace(/\+/g, '%2B');
		if(c.indexOf('?') == -1)
		c += "?";
	else
		c += "&";
	c += "newpopup=true";
	
	//alert(c);
	var infoDiv = document.getElementById('InfoDiv');
	var infoDivContent = document.getElementById('InfoDivContent');

	var frame = document.getElementById('InfoDivIFrame');
	frame.src = c;

	
	if (width > -1 && height > -1)
	{
		infoDivContent.style.width = width;
		infoDivContent.style.height = height;
		CenterPopUp();
	}
	else
	{
		infoDivContent.style.width = '80%';
		infoDivContent.style.height = '80%';
		infoDivContent.style.left = '10%';
		infoDivContent.style.top = '10%';
	}
		
	infoDiv.style.display = '';
	infoDivContent.style.display = '';
	
	document.body.style.overflowX = 'hidden';
	
	if (!document.all)
		infoDiv.style.height = document.height;
	
	MemEater();
	
	return false;
}

function MemEater()
{
	var infoDiv = document.getElementById('InfoDiv');
	if(infoDiv.style.display == '')
	{
		top_offset = 0;
		right_offset = 0;
		left_dist = 0;
		ie4=document.all;
		ns6=document.getElementById&&!document.all;
		ns4=document.layers;
		
		if (ie4) 
		{
			infoDiv.style.pixelTop=document.body.scrollTop;
		}
		else if (ns6) 
		{
			infoDiv.style.top=window.pageYOffsett;
		}
		else if (ns4) 
		{
			eval(infoDiv.top=eval(window.pageYOffset));
		}
		
		CenterPopUp();
		setTimeout("MemEater()", 5);
	}
	else
	{
		clearTimeout();
	}
}

function DisplayInfo()
{
	var infoDiv = document.getElementById('InfoDiv');
	var infoDivContent = document.getElementById('InfoDivContent');
	
	infoDiv.style.height = document.height;
	
    if(http.readyState == 4 && http.status == 200)
    {
		var response = http.responseText;
		
		ChangeText(infoDivContent, response);
		infoDiv.style.display = '';
		infoDivContent.style.left = (window.screen.availWidth - 400) / 2;
		infoDivContent.style.display = '';
    }
}

function CloseInfo()
{
	var open = window.opener;
	var parent = self.parent.document;
	if (!open)
	{
		ShowCombosIn(parent);
		var infoDiv = parent.getElementById('InfoDiv');
		var infoDivContent = parent.getElementById('InfoDivContent');

		infoDiv.style.display = 'none';
		infoDivContent.style.display = 'none';
		
		clearTimeout();
	}
	else
	{
		ShowCombosIn(open.document);
		self.close();
	}
	
	return false;	
}

// Close the PopUp window, used by PopUpCloseImg
function ClosePopUp()
{
	ShowCombosIn(document);
	var infoDiv = document.getElementById('InfoDiv');
	var infoDivContent = document.getElementById('InfoDivContent');

	infoDiv.style.display = 'none';
	infoDivContent.style.display = 'none';
	
	return false;	
}

// Sets the PopUp Title, must be used in the iFrame
function SetPopUpTitle(title)
{
	var parent = self.parent.document;
	var popUpTitle = parent.getElementById('PopUpTitle');
	popUpTitle.innerHTML = title;
}

function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(window.ActiveXObject)
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}
function CenterPopUp()
{
	var infoDivContent = document.getElementById('InfoDivContent');
	var conFrame = self.parent.document.getElementById('contentFrame');
	
	var dw = infoDivContent.style.width;
	var dh = infoDivContent.style.height;
	var idw = dw.indexOf("px");
	var idh = dh.indexOf("px");
			
	if (idw > -1 && idh > -1)
	{
		dw = dw.substr(0, idw);
		dh = dh.substr(0, idh);
		
		var w = conFrame.width - dw;
		var h = conFrame.height - dh;
		var l = w/2;
		var t = h/2;		
		
		infoDivContent.style.left = l;
		infoDivContent.style.top = t;
	}
}
/*
function ChangeText(el, text)
{
	s = text.indexOf("<form name=\"Form1\"");
	e = text.indexOf("id=\"Form1\">") + 11;// - s;
	//alert(text.substr(0, s));
	//alert(text.substr(e));
	el.innerHTML = text.substr(0, s) + text.substr(e);
	return false;
}
*/

/* DropDownList STUFF */

function HideCombos()
{
	var parent = document;
	//alert('teste');
	var ns4 = (document.layers) ? true : false;
	var ie4 = (document.all && !document.getElementById) ? true : false;
	var ie5 = (document.all && document.getElementById) ? true : false;
	var ns6 = (!document.all && document.getElementById) ? true : false;
	
	for(var i=0; i < parent.forms[0].elements.length; i++)
	{
		current_element = parent.forms[0].elements[i];
		 //alert(current_element.type);
		if (current_element.className == 'comboBoxData' || (current_element.type == "select-one" || current_element.type == "select-multiple"))
		{
			if (ie4 || ie5) current_element.style.visibility = 'hidden';
			if (ns4) current_element.visibility = 'hidden';
		}
	}
}

function ShowCombos()
{
	ShowCombosIn(document);
}

function ShowCombosIn(doc)
{	
	var ns4 = (doc.layers) ? true : false;
	var ie4 = (doc.all && !doc.getElementById) ? true : false;
	var ie5 = (doc.all && doc.getElementById) ? true : false;
	var ns6 = (!doc.all && doc.getElementById) ? true : false;
	
	for(var i=0; i < doc.forms[0].elements.length; i++)
	{
		 current_element = doc.forms[0].elements[i];
		 		 
		if (current_element.className == 'comboBoxData' || (current_element.type == "select-one" || current_element.type == "select-multiple"))
		{
			if (ie4 || ie5) current_element.style.visibility = 'visible';
			if (ns4) current_element.visibility = 'visible';
		}
	}
}

var http = createRequestObject();


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
