function reCenterDialog(){
   $('#divDialog').dialog('option', 'position', 'center');
}
function setDialogContentHeight(height){
	var heightDiff = $('#divDialog').dialog('option', 'height') - $('#ifMyDocuments').height();
   $('#divDialog').dialog('option', 'height', height + heightDiff);
}

function showDialog(title, href, width, height) {
	logMyDocsActivity("Parent: Opening dialog ("+title+", "+href+", "+width+", "+height+")");
	$('#ifMyDocuments').attr('src', href);
	if(width)
		$('#divDialog').dialog('option', 'width', width);
	if(height)
		$('#divDialog').dialog('option', 'height', height);
	$('#divDialog').dialog('option', 'title', title);
	$('#divDialog').dialog('open');
	return false;
}

function showSaveAnswersDialog(title, href, width, height) {
	logMyDocsActivity("Parent: Opening dialog ("+title+", "+href+", "+width+", "+height+")");
	var vstring = getVarString();
	if(width)
		$('#divDialog').dialog('option', 'width', width);
	if(height)
		$('#divDialog').dialog('option', 'height', height);
	$('#divDialog').dialog('option', 'title', title);
	$('#divDialog').dialog('open');
	postToTarget(href, 'ifMyDocuments', {'variablestring':vstring});
	return false;
}

function hidePopWin() {
	logMyDocsActivity("Parent: Closing dialog (1).");
	$('#divDialog').dialog('close');
	$('#ifMyDocuments').attr('src', '');
	$('#ifMyDocuments').innerHTML='';
	toggleLoader(true);
}

function invalidateCache() {
	var invalid = false;
	for(var i=0; i<window.frames.length; i++) {
		try {
			if(window.frames[i].invalidateCache) {
				window.frames[i].invalidateCache(hidePopWin2);
				invalid = true;
			}
		}
		catch(e) {}
	}
	if(!invalid)
		hidePopWin2();
}

function hidePopWin2() {
	logMyDocsActivity("Parent: Closing dialog (2).");
	$('#ifMyDocuments').attr('src', '');
	$('#ifMyDocuments').innerHTML='';
	toggleLoader(true);
}

var tipOpen = false;
$(document).ready(function() {
	$('#divDialog').dialog({
		autoOpen: false,
		width:900,
		height:400,
		draggable: true,
		resizable: false,
		modal: true,
		close: function(ev, ui) { 
			logMyDocsActivity("Parent: Closing dialog (3).");
			invalidateCache(); 
		}
	});
	$('#divTipDialog').dialog({
		autoOpen: false,
		width:300,
		height:225,
		draggable: true,
		resizable: true,
		modal: true,
		close: function(ev, ui) { 
			tipWindow_Callback(getObject('chkShow').checked);
			if(tipOpen) {
				closeTipWindow(false);
			} 
		}
	});
});

function toggleLoader(set) {
	if(set)
		$('#tblLoader').show();
	else
		$('#tblLoader').hide();
}

var _show_tip = true;
function tipWindow() {
	logMyDocsActivity("Parent: Opening tip window.");
	$('#divTipDialog').dialog('option', 'title', 'Saving Answers');
	$('#divTipDialog').dialog('open');
	tipOpen=true;
 	return false;
}

function tipWindow_Callback(arg) {
	 if(arg) {
		  var expiry = new Date();
		  expiry.setDate(expiry.getDate()+(365*3));
		  document.cookie = "HideSaveAnswersTip=1;path=/;expires="+expiry.toGMTString();
		  _show_tip = false;
	 }
}

var saveAnswersHref = '';
function closeTipWindow(cont) {
	logMyDocsActivity("Parent: Closing tip window.");
	try {
		if(tipOpen) {
			tipOpen=false;
			$('#divTipDialog').dialog('close');
		}
	} catch (e) { }
	if(cont)
		showSaveAnswersDialog("Save Answers", saveAnswersHref, 581, 439);
}

function replaceCopyright(html) {
	if(html.search(/\x0a\xa9/i) > 0)
		html = html.replace(/\x0a\xa9/ig, '&copy;');
	else if(html.search(/\xa9/i) > 0)
		html = html.replace(/\xa9/ig, '&copy;');

	if(html.search(/\x0a\xa3/i) > 0)
		html = html.replace(/\x0a\xa3/ig, '&pound;');
	else if(html.search(/\xa3/i) > 0)
		html = html.replace(/\xa3/ig, '&pound;');
	
	while(html.search(String.fromCharCode(8482)) > 0)
	{
		html = html.replace(String.fromCharCode(8482), '&trade;');
	}
	/*
	if(html.search(String.fromCharCode(8482)) > 0)
		html = html.replace(String.fromCharCode(8482), '&trade;');
	*/

	return html;
}

var saveDocument_Callback = null;
function saveDocument(format, filename, callback) 
{
	logMyDocsActivity("Parent: Saving document ("+format+", "+filename+")");
	if(callback)
		saveDocument_Callback = callback;
	if(format == 'html')
		saveAsHTML(filename);
	if(format == 'doc')
		saveAsWord(filename);
	if(format == 'pdf')
		saveAsPDF(filename);
}

function logMyDocsActivity(activity) {
	// Uncomment to enable activity logging
	var postData = "logged_data="+escape(activity)+"&log_file="+escape("MyDocs/activity-"+ip+".log")+"";
	httpRequest(postData, "/temp/ajax_logger.php", null, null, false, true);
}

