var currentVideo;

/*
Prevent JS error from appearing on the client browser
*/
function stopErrors() {
	return true;
}
//window.onerror = stopErrors;

/*
Pre-load image functions
*/
function preloadBaseImages() {
	var imageArray = new Array('blank.gif', 
							   'progress_dots.gif');

    preloadImages(imageArray, '/images/');	
}


function preloadImages(imageArray, dirPrefix) {
	for(i = 0; i < imageArray.length; i++) {
		var imageName = new Image();
        imageName.src = dirPrefix + imageArray[i];
	}
}


/*
Replaces text with by in string
*/
function replace(string, text, by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


/*
Find an object on the page
*/
function findObj(objName) {
	var theObj = document.getElementById(objName);
	
	if (!theObj) {
		//theObj = document.all[objName];
	}
	
	return theObj;
}


/*
Find an object that is contained within an object
*/
function findContainedObj(objName, objContainer) {
	var theObj = objContainer.elements[objName];
	
	if ((!theObj) && (objContainer.all)) {
		theObj = objContainer.all[objName];
	}
	
	// If no object found, search through layers
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("div");
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
	
	// If we still haven't found the object, look for table cells
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("td");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
		
	// If we still haven't found the object, look for spans
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("span");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}	

	// If we still haven't found the object, look for tables
	if ((!theObj) && (objContainer.getElementsByTagName)) {
		var tmpObj = objContainer.getElementsByTagName("table");		
		for (var i = 0; i < tmpObj.length; i++) {
			if ((tmpObj[i].name == objName) || (tmpObj[i].id == objName)) {
				theObj = tmpObj[i];
			}
		}
	}
	
	return theObj;	
}


/*
Login the user
*/
function loginUser() {
	var tmpFrm = findObj('login_frm');
	
	doFormSubmit('Logging In...', tmpFrm);
	clearFormErrors(tmpFrm);
	
	performAjax(tmpFrm, '/index.php?action=XML_LOGIN', '', '');
}


/*
Show the update password table
*/
function showUpdatePassword() {
	var tmpLoginTable = findObj('login_table');
	var tmpPwTable = findObj('pw_update_table');
	var tmpPwElem = findObj('passwd1');
	var tmpLoginBtn = findObj('login_btn');
	var tmpUpdateBtn = findObj('update_btn');
	
	tmpLoginTable.style.display = 'none';
	tmpLoginBtn.style.display = 'none';
	tmpPwTable.style.display = 'block';
	tmpUpdateBtn.style.display = 'block';
	
	tmpPwElem.focus();
}


/*
Update the user's password
*/
function updateUserPassword() {
	var tmpFrm = findObj('login_frm');
	
	doFormSubmit('Updating Password...', tmpFrm);
	clearFormErrors(tmpFrm);
	
	performAjax(tmpFrm, '/index.php?action=XML_UPDATE_PASSWORD', '', '');
}


/*
Redirect the current window after a set time
*/
function redirectWindowTimeout(iLocation) {
	setTimeout('window.location.href=\'' + iLocation + '\'', 250);
}


/*
Switch the video player to the specified video
*/
function pickVideo(iId, iFile, iDisplay) {
	var tmpObj = findObj(currentVideo);
	tmpObj.className = 'videoDiv';
	
	var newObj = findObj(iId);
	newObj.className = 'videoActiveDiv';
	currentVideo = iId;	
	
	var tmpDisplay = findObj('current_movie');
	tmpDisplay.innerHTML = iDisplay;
	
	var s1 = new SWFObject("/swf/mediaplayer.swf", "mediaplayer", "480", "395", "7");
	s1.addParam("allowfullscreen", "true");
	
	s1.addVariable("width", "480");
	s1.addVariable("height", "395");
	
	s1.addVariable("file", iFile);
	s1.addVariable("image", "/images/help/das/admin.jpg");
	
	s1.write("video_div");
}