function validateLogin(){
	var sError="";
	var txtEmailAddress = document.getElementById("txtEmailAddress");
	var txtPassword = document.getElementById("txtPassword");
	
// Check Values
	
	if(!txtEmailAddress.value){
		sError += "Email Address is Mandatory\n\n";
	}else if(!isEmail(txtEmailAddress.value)){
		sError += "Email Address is Invalid\n\n";
	}
	
	if(!isValidPassword(txtPassword.value)){
		sError += "Password is Invalid\n\n";
	}
	
// Return Error
	
	if(sError){
		alert(errorMessage(sError));
		return false;
	}else{
		return true;
	}	
}

function validateForget(){
	var sError="";
	var txtEmailAddress = document.getElementById("txtEmailAddress");
	
// Check Values
	
	if(!txtEmailAddress.value){
		sError += "Email Address is Mandatory\n\n";
	}else if(!isEmail(txtEmailAddress.value)){
		sError += "Email Address is Invalid\n\n";
	}
	
// Return Error
	
	if(sError){
		alert(errorMessage(sError));
		return false;
	}else{
		return true;
	}	
}

function validateUser(form){
	var sError="";
	var txtFirstName = document.getElementById("txtFirstName");
	var txtSurname = document.getElementById("txtSurname");
	var txtEmailAddress = document.getElementById("txtEmailAddress");
	var txtDescription = document.getElementById("txtDescription");
	
// Check Values
	
	if(!txtFirstName.value){
		sError += "First Name is Required\n\n";
	}

	if(!txtSurname.value){
		sError += "Surname is Required\n\n";
	}

	if(!txtEmailAddress.value){
		sError += "Email Address is Required\n\n";
	}else if(!isEmail(txtEmailAddress.value)){
		sError += "Email Address is Invalid\n\n";
	}
	
	if(!validateRadio(form, "iPrivilege")){
		sError += "Privilege is Required\n\n";
	}
	
// Return Error
	
	if(sError){
		alert(errorMessage(sError));
		return false;
	}else{
		return true;
	}	
}

function validateCategory(){
	var sError="";
	var txtCategoryName = document.getElementById("txtCategoryName");
	
// Check Values
	
	if(!txtCategoryName.value){
		sError += "Category Name is Required\n\n";
	}
	
// Return Error
	
	if(sError){
		alert(errorMessage(sError));
		return false;
	}else{
		return true;
	}	
}

function validateDocument(){
	var sError="";
	var txtDocumentTitle = document.getElementById("txtDocumentTitle");
	var flUpload = document.getElementById("flUpload");
	var iHasFile = document.getElementById("iHasFile");
	
// Check Values
	
	if(!txtDocumentTitle.value){
		sError += "Document Title is Required\n\n";
	}
	
	if(!iHasFile.value){
		if(!flUpload.value){
			sError += "Document File is Required\n\n";
		}
	}
	
// Return Error
	
	if(sError){
		alert(errorMessage(sError));
		return false;
	}else{
		return true;
	}		
	
}

function actionUser(action, prompt){
	
	var sURL = "";
	var sResponse = "";
	
// check for prompt on delete
	
	if(prompt){
		sResponse = confirm("Are you sure you want to delete this user?");
		
		if(!sResponse){
			return false;
		}
		
	}
	
// check for single radio button

	if(typeof(document.edit_users.edit_user.length) == "undefined"){
		sURL = action + document.edit_users.edit_user.value;
		document.location.href = sURL;		
	}
	
// loop radio buttons
	
	for (var i=0; i < document.edit_users.edit_user.length; i++){
		
		if(document.edit_users.edit_user[i].checked){
			sURL = action + document.edit_users.edit_user[i].value;
			document.location.href = sURL;
		}
		
	}


}

function actionCategory(action, prompt){
	
	var sURL = "";
	var sResponse = "";
	var iId = "";
	
// Get Elements

	var oTable = document.edit_category.getElementsByTagName("table")[2];
	var oChecks = oTable.getElementsByTagName("input")
	
	for (var i=0; i < oChecks.length; i++){
		
		if(oChecks[i].id == "cat"){
			if(oChecks[i].checked){
				iId = oChecks[i].value;
			}
		}
	}

// Is an options selected

	if(iId){
		
	// Do we need to prompt for delete
	
		if(prompt){
			sResponse = confirm("Are you sure you want to delete this category?");
		}
		
		if((prompt && sResponse) || !prompt){
			sURL = action + iId;
			document.location.href = sURL;
		}
	}

}

function actionDocument(action, prompt){
	
	var sURL = "";
	var sResponse = "";
	var iId = "";
	
// Get Elements

	var oTable = document.edit_category.getElementsByTagName("table")[2];
	var oChecks = oTable.getElementsByTagName("input")
	
	for (var i=0; i < oChecks.length; i++){
		if(oChecks[i].id == "doc"){
			if(oChecks[i].checked){
				iId = oChecks[i].value;
			}
		}
	}

// Is an options selected

	if(iId){
		
	// Do we need to prompt for delete
	
		if(prompt){
			sResponse = confirm("Are you sure you want to delete this document?");
		}
		
		if((prompt && sResponse) || !prompt){
			sURL = action + iId;
			document.location.href = sURL;
		}
	}

}


function roll_over(obj, cls){
	
	if(obj.className != "active_row"){
		obj.className = cls;	
	}
	
}

function set_active_row(obj, id){
	
	var oTable = document.edit_category.getElementsByTagName("table")[2];
	var oTr = "";
	var iCounter = 0;
// Set String
	var sType = obj.id.substr(0, 3);
// Set object type
	eval("var oType = document.edit_category."+sType+";");
	
	//alert(oType)
// Is this already an active row

	if(obj.className == "active_row"){
		
		if(typeof(oType.length) == "undefined"){
			
			oType.checked = false;
			
		}else{
			
		// Loop through checkboxes, unchecking all
		
			for (var i=0; i < oType.length; i++){
				oType[i].checked = false;	
			}			
			
		}
		
	// Set class name back to org
	
		obj.className = "alt";
		
	}else{

		if(typeof(oType.length) == "undefined"){
			
			oType.checked = true;
			
		}else{	
			
		// Loop through checkboxes, if value then check if not then uncheck
		
			for (var i=0; i < oType.length; i++){
				
				if(oType[i].value == id){
					oType[i].checked = true;
				}else{
					oType[i].checked = false;	
				}
				
			}		
		
		}
		
	// Set class

		obj.className = "active_row";			
		
	}
	
// reset table rows to non-active color
	
	for (var i=0; i < oTable.getElementsByTagName("tr").length; i++){
		oTr = oTable.getElementsByTagName("tr")[i];
		var str=oTr.id;
		if(str.substr(0, 4) == 'docs' || str.substr(0, 4) == 'cats'){
			if(str != obj.id){				// prevents the row selected (via about script) from being unhighlighted
				if(iCounter % 2 == 0){
					oTr.className = "alt";
				}else{
					oTr.className = "normal";
				}
			}
			iCounter++;
		}
	}	

}

function enter_category(id){
	if(id){
		document.location.href="index.php?c="+id;
	}
}

function enter_document(id){
	if(id){
		document.location.href="view-document.php?id="+id;
	}	
}

function enter_user(id){
	if(id){
		document.location.href="view-user.php?id="+id;
	}	
}

function confirm_delete(type, url){
	sResponse = confirm("Are you sure you want to delete this "+type+"?");
	if(sResponse){
		document.location.href = url;
	}
}

function confirm_reset(url){
	sResponse = confirm("Are you sure you want to reset this user's password?\nDoing so will also send an email to the user with the new password");
	if(sResponse){
		document.location.href = url;
	}	
}
//************ FUNCTIONS ***************//

function validateRadio(form, name){

	for(var i=0; i<form.eval(name).length; i++){
		if(form.eval(name)[i].checked==true){		
			return true;
		}
		
	}
	
	return false;
	
}

function isValidPassword(pString){
	if(pString.length > 10){
		return false;
	}else if(pString.length < 8){
		return false;
	}else{
		return true;
	}
}

function errorMessage(pString){
	var sMsg="The form cannot be submited because the following fields are either mandatory or invalid\n\n";
	sMsg+=pString;
	sMsg+="\n\nPlease correct the above fields and re-submitted the form";
	return sMsg;
}

function isEmail(pString){
	var sCrit = /^.+@.+\..{2,3}$/;
	if(!pString.match(sCrit)){
		return false;	
	}else{
		return true;
	}
}

