var myGPedigree;
var licWin;
var DISAGREE=false;
var glicrequest;


//
// setLoggedIn:
//
function setLoggedIn(userId){
	
	myGPedigree.setLoggedIn(userId);

}

//
// copyNodeAndPrint:
//
function copyNodeAndPrint(id,printFlag){
	
	document.getElementById(id).appendChild(opener.myGPedigree.getSVGDocumentFragment());
	if(printFlag)
	window.setTimeout('if(window.print) window.print();',1000);
	
}

//
// _addContentNodeForIE:
//
function _addContentNodeForIE(){
	
	var _contentNode = document.createElement("div");
	_contentNode.id = "content";
	var pElement = document.createElement("p");
	pElement.appendChild(document.createTextNode("The Web Service requires native SVG rendering support. Please use Firefox 1.5+ or Opera 9.0+."));
	_contentNode.appendChild(pElement); 
	window.document.body.appendChild(_contentNode);
}

//
// pageLoader:
//
function pageLoader(requestFilesFlag,errorMessage,hideLicense){
	
	
	// Bug out quickly if it is IE:
	//var detector=new gBrowserDetector();
	//if(detector.code=="E"){
	//	// "Downgrade" images for Internet Explorer --
	//	// -- this is a really cheap but fast way to fix IE's transparent PNG bugs:
	//	document.getElementById("title").style.backgroundImage="url('../images/madelinePublicServiceLogoIdx.png')";
	//	document.getElementById("umlogo").style.backgroundImage="url('../images/UMKECIdx.png')";
	//	// "Downgrade" fixed elements to "absolute" instead:
	//	document.getElementById("menu").style.position="absolute";
	//	document.getElementById("title").style.position="absolute";
	//	document.getElementById("umlogo").style.position="absolute";
	//	
	//	// No point in showing these in IE:
	//	document.getElementById("myTable").style.display="none";
	//	//
	//	// Add a "content" node discussing the problem with IE:
	//	// ...
	//	_addContentNodeForIE();
	//	return;
	//}
	
	if(!hideLicense) {
		// Show the license in a window
		showLicenseWindow();
		return;
	}
	
	var grid = document.getElementById("myTable"); // table node
	grid.style.display="";
	var svgContainer = document.getElementById("svgContainer"); // svg container node
	
	// Initialize the grid fields in order:
	var gridFields = new Array('FamilyId','IndividualId','Gender','Father','Mother','Deceased','Proband','DOB','MZTwin','DZTwin','Sampled','Affected','FirstName','LastName');
	
	//
	// Scrollable Table:
	//
	var fixedColumnCount=2; // Family Id and Individual Id
	var content = document.getElementById("content");
	var gst = new gScrollableTable("scrollTable", grid , fixedColumnCount, content);
	
	// Table editor
	// NOTE BENE: We want to allow editing ofthe "IndividualID" column 
	//        so we subtract one from the fixedColumnCount:
	var gte = new gTableEditor(gst.getNode(),fixedColumnCount-1,gst);
	
	//
	// Pedigree Manager:
	//
	
	myGPedigree = new gPedigreeManager(svgContainer,gst,gte,"pedigree.php","svgDC",gridFields,true);
	
	if(requestFilesFlag) myGPedigree.requestFiles();
	if(errorMessage) alert("Error :"+errorMessage);
	return;
	
}

//
// Code that handles displaying license window for user to agree to the terms and conditions:
//
function disagreeHandler(evt){
	
	DISAGREE = true;
	licWin.show(false);
	
}

function agreeHandler(evt){
	
	_glicrequest.abort();
	_glicrequest.setQueryTermsString('lic='+1);
	_glicrequest.load();
	
}

function agreePHandler(evt){
	
	_glicrequest.abort();
	_glicrequest.setQueryTermsString('lic='+2);
	_glicrequest.load();
	
}

function setLicense(request){
	
	licWin.show(false);
	pageLoader('','',1);
	
	
}


function showLicenseWindow(){
	
	// gRequest object
	_glicrequest = new gRequest("webservice.php",setLicense,"post");
	
	var licenseSection = document.createElementNS(NS.xhtml,"div");
	licenseSection.id = "lic"; 
	var licenseString = "Madeline 2.0 Public Web Service is Copyright (c) 2006, 2007 by the Regents of the University of Michigan.";
	var pLicense = document.createElementNS(NS.xhtml,"p");
	var textElement = document.createTextNode(licenseString);
	pLicense.appendChild(textElement);
	licenseSection.appendChild(pLicense);
	
	var pExperimental = document.createElementNS(NS.xhtml,"p");
	var textElement2 = document.createTextNode("This is an experimental service which has no warranty and which could result in the loss of data.");
	pExperimental.appendChild(textElement2);
	licenseSection.appendChild(pExperimental);
	
	var pWarranty = document.createElementNS(NS.xhtml,"p");
	var textElement3 = document.createTextNode("Except when otherwise stated in writing the copyright holders and/or other parties provide the program and web service 'as is' without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.");
	pWarranty.appendChild(textElement3);
	licenseSection.appendChild(pWarranty);
	
	var pUser = document.createElementNS(NS.xhtml,"p");
	var textElement4 = document.createTextNode("The user will assume any risk related to the quality and performance of the program and web service. Should the program and web service prove defective, the user will assume the cost of all necessary servicing, repair or correction.");
	pUser.appendChild(textElement4);
	licenseSection.appendChild(pUser);
	
	var aElement = document.createElementNS(NS.xhtml,"a");
	aElement.setAttributeNS(null,"href","http://eyegene.ophthy.med.umich.edu/madeline/disclaimer.php");
	aElement.appendChild(document.createTextNode("See Disclaimer for full details."));
	pUser.appendChild(aElement);
	
	var disagree = new gFormButton("Disagree","disagree",licenseSection);
	var agree    = new gFormButton("Agree to the license today","agree",licenseSection);
	var agreeP = new gFormButton("Agree to the license for a year","agreep",licenseSection);
	licWin = new gWindow("License","licw",225,205,400,550,licenseSection);
	licWin.allowClose(false);
	licWin.allowResize(false);
	disagree.addEventListener("click",disagreeHandler);
	agree.addEventListener("click",agreeHandler);
	agreeP.addEventListener("click",agreePHandler);
	
}


