// JavaScript Document
var globalPageValue = gobalPageData  = Login = null;
var uploadFrame = document.getElementById('fileUpload');
var theProfList = nums = '';
if (!document.getElementById) {
	document.getElementById = function(strID) {
		if (document.layers) return document[strID];
		if (document.all) return document.all[strID];
		return null; // expect the unexpected
	}
}
/*window.dhtmlHistory.create()
window.onload = initialize;
    
function initialize() {
	// initialize the DHTML History
	// framework
	dhtmlHistory.initialize();
	
	// subscribe to DHTML history change
	// events
	dhtmlHistory.addListener(historyChange);
	  
	// if this is the first time we have
	// loaded the page...
	if (dhtmlHistory.isFirstLoad()) {
		dhtmlHistory.add("home", 	 null);
	}
	  page = getAnchorValue();
	  (page)? LoadPage(page) : LoadPage('home');
}
function historyChange(newLocation, 
                       historyData) {
	LoadPage(newLocation);
	gobalPageData = historyData;
  //debug("A history change has occurred: "
   //     + "newLocation="+newLocation
    //    + ", historyData="+historyData, 
       // true);
}*/

function getAnchorValue() {
	/*var url = document.location;
	if(url.toString().indexOf('#') == '-1') return null;
	var strippedUrl = url.toString().split("#");
	if((strippedUrl[1]))
	return strippedUrl[1];*/
	
	//going to rework this to take the file name
	url = document.location;
	var splitURLs = url.toString().split('/');
	var pageParts = splitURLs[splitURLs.length - 1].toString().split('.');
	return (pageParts[0] == 'index')? 'home':pageParts[0];
}
function geFolderValue() {
	/*var url = document.location;
	if(url.toString().indexOf('#') == '-1') return null;
	var strippedUrl = url.toString().split("#");
	if((strippedUrl[1]))
	return strippedUrl[1];*/
	
	//going to rework this to take the file name
	url = document.location;
	var splitURLs = url.toString().split('.');
	var num = splitURLs.length - 1;
	var path = '';
	var pageParts = splitURLs[num].toString().split('/');
	for(var i = 1; i < num; i++) {
		path += '../';
	}
	return path;
}
globalPageValue = page = getAnchorValue();
if(!FlashDetect.versionAtLeast(10)) {
	alert("Adobe flash player needs to be updated!");
}
function getElementByClass(cName,tag,p){ 
	if(!cName) 	return false;  
	
	tag = (tag)? tag : "*" ; 
	p = (p)? p : document;
	
	var i; 
	var cRegEx=new RegExp("(^¦\\s)"+cName+"(\\s¦$)"); 
	var ra=[]; 
	var a=p.getElementsByTagName(tag); 
	
	for(i=0;i<a.length;i++){ 
		if(cRegEx.test(a[i].className)) { 
			ra.push(a[i]); 
		}
	} 
	return ra; 
} 
function createXmlHttpRequestObject()  
{ 
	// will store the reference to the XMLHttpRequest object 
	var xmlHttp; 
	// this should work for all browsers except IE6 and older 
	try 
	{ 
		// try to create XMLHttpRequest object 
		xmlHttp = new XMLHttpRequest(); 
	} 
	catch(e) 
	{ 
		// assume IE6 or older 
		var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0', 
										'MSXML2.XMLHTTP.5.0', 
										'MSXML2.XMLHTTP.4.0', 
										'MSXML2.XMLHTTP.3.0', 
										'MSXML2.XMLHTTP', 
										'Microsoft.XMLHTTP'); 
		 // try every prog id until one works 
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)  
		{ 
			try
			{  
			// try to create XMLHttpRequest object 
			xmlHttp = new ActiveXObject(XmlHttpVersions[i]); 
			}  
			catch (e) {} 
		} 
	} 
	// return the created object or display an error message 
	if (!xmlHttp) 
		alert("Error creating the XMLHttpRequest object."); 
	else  
		return xmlHttp; 
}

//create a xml file
function createDomObject() 
{ 
	// will store reference to the DOM object  
	var xmlDoc; 
	// create XML document   
	if (document.implementation && document.implementation.createDocument) 
	{ 
		xmlDoc = document.implementation.createDocument("", "", null); 
	} 
	// works for Internet Explorer 
	else if (window.ActiveXObject) 
	{ 
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
	} 
	// returns the created object or displays an error message 
	if (!xmlDoc) 
		alert("Error creating the DOM object."); 
	else  
		return xmlDoc;    
} 

//make sure it was successful
function handleServerResponse() 
{ 
	
	// retrieve the server's response packaged as an XML DOM object 
	var xmlResponse = xmlHttp.responseXML; 
	// catching potential errors with IE and Opera 
	if (!xmlResponse || !xmlResponse.documentElement) 
		alert("Invalid XML structure:\n" + xmlHttp.responseText); 
	// catching potential errors with Firefox 
	var rootNodeName = xmlResponse.documentElement.nodeName; 
	if (rootNodeName == "parsererror")  
		alert("Invalid XML structure:\n" + xmlHttp.responseText); 
	// getting the root element (the document element) 
	xmlRoot = xmlResponse.documentElement; 
	// testing that we received the XML document we expect 
	if (rootNodeName != "response" || !xmlRoot.firstChild) 
		alert("Invalid XML structure:\n" + xmlHttp.responseText); 
	// the value we need to display is the child of the root <response> 
	
}
var memberData = null;
function login(name, pass) {
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 if(xmlHttp.responseXML) {
			memberData = xmlHttp.responseXML;
			aValue = getAnchorValue();
			page = (aValue)? aValue : 'home';
			createUserFloat();
			window.location.reload();
			return;
		} else if(xmlHttp.responseText) {
			document.getElementById('loginErrorMsg').innerHTML = xmlHttp.responseText;
			return;
		}	}
	var name = document.getElementById("clientName").value;
	var password = document.getElementById("clientPassword").value;
	xmlHttp.open("GET", "backbone/login.php?name="+name+"&password="+password, true);
	xmlHttp.send(null);	
	
}
function createUserFloat() {
	var xmlHttp = createXmlHttpRequestObject(); 
	userFloat = new customPrompt('alert', document.getElementById('userFloater'), document.getElementById('userFloater_front'));
	userFloat.setTitle('Your personal links');
	
	userFloat.valign = 'top';
	userFloat.halign = 'right';
	userFloat.doNotShowButton = true;
	userFloat.setFadeTime(150);
	userFloat.setFadeColor('#000');
	
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 	if(xmlHttp.responseText) {
				userFloat.setMsg(xmlHttp.responseText);
				userFloat.init();
				return;
			}
		}	
	xmlHttp.open("GET", "backbone/userPanel.php?build=true", true);
	xmlHttp.send(null);
	
	
}
function addNewUser() {
	addHtml = '	<table border="0" cellspacing="0" cellpadding="0">'
+ '  <tr>'
+ '    <td width="83">Name</td>'
+ '    <td><input type="text" name="name" id="newClientName" /></td>'
+ '  </tr>'
+ '  <tr>'
+ '    <td width="83">Login</td>'
+ '    <td><input type="text" name="login" id="newClientLogin" /></td>'
+ '  </tr>'
+ '  <tr>'
+ '    <td>Email</td>'
+ '    <td><input type="text" name="email" id="newClientEmail" /></td>'
+ '  </tr>'
+ '  <tr>'
+ '    <td><input name="cancel" value="Cancel" type="button" onclick="profilePrompt.dir=1;profilePrompt.timeCount=0;profilePrompt.fade()" /></td>'
+ '    <td><input name="add" value="Add" type="submit" onclick="addingClient()" /></td>'
+ '  </tr>'
+ '</table>';
	profilePrompt = new customPrompt('alert', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
	profilePrompt.doNotShowButton = true;
	profilePrompt.setFadeTime(125);
	profilePrompt.setMsg(addHtml);
	profilePrompt.setOpacity(0.75);
	profilePrompt.setTitle('Add Client');
	profilePrompt.init();
}
function addingClient() {
	//show a loader here
	var email = document.getElementById('newClientEmail').value;
	var name = document.getElementById('newClientName').value;
	var login = document.getElementById('newClientLogin').value;
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 //if(xmlHttp.responseText) { 
			addingPrompt.resetFade(1);
			addingPrompt.fade();
			addingPrompt = null;
			if(xmlHttp.responseText) {
				msg = xmlHttp.responseText;				
			} else {
				msg = 'Client Added Successfully!';
				createUserFloat();
			}
			uploadMsg = '<table cellpadding="1" cellspacing="0" border="0px"><tr><td>'+msg+'</td></tr>'
					+' <tr><td><input type="button" name="crop" value="Done" onclick="donePrompt.resetFade(1);donePrompt.fade();" /></td></table>';  
			donePrompt = new customPrompt('default', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			donePrompt.doNotShowButton = true;
			donePrompt.setFadeTime(125);
			donePrompt.setInnerHTML(uploadMsg);
			donePrompt.setTitle(null);
			donePrompt.init();
		//}
	}
	xmlHttp.open('GET','backbone/userPanel.php?addProfile=true&name='+name+'&email='+email+'&login='+login,true);
	xmlHttp.send(null);
	profilePrompt.resetFade(1);
	profilePrompt.fade();
	profilePrompt = null;
	addingPrompt = new customPrompt('default', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	addingPrompt.doNotShowButton = true;
	addingPrompt.setFadeTime(125);
	addingPrompt.setInnerHTML('<p><h3>Please wait while the client is added!</h3></p>');
	addingPrompt.setTitle(null);
	addingPrompt.init();
	
}
function manageProfileAccess(userID) {
	var suggestCxn = createXmlHttpRequestObject();
	var theURL = 'backbone/userPanel.php?showProfileList='+userID;
	
	suggestCxn.onreadystatechange = function() {
		if(suggestCxn.readyState != 4) return;
		if(suggestCxn.responseXML) {
			//don't do anything			
		} else if(suggestCxn.responseText) {
			//create an prompt showing them
			promptFront = document.getElementById('dimmer2Front');
			promptBack = document.getElementById('dimmer2Back');
			promptFront.style.height = 350 + 'px';
			promptBack.style.height = 350 + 'px';
			promptFront.style.width = 400 + 'px';
			promptBack.style.width = 400 + 'px';
			promptFront.style.overflow = 'auto';
			mPA = new customPrompt('custom', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			mPA.setInnerHTML(suggestCxn.responseText);
			mPA.setFadeTime(150);
			mPA.setFadeColor('#000');
			mPA.init();
		}
	}
	suggestCxn.open("GET", theURL, true);
	suggestCxn.send(null);
}
function listToggle(opt) {
	theProfList += [opt.value] + '=>' + opt.checked + ',';
}
function updateAccess(userID) {
	//show a loader here
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 //if(xmlHttp.responseText) { 
			addingPrompt.resetFade(1);
			addingPrompt.fade();
			addingPrompt = null;
			if(xmlHttp.responseText) {
				msg = xmlHttp.responseText;				
			} else {
				msg = 'Updated Successfully!';
			}
			uploadMsg = '<table cellpadding="1" cellspacing="0" border="0px"><tr><td>'+msg+'</td></tr>'
					+' <tr><td><input type="button" name="crop" value="Done" onclick="donePrompt.resetFade(1);donePrompt.fade();" /></td></table>'; 
			promptFront = document.getElementById('dimmer2Front');
			promptBack = document.getElementById('dimmer2Back');
			promptFront.style.height = null;
			promptBack.style.height = null;
			promptFront.style.width = null;
			promptBack.style.width = null;
			promptFront.style.overflow = null;
			donePrompt = new customPrompt('default', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			donePrompt.doNotShowButton = true;
			donePrompt.setFadeTime(125);
			donePrompt.setInnerHTML(uploadMsg);
			donePrompt.setTitle(null);
			donePrompt.init();
			theProfList = '';
		//}
	}
	xmlHttp.open('GET','backbone/userPanel.php?updatePriv='+theProfList+'&id='+userID,true);
	xmlHttp.send(null);
	addingPrompt = new customPrompt('default', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	addingPrompt.doNotShowButton = true;
	addingPrompt.setFadeTime(125);
	addingPrompt.setInnerHTML('<p><h3>Please wait while the client privileges are updated!</h3></p>');
	addingPrompt.setTitle(null);
	addingPrompt.init();
}
function removeUser(userID) {
	//show a loader here
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 //if(xmlHttp.responseText) { 
			addingPrompt.resetFade(1);
			addingPrompt.fade();
			addingPrompt = null;
			if(xmlHttp.responseText) {
				msg = xmlHttp.responseText;				
			} else {
				msg = 'Client Removed Successfully!';
				createUserFloat();
			}
			uploadMsg = '<table cellpadding="1" cellspacing="0" border="0px"><tr><td>'+msg+'</td></tr>'
					+' <tr><td><input type="button" name="crop" value="Done" onclick="donePrompt.resetFade(1);donePrompt.fade();" /></td></table>';  
			donePrompt = new customPrompt('default', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			donePrompt.doNotShowButton = true;
			donePrompt.setFadeTime(125);
			donePrompt.setInnerHTML(uploadMsg);
			donePrompt.setTitle(null);
			donePrompt.init();
		//}
	}
	xmlHttp.open('GET','backbone/userPanel.php?removeProfile=true&id='+userID,true);
	xmlHttp.send(null);
	addingPrompt = new customPrompt('default', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	addingPrompt.doNotShowButton = true;
	addingPrompt.setFadeTime(125);
	addingPrompt.setInnerHTML('<p><h3>Please wait while the client is removed!</h3></p>');
	addingPrompt.setTitle(null);
	addingPrompt.init();
}
function promoteUser(userID) {
	//show a loader here
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 //if(xmlHttp.responseText) { 
			addingPrompt.resetFade(1);
			addingPrompt.fade();
			addingPrompt = null;
			if(xmlHttp.responseText) {
				msg = xmlHttp.responseText;				
			} else {
				msg = 'Client Promoted Successfully!';
				createUserFloat();
			}
			uploadMsg = '<table cellpadding="1" cellspacing="0" border="0px"><tr><td>'+msg+'</td></tr>'
					+' <tr><td><input type="button" name="crop" value="Done" onclick="donePrompt.resetFade(1);donePrompt.fade();" /></td></table>';  
			donePrompt = new customPrompt('default', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
			donePrompt.doNotShowButton = true;
			donePrompt.setFadeTime(125);
			donePrompt.setInnerHTML(uploadMsg);
			donePrompt.setTitle(null);
			donePrompt.init();
		//}
	}
	xmlHttp.open('GET','backbone/userPanel.php?promoteUser=true&id='+userID,true);
	xmlHttp.send(null);
	addingPrompt = new customPrompt('default', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	addingPrompt.doNotShowButton = true;
	addingPrompt.setFadeTime(125);
	addingPrompt.setInnerHTML('<p><h3>Please wait while the client is promoted!</h3></p>');
	addingPrompt.setOpacity(0.75);
	addingPrompt.setTitle(null);
	addingPrompt.init();
}
function loginFloat() {
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 if(xmlHttp.responseXML) {
			memberData = xmlHttp.responseXML;
			aValue = getAnchorValue();
			page = (aValue)? aValue : 'home';
			Login.dir = 1;
			Login.timeCount = 0;
			Login.fade();
			createUserFloat();
			window.location.reload();
			return;
		} else if(xmlHttp.responseText) {
			document.getElementById('loginErrorMsg').innerHTML = xmlHttp.responseText;
			return;
		}	}
	var name = document.getElementById("clientNameF").value;
	var password = document.getElementById("clientPasswordF").value;
	xmlHttp.open("GET", "backbone/login.php?name="+name+"&password="+password, true);
	xmlHttp.send(null);
}
/*
function loginFloat() {
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 if(xmlHttp.responseXML) {
			memberData = xmlHttp.responseXML;
			aValue = getAnchorValue();
			page = (aValue)? aValue : 'home';
			Login.dir = 1;
			Login.timeCount = 0;
			Login.fade();
			LoadPage(page);

			return;
		} else if(xmlHttp.responseText) {
			document.getElementById('loginErrorMsg').innerHTML = xmlHttp.responseText;
			return;
		}	}
	var name = document.getElementById("clientNameF").value;
	var password = document.getElementById("clientPasswordF").value;
	xmlHttp.open("GET", "backbone/login.php?name="+name+"&password="+password, true);
	xmlHttp.send(null);
}
function LoadPage(page) {
	loading = new customPrompt('alert', document.getElementById('dimmerFront'), document.getElementById('dimmerBack'));
	var xmlHttp = createXmlHttpRequestObject();  
	var loc = 'pages/' + page + '.php';
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 if(xmlHttp.responseText) {
			 document.getElementById('pageBody').innerHTML = xmlHttp.responseText;
			 /*loading.dir = 1;
			 loading.timeCount = 0;
			 loading.fade();*//*
			 loading = null;
			 globalPageValue =  page;
			 if(page == 'talent') {
				 profile('*');
			 }
			 return;
		} 
	}
	loading.doNotShowButton = true;
	loading.setFadeTime(125);
	loading.setMsg('Please wait while the page load!');
	loading.setOpacity(0.75);
	loading.setTitle(' ');
	//loading.init();
	xmlHttp.open("GET", loc, true);
	xmlHttp.send(null);
}*/
function clearLoginFloat() {
			Login.dir = 1;
			Login.timeCount = 0;
			Login.fade();
}
function goToPage(page) {
	dhtmlHistory.add(page, 	 null);
	LoadPage(page);
}
function updateDynParts(type, tag, p) {
	if(!memberData) return false;
	if(type) {
		toUpdate = getElementByClass(type,tag,p);
		for(var i = 0; i < toUpdate.length; i++) {
			toUpdate[i].innerHTML = memberData.getElementByTagName(type).firstChild.nodeValue;
		}
	} else {
		var allDyn = new Array('clientName','clientEmail','talentList');
		for(var j = 0; j < allDyn.length; j++) {
			toUpdate = getElementByClass(allDyn[j],tag,p);
			for(var i = 0; i < toUpdate.length; i++) {
				toUpdate[i].innerHTML = memberData.getElementByTagName(type).firstChild.nodeValue;
			}
		}
	}
}
function show(what) {
	alert(what);
	document.write(memberData.getElementByTagName(what).firstChild.nodeValue);
}
function showLogin() {
	if(memberData) { 
		Login = new customPrompt('alert', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
		Login.doNotShowButton = true;
		Login.setFadeTime(750);
		Login.setLifeTime(1000);
		Login.setMsg('You are already logged in!');
		Login.setOpacity(0.65);
		Login.setTitle('');
		Login.init();
		return false;
	}
	Login = new customPrompt('alert', document.getElementById('dimmer2Front'), document.getElementById('dimmer2Back'));
	Login.doNotShowButton = true;
	Login.setFadeTime(225);
	Login.setMsg('<table width="300" border="0" cellpadding="0" cellspacing="0">'
 +'                 <tr>'
 +'                   <td width="77">Client Name</td>'
 +'                   <td width="213"><div align="center">'
 +'                     <input type="text" name="clientNameF" id="clientNameF">'
 +'                   </div></td>'
 +'                 </tr>'
 +'                 <tr>'
 +'                   <td>Password</td>'
 +'                   <td><div align="center">'
 +'                     <input type="password" name="clientPasswordF" id="clientPasswordF">'
 +'                   </div></td>'
 +'                 </tr>'
 +'                 <tr>'
 +'                   <td colspan="2"><div align="center">'
 +'                     <input type="button" name="Login" id="Login" value="Login" onClick="loginFloat()">'
 +'                     <input type="button" name="Cancel" id="Cancel" value="Cancel" onClick="clearLoginFloat()">'
 +'                   </div></td>'
 +'                   </tr>'
 +'               </table>'
 +'                 <strong><p id="loginErrorMsg" align="center">&nbsp;</p></strong>'
 +'                   <p><br>'
 +'                   </p>');
	Login.setOpacity(0.75);
	Login.setTitle('Client Login');
	Login.init();
}
function getMemberData() {
	var xmlHttp = createXmlHttpRequestObject();  
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState != 4) {return false;}
		 if(xmlHttp.responseXML) {
			memberData = xmlHttp.responseXML;
			return;
		} else {
			aValue = getAnchorValue();
			page = (aValue)? aValue : 'home';
			//LoadPage(page);
		}
	}
	xmlHttp.open("GET", "backbone/login.php?action=getMemberData", true);
	xmlHttp.send(null);
}

