/**Generated on 03-09-2010 - 4:06 pm**/ 
/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function drawSection(eid){
	sDivs = document.getElementById(eid).getElementsByTagName("div");
	cDivs = new Array();
	for(u=sDivs.length; u>0; u--){
		theDiv = sDivs.item(u-1);
		if(theDiv.getAttribute("name") == "content" || theDiv.getAttribute("name") == "ctTop"){
			//cDivs.push(sDivs.item(u).parentNode);
			dm = "";
			if(theDiv.parentNode.getElementsByTagName("div").item(0) == theDiv)dm="0px";
			encloseElement(theDiv, dm);
		}
	}
	if(document.getElementById("sideNav") != null){
		document.getElementById("sideNav").className = parseInt(document.getElementById("sideNav").offsetTop);
	}
	aTags = document.getElementById(eid).getElementsByTagName("a");
	for(i=0; i<aTags.length; i++){
		href = aTags.item(i).href;
		if(href.indexOf("#") == -1 && href.indexOf("perpetualtechnologies") > -1){
			aTags.item(i).onclick = clickHandler;
		}
	}
}

function getWindowSize(){
	var x = 0;
	var y = 0;
	if (self.innerHeight)
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return {"x":x,"y":y};
}

function makePop(url,w,title){
	var popup = document.createElement('div');
	popup.style.paddingTop = "0px";
	popup.style.position = "fixed";
	popup.id = "popup";
	popup.innerHTML = '<div class="content" name="content"><div class="content" name="content"><div class="closeBtn" style="float:right; height:20px; width:20px; position:relative; top:-4px; right:-4px;"><a href="#" onclick="closePop(); return false;">x</a></div><h2 style="margin:0px; padding:0px;">'+title+'</h2></div><div class="content" name="content"><div id="popContent"> '+popup.innerHTML+'</div></div></div>';
	document.body.appendChild(popup);
	drawSection("popup");
	fixPop(50,50);
	ajaxpage(url, "popContent");
	setTimeout(function(){
						fixPop(w,"");}
						,300);
}

function fixPop(popWidth,popHeight){
	var winSize = getWindowSize();
	var x = winSize.x;
	var y = winSize.y;
	var popup = document.getElementById("popup");
	popup.style.width = popWidth+'px';
	if(popHeight != ""){
		document.getElementById("popContent").style.height = popHeight+'px';
	}
	else{
		document.getElementById("popContent").style.height = "";
	}
	var eSize = getElementSize("popup");
	popup.style.left = ((x-eSize.w) / 2)+"px";
	popup.style.top = ((y-eSize.h) / 2)+"px";
}

function closePop(){
	popup = document.getElementById("popup");
	document.body.removeChild(popup);
}

function getElementSize(Elem) {
	//alert("Test..");
	if(document.getElementById){
		elem = document.getElementById(Elem);
	}
	else if (document.all){
		elem = document.all[Elem];
	}
	if (elem.style.pixelWidth){
		w=elem.style.pixelWidth;
		h=elem.style.pixelHeight;
		size = {"w":w,"h":h};
	}
	else{
		w=elem.offsetWidth;
		h=elem.offsetHeight;
		size = {"w":w,"h":h};
	}
	return size;
}
	
function getPage(url){
	newUrl = url.replace(".htm", "");
	lastChar = newUrl.slice(-1);
	if(lastChar == "/") newUrl+="index";
	ajaxpage("http://"+window.location.hostname+"/js/getTitle.php?url="+newUrl+".xml", "head");
	ajaxpage("http://"+window.location.hostname+"/classes/parsePage.php?s="+newUrl+".xml", "mainContent");
}

function checkUser(fld){
	user = fld.value;
	regMatch = /^([a-zA-Z0-9_-]+)$/.test(user);
	responseFld = document.getElementById("response-"+fld.getAttribute("name"));
	if(regMatch == false){
		responseFld.className = "error";
		responseFld.innerHTML = "Only alphameric characters are allowed";
	}
	else ajaxpage("/db/checkUser.php?user="+user,"response-"+fld.getAttribute("name"));
}

function checkPass(fld){
	pass = fld.value;
	regMatch = /^([a-zA-Z0-9_@-]+)$/.test(pass);
	responseFld = document.getElementById("response-"+fld.getAttribute("name"));
	if(regMatch == false){
		responseFld.className = "error";
		responseFld.innerHTML = "Only letters, numbers, -, @ and _ are allowed";
	}
	else{
		if(pass.length >= 6 && pass.length <= 10){
			responseFld.className = "valid";
			responseFld.innerHTML = "Password is valid";
		}
		else{
			responseFld.className = "error";
			responseFld.innerHTML = "Password must be between 6 and 10 characters";
		}
	}
}

function confPass(fld){
	pass = fld.value;
	daPass = document.getElementById("password").value;
	responseFld = document.getElementById("response-"+fld.getAttribute("name"));
	if(pass == daPass){
		responseFld.className = "valid";
		responseFld.innerHTML = "Password matches";
	}
	else{
		responseFld.className = "error";
		responseFld.innerHTML = "Must match password";
	}
}

function verifyEmail(input){
	var email = input.value;
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	responseFld = document.getElementById("response-email");
	if (!filter.test(email)) {
		responseFld.className = "error";
		responseFld.innerHTML = 'Please enter a valid email address';
	}
	else{
		responseFld.className = "valid";
		responseFld.innerHTML = 'Email is valid';
	}
}

function setPref(element,value){
	editCookie(element.getAttribute("name"),value);
	ajaxpage("/js/setPrefs.php?"+element.getAttribute("name")+"="+value,element.id);
	if(element.getAttribute("name") == "colorscheme"){
		swapColor(getCookie("colorscheme"));
	}
}

function verifyName(fld){
	fName = fld.value;
	regMatch = /^([a-zA-Z]+)$/.test(fName);
	responseFld = document.getElementById("response-"+fld.getAttribute("name"));
	if(regMatch == false){
		responseFld.className = "error";
		responseFld.innerHTML = "Only letters and numbers allowed";
	}
	else if(fName.length < 3){
		responseFld.className = "error";
		responseFld.innerHTML = "Name must be at least 3 characters";
	}
	else{
		responseFld.className = "valid";
		responseFld.innerHTML = "";
	}
}

function checkForm(daForm){
	inputs = daForm.getElementsByTagName("input");
	error = 0;
	for(i=0; i<inputs.length; i++){
		thisInput = inputs.item(i);
		iName = thisInput.getAttribute("name");
		rf = document.getElementById("response-"+iName);
		if(rf != null && rf.className == "error"){
			error+=1;
		}
	}
	if(error != 0){
		alert("Please fill in all fields with valid data");
		return false;
	}
	else{
		subForm(daForm);
		return false;
	}
}

function subForm(daForm){
	inputs = daForm.getElementsByTagName("input");
	qstr = "?";
	for(i=0; i<inputs.length; i++){
		thisInput = inputs.item(i);
		iName = thisInput.getAttribute("name");
		if(iName != null){
			qstr += iName+"="+thisInput.value+"&";
		}
	}
	qstr = qstr.substr(0,qstr.length - 1);
	ajaxpage(daForm.action+qstr, "formResponse");
}

function subFrm(daForm,ct){
	inputs = daForm.getElementsByTagName("input");
	qstr = "?";
	for(i=0; i<inputs.length; i++){
		thisInput = inputs.item(i);
		iName = thisInput.getAttribute("name");
		if(iName != null){
			qstr += iName+"="+thisInput.value+"&";
		}
	}
	qstr = qstr.substr(0,qstr.length - 1);
	//deleteCookie("colorscheme", "/");
	ajaxpage(daForm.action+qstr, "login");
	setTimeout(function(){
						user = getCookie("username");
						if(user != ""){
							getPage("http://"+window.location.hostname+"/users/myAccount.htm");
							ajaxpage("/sidebar.php?u="+user,"sidebar-content");
							swapColor(getCookie("colorscheme"));
							setTimeout(function(){
												fixSide();
												},300);
							
						}},300);
	return false;
}

function fixSide(){
	nh = parseInt(document.getElementById("ctSide").offsetHeight);
	if(parseInt(document.getElementById("sideContent").style.height) > 40){
		document.getElementById("sideContent").style.height = nh+"px";
	}
	else showNav();
}

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

function swapColor(color) {
	oldCss = document.getElementsByTagName("link")[0].getAttribute("href");
	oldColor = oldCss.replace("/css/pageStyles-","");
	oldColor = oldColor.replace(".css","");
	replacejscssfile(oldCss, 
					"/css/pageStyles-"+ color +".css", 
					"css");
	cImg = document.getElementById("logo").src;
	newLogo = cImg.replace(oldColor, color);
	document.getElementById("logo").src = newLogo;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function editCookie(c_name,val)
{
deleteCookie(c_name, "/");
Set_Cookie(c_name, val, 1, "/");
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

//expires (hours)
if ( expires )
{
expires = expires * 1000 * 60 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function deleteCookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function logout(){
	deleteCookie("username", "/");
	editCookie("colorscheme","blue");
	ajaxpage("/users/logout.php", "login");
	ajaxpage("/sidebarNull.php","sidebar-content");
	getPage("http://"+window.location.hostname+"/index.htm");
	swapColor(getCookie("colorscheme"));
	hideNav();
}

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
//if (bustcachevar) //if bust caching of external page
//bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
if(containerid != "formResponse"){
page_request.open('GET', url, true);
page_request.send(null);
}
else{
postData = url.split("?");
params = postData[1];
page_request.open('POST', postData[0], true);
page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
page_request.setRequestHeader("Content-length", params.length);
page_request.setRequestHeader("Connection", "close");
page_request.send(params);
}
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
	if(containerid == "head"){
		meta = page_request.responseText;
		replaceTitle(meta);
	}
	else{
		if(containerid.indexOf("pref_") > -1){
			document.getElementById(containerid).innerHTML=page_request.responseText;
		}
		else if(containerid.indexOf("response-") == -1){
			document.getElementById(containerid).innerHTML=page_request.responseText;
			drawSection(containerid);
		}
		else{
			pgVars = page_request.responseText.split("::::");
			valid = pgVars[0];
			resp = pgVars[1];
			document.getElementById(containerid).className=valid;
			document.getElementById(containerid).innerHTML=resp;
		}
	}
}
}

function replaceTitle(newTitle){
	document.getElementsByTagName("title").item(0).innerHTML = newTitle;
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}