function Link(label, URL, subsection)
{
	this.className = "Link";
	this.label = label;
	this.URL = URL;
	this.subsection = subsection;
}

Link.prototype.getURL = function(){
	return this.URL;
}

Link.prototype.getLink = function(clickable){
	if(typeof(clickable) == "undefined" || clickable == ""){
		var clickable = this.label;
	}
	var link = "<a href=\"" + this.URL + "\">" + clickable + "</a>";
	return link;
}

function Wizard(server, customer, label, wizardID, codebase, opts, subsection)
{
	this.className = "Wizard";
	this.server = server;
	this.customer = customer;
	this.label = label;
	this.wizardID = wizardID;
	this.codebase = codebase;
	this.opts = opts;
	this.subsection = subsection;
}

Wizard.prototype.getURL = function(){
	var URL = "http://" + this.getServerRoot() + "/" + this.getCodebase() + "/WizardServlet?DumpWizardFromSession=true&customer=" + this.customer + "&wizardID=" + this.wizardID + this.opts;
	return URL;
}

Wizard.prototype.getLink = function(clickable){
	if(typeof(clickable) == "undefined" || clickable == ""){
		var clickable = this.label;
	}
	var link = "<a href=\"http://" + this.getServerRoot() + "/" + this.getCodebase() + "/WizardServlet?DumpWizardFromSession=true&customer=" + this.customer + "&wizardID=" + this.wizardID + this.opts + "\">" + clickable + "</a>";
	return link;
}

Wizard.prototype.getCodebase = function(){
	if(this.codebase == "default" || this.codebase == "" || typeof(this.codebase) == "undefined") {
		switch (this.server)
		{
		case "webdc":
			return "DesignCenter/servlet";
			break;
		case "stage":
			return "DesignCenter/servlet";
			break;
		case "apps":
			return "DesignCenter/servlet";
			break;
		case "localhost":
			return "servlet";
			break;
		default:
			return "DesignCenter/servlet";
			break;
		}
	} else {
		return this.codebase;
	}
}

Wizard.prototype.getServerRoot = function(){
	switch (this.server)
	{
	case "webdc":
		return "webdc.transim.com";
		break;
	case "stage":
		return "stage.transim.com";
		break;
	case "apps":
		return "apps.transim.com";
		break;
	case "localhost":
		return "localhost:9090";
		break;
	default:
		return "stage.transim.com";
		break;
	}
}

function hasSubsection(w) {
	for(var i = 0;i<w.length;i++){
		if(w[i].subsection != "" && typeof(w[i].subsection != "undefined")){
			return true;
		}
	}
	return false;
}

function getRowCount2(w,si) {
	var count = 0;
	var found = false;
	for(var i = si;i<w.length;i++){
		if(w[i].subsection == w[si].subsection){
			found = true;
			count++;
		} else {
			if(found){
				 return count;
			}
		}
	}
	return count;
}

function createWizardLinks(sectionTitle,linkObjects,listStyle,allowServerSelection) {
	if(typeof(allowServerSelection) == "undefined"){
		allowServerSelection = false;
	}
	// draw section header
	if(sectionTitle !=""){
		drawSectionTitle(sectionTitle);
	}
	// are there subsections?
	var hasSubsections = hasSubsection(linkObjects);
	var lastSection;
	//loop through wizards or links ids and create links
	for(var i = 0;i<linkObjects.length;i++){
		var firstRow = false;
		if(i == 0 || linkObjects[i].subsection != lastSection){
			var printSubSection = true;
			var subSectionRowCount = getRowCount2(linkObjects,i)
		}
		if(hasSubsections && printSubSection){
			var printSubSection = false;
			var firstRow = true;
			document.write("<tr><td class='wizardLink' rowspan='"+subSectionRowCount+"'>"+linkObjects[i].subsection+"</td>");
		}
		createLaunchRow(linkObjects[i],hasSubsections,firstRow,allowServerSelection);
		var lastSection = linkObjects[i].subsection;
	}
	drawSpacerRow(10);
}

function drawPartsLinks2(sectionTitle,wizard,optionsArray,partsArray,listHeight) {
	// draw section header
	if(sectionTitle !=""){
		drawSectionTitle(sectionTitle);
	}

	document.write(""
	+"<tr>"
	+"	<td class='wizardLink'>"+wizard.subsection+"</td>"
	+"	<td class='wizardLink'>"
	+		"<div class='partsList' style='height:"+listHeight+"px;'>"
	+"			<table width='100%'>"
	+"				<tr>");

	for(var i=0;i<optionsArray.length;i++){
		document.write("<td class='wizardLinkHeader'>"+optionsArray[i]+"</td>");
	}
	document.write(""
	+"	<td class='wizardLinkHeader'>Action</td>"
	+"</tr>");
	for(var i=0;i<partsArray.length;i++){
		var label = "Launch Wizard";
		document.write("<tr>");
		var optsString = "";
		for(var ii=0;ii<optionsArray.length;ii++){
			optsString +="&"+optionsArray[ii]+"="+partsArray[i][ii];
			document.write("<td class='wizardLink'>"+unescape(partsArray[i][ii])+"</td>");
		}
		createLaunchRowWithAltOptions(wizard,false,true,true,optsString);
		document.write("</tr>");
	}
	document.write("</table></div></td></tr>");
}

function wizardLink2(customer, label, wizardID, codebase, opts) {
	var link   = "http://" + resolveServer() + "/" + codebase + "/WizardServlet?DumpWizardFromSession=true&customer=" +
	             customer + "&wizardID=" + wizardID + opts;
	return link;
}

function createLaunchRow(wizard,hasSubsections,firstRow,allowServerSelection) {
	createLaunchRowWithAltOptions(wizard,hasSubsections,firstRow,allowServerSelection,"")
}

function createLaunchRowWithAltOptions(wizard,hasSubsections,firstRow,allowServerSelection,altOptions) {
		if(hasSubsections){
			var colCount = 1;
		} else {
			var colCount = 2;
		}

		if(wizard.className == "Wizard"){
			if(allowServerSelection){
				myURL = "javascript:document.location.href = wizardLink2('" + wizard.customer + "', '" + wizard.label + "', '" + wizard.wizardID + "', '" + wizard.getCodebase() + "', '" + wizard.opts + altOptions + "');";
			} else {
				myURL = wizard.getURL() + altOptions;
			}
		}

		if(wizard.className == "Link"){
			myURL = wizard.getURL();
		}

		if(!firstRow){
			document.write("<tr>");
		}

		document.write("<td colspan='"+colCount+"' class='wizardLink' onclick=\"//document.location.href=myURL;\" onmouseover=\"this.className='wizardLinkOver';\" onmouseout=\"this.className='wizardLink';\"><span style='color:#999'>&raquo;</span> <a href=\""+myURL+"\" target=>"+wizard.label+"</a></td>");

		if(!firstRow){
			document.write("</tr>");
		}
}

function resolveServer() {
	if(typeof document.forms[0] == "undefined"){
		var server = resolveServerFromURL();
	} else if(typeof document.forms[0].server == "undefined"){
		var server = resolveServerFromURL();
	} else {
		var server = document.forms[0].server.options[document.forms[0].server.selectedIndex].value;
	}
	return server;
}

function resolveServerFromURL() {
	if(window.location.toString().indexOf("stage") != -1 || window.location.toString().indexOf("charlie") != -1) {
		return "stage";
	} else if(window.location.toString().indexOf("apps") != -1 || window.location.toString().indexOf("alpha") != -1 || window.location.toString().indexOf("bravo") != -1) {
		return "apps";
	} else if(window.location.toString().indexOf("webdc") != -1) {
		return "webdc";
	} else if(window.location.toString().indexOf("www") != -1) {
		return "webdc";
	} else {
		return "localhost";
	}
}

function createSelectServer(oneliner) {
document.write(""
	+"<tr>"
	+"	<td class='sectionHeader' "
	+ (oneliner == null ? "colspan='2'" : "")
	+ ">Server Selection:</td>"
	+ (oneliner == null ? "</tr>\n<tr>" : "")
	+ "<td class='wizardLink' "
	+ (oneliner == null ? "colspan='2'" : "")
	+ ">"
	+"		<select name='server'>"
	+"			<option value='apps.transim.com'   >apps.transim.com</option>"
	+"			<option value='stage.transim.com'  >stage.transim.com</option>"
	+"			<option value='webdc.transim.com'  >webdc.transim.com</option>"
	+"			<option value='localhost:"
	+(window.location.hostname == "localhost" ? window.location.port + (window.location.port == 8080 ? "/DesignCenter" : "") : "9090")
	+"'                  >localhost</option>"
	+"			<option value='alpha.transim.com'  >alpha.transim.com</option>"
	+"			<option value='bravo.transim.com'  >bravo.transim.com</option>"
	+"			<option value='charlie.transim.com'>charlie.transim.com</option>"
	+"			<option value='appserv-a.transim.com'>appserv-a.transim.com</option>"
	+"			<option value='appserv-b.transim.com'>appserv-b.transim.com</option>"
	+"		</select>"
	+"	</td>"
	+"</tr>");
	drawSpacerRow(10);
}

/****************************
* OLD SCHOOL WIZARD LAUNCHING
****************************/

function determineServer() {
	if(typeof document.forms[0] == "undefined"){
		var server = getServerFromURL();
	} else if(typeof document.forms[0].server == "undefined"){
		var server = getServerFromURL();
	} else {
		var server = document.forms[0].server.options[document.forms[0].server.selectedIndex].value;
	}
	return server;
}

function getServerFromURL() {
	if(window.location.toString().indexOf("stage") != -1 || window.location.toString().indexOf("charlie") != -1) {
		return "stage.transim.com/DesignCenter";
	} else if(window.location.toString().indexOf("apps") != -1 || window.location.toString().indexOf("alpha") != -1 || window.location.toString().indexOf("bravo") != -1) {
		return "apps.transim.com/DesignCenter";
	} else if(window.location.toString().indexOf("webdc") != -1) {
		return "webdc.transim.com/DesignCenter";
	} else {
		return "localhost:" + window.location.port + (window.location.port == 8080 ? "/DesignCenter" : "");
	}
}

function wizardLink(wizard,opts) {
	var link   = "http://" + determineServer() + "/servlet/WizardServlet?DumpWizardFromSession=true&customer=" +
	             customer + "&wizardID=" + wizard + opts;
	return link;
}
function selectServer() {
	var selectedIndex = 0;
	if(window.location.toString().indexOf("apps") != -1) {
		selectedIndex = 0;
	} else if(window.location.toString().indexOf("stage") != -1) {
		selectedIndex = 1;
	} else if(window.location.toString().indexOf("webdc") != -1) {
		selectedIndex = 2;
	} else if(window.location.toString().indexOf("localhost") != -1) {
		selectedIndex = 3;
	}
	document.forms[0].server.selectedIndex = selectedIndex;
}

function drawSelectServer(oneliner) {
document.write(""
	+"<tr>"
	+"	<td class='sectionHeader' "
	+ (oneliner == null ? "colspan='2'" : "")
	+ ">Server Selection:</td>"
	+ (oneliner == null ? "</tr>\n<tr>" : "")
	+ "<td class='wizardLink' "
	+ (oneliner == null ? "colspan='2'" : "")
	+ ">"
	+"		<select name='server'>"
	+"			<option value='apps.transim.com/DesignCenter'   >apps.transim.com</option>"
	+"			<option value='stage.transim.com/DesignCenter'  >stage.transim.com</option>"
	+"			<option value='webdc.transim.com/DesignCenter'  >webdc.transim.com</option>"
	+"			<option value='localhost:"
	+(window.location.hostname == "localhost" ? window.location.port + (window.location.port == 8080 ? "/DesignCenter" : "") : "9090")
	+"'                  >localhost</option>"
	+"			<option value='alpha.transim.com/DesignCenter'  >alpha.transim.com</option>"
	+"			<option value='bravo.transim.com/DesignCenter'  >bravo.transim.com</option>"
	+"			<option value='charlie.transim.com/DesignCenter'>charlie.transim.com</option>"
	+"			<option value='appserv-a.transim.com:8080/DesignCenter'>appserv-a.transim.com</option>"
	+"			<option value='appserv-b.transim.com:8080/DesignCenter'>appserv-b.transim.com</option>"
	+"		</select>"
	+"	</td>"
	+"</tr>");
	drawSpacerRow(10);
}

function drawWizardLinks(sectionTitle,wizards,listStyle) {
	if(listStyle == "wizards"){
		var subsectionPos = 3;
	} else if (listStyle == "links"){
		var subsectionPos = 2;
	}
	// draw section header
	if(sectionTitle !=""){
		drawSectionTitle(sectionTitle);
	}
	// are there subsections?
	var hasSubsections = getCols(wizards,subsectionPos);
	var lastSection
	//loop through wizard ids and create links
	for(var i = 0;i<wizards.length;i++){
		var firstRow = false;
		if(i == 0 || wizards[i][subsectionPos] != lastSection){
			var printSubSection = true;
			var subSectionRowCount = getRowCount(wizards,subsectionPos,wizards[i][subsectionPos],i)
		}
		if(hasSubsections && printSubSection){
			var printSubSection = false;
			var firstRow = true;
			document.write("<tr><td class='wizardLink' rowspan='"+subSectionRowCount+"'>"+wizards[i][subsectionPos]+"</td>");
		}
		var lastSection = wizards[i][subsectionPos];

		if(listStyle == "wizards"){
			createRow("javascript:window.location.href=wizardLink('"+wizards[i][1]+"','"+wizards[i][2]+"');",wizards[i][0],hasSubsections,firstRow);
		} else if (listStyle == "links"){
			createRow(wizards[i][1],wizards[i][0],hasSubsections,firstRow);
		}
	}
	drawSpacerRow(10);
}

function drawSpacerRow(height,columnWidth) {
	document.write(""
	+"<tr>"
	+"	<td colspan='2' style='height:"+height+"px;'></td>"
	+"</tr>");
}

function createRow(URL,label,hasSubsections,firstRow) {
		if(hasSubsections){
			var colCount = 1;
		} else {
			var colCount = 2;
		}
		if(!firstRow){
			document.write("<tr>");
		}
		document.write("<td colspan='"+colCount+"' class='wizardLink' onmouseover=\"this.className='wizardLinkOver';\" onmouseout=\"this.className='wizardLink';\"><span style='color:#999'>&raquo;</span> <a href=\""+URL+"\" target=>"+label+"</a></td>");

		if(!firstRow){
			document.write("</tr>");
		}
}

function drawSectionTitle(sectionTitle) {
	document.write(""
	+"<tr>"
	+"	<td colspan='2' class='sectionHeader'>"+sectionTitle+"</td>"
	+"</tr>");
}

function getCols(l,p) {
	for(var i = 0;i<l.length;i++){
		if(l[i][p] != ""){
			return true;
		}
	}
	return false;
}

function getRowCount(l,p,ss,si) {
	var count = 0;
	var found = false;
	for(var i = si;i<l.length;i++){
		if(l[i][p] == ss){
			found = true;
			count++;
		} else {
			if(found){
				 return count;
			}
		}
	}
	return count;
}

function drawPartsLinks(sectionTitle,subTitle,optionsArray,partsArray,globalOpts,wizardID,listHeight) {

	// draw section header
	if(sectionTitle !=""){
		drawSectionTitle(sectionTitle);
	}

	document.write(""
	+"<tr>"
	+"	<td class='wizardLink'>"+subTitle+"</td>"
	+"	<td class='wizardLink'>"
	+		"<div class='partsList' style='height:"+listHeight+"px;'>"
	+"			<table width='100%'>"
	+"				<tr>");

	for(var i=0;i<optionsArray.length;i++){
		document.write("<td class='wizardLinkHeader'>"+optionsArray[i]+"</td>");
	}

	document.write(""
	+"	<td class='wizardLinkHeader'>Action</td>"
	+"</tr>");
	for(var i=0;i<partsArray.length;i++){
		var label = "Launch Wizard";
		document.write("<tr>");
		var optsString = globalOpts;
		for(var ii=0;ii<optionsArray.length;ii++){
			optsString +="&"+optionsArray[ii]+"="+partsArray[i][ii];
			document.write("<td class='wizardLink'>"+unescape(partsArray[i][ii])+"</td>");
		}
		var URL ="javascript:window.location.href=wizardLink('"+wizardID+"','"+optsString+"');"
		createRow(URL,label,true,true)
		document.write("</tr>");
	}
	document.write("</table></div></td></tr>");
}
