function checkFileRename(x)
{
	var reason = "";
	var fileVal=x.new_filename.value;

	reason+=validate(x.new_filename);

	// validate filename
	validFile = /^[^_]+[a-z0-9_.\-\s]{2,}$/i;
	if(fileVal.search(validFile) == -1)
	{
		alert("Invalid filename! Filename must start with a letter or number and must be at least 3 characters long. Special characters are not allowed.");
		return false;
	}

	if(reason != "")
	{
		alert("Please put in a valid filename to rename the file!");
		return false;
	}
}

function chkNewAlbum(theForm)
{
	var reason = "";
	var fName = theForm.folder_name.value;
	reason += validate(theForm.folder_name);

	if(reason != "")
	{
		alert("Please put in a valid album or folder name!");
		return false;
	}
} // end

function chkRegister(x)
{
	var reason = "";
	var userVal=x.username.value;
	var passVal=x.password.value;
	
	reason += validate(x.username);
	reason += validate(x.password);
	reason += validate(x.re_password);
	reason += validateEmail(x.email);

	if(x.username.value.length <= 2)
	{
		alert("Username must be at least 3 characters long!");
		return false;
	}
	
	// validate username
	validUser = /^[^_]+[a-z0-9_]{1,}$/i;
	if(userVal.search(validUser) == -1)
	{
		alert("Invalid username! Username must start with a letter or number and must be at least 3 characters long. No spaces, special characters or symbols.");
		return false;
	}
	
	// validate password
	validPass = /^[a-z0-9]{1,}$/i;
	if(passVal.search(validPass) == -1 || x.password.value.length <= 4)
	{
		alert("Invalid password! Password must be at least 5 characters in length, composed of letters and numbers only.");
		return false;
	}

	if(x.password.value != x.re_password.value)
	{
		alert("Passwords do not match!");
		return false;
	}

	if(reason != "")
	{
		alert("Please fill out the registration form correctly!");
		return false;
	}

} // end

function loginCheck(theForm)
{
	var reason = "";
	reason += validate(theForm.username);
	reason += validate(theForm.password);

	if(reason != "")
	{
		alert("Please login with a valid username and password!");
		return false;
	}
} // end

function chkPassCh(theForm)
{
	var reason = "";
	var passVal = theForm.password.value;
	var new_passVal = theForm.new_pass.value;
	reason += validate(theForm.password);
	reason += validate(theForm.new_pass);
	reason += validate(theForm.new_pass_confirm);
	

	// validate password
	validPass = /^[a-z0-9]{1,}$/i;
	if(passVal.search(validPass) == -1 || new_passVal.search(validPass) == -1 || new_passVal.length <= 4)
	{
		alert("Invalid password! Password must be at least 5 characters in length, composed of letters and numbers only.");
		return false;
	}

	if(reason != "")
	{
		alert("Please fill out the required fields!");
		return false;
	}

	if(theForm.new_pass_confirm.value != theForm.new_pass.value)
	{
		alert("New passwords do not match!");
		return false;
	}
	
} // end

function chkEmailCh(theForm)
{
	var reason = "";
	var passVal = theForm.password.value;
	reason += validate(theForm.password);
	reason += validateEmail(theForm.new_email);
	reason += validateEmail(theForm.new_email_verify);

	if(reason != "")
	{
		alert("Please fill out the required fields correctly!");
		return false;
	}


	// validate password
	validPass = /^[a-z0-9]{1,}$/i;
	if(passVal.search(validPass) == -1 || x.password.value.length <= 4)
	{
		alert("Invalid password format!");
		return false;
	}

	if(theForm.new_email.value != theForm.new_email_verify.value)
	{
		alert("Please verify your new email address!");
		return false;
	}

} // end

function chkContact(theForm)
{
	var reason = "";
	var nameVal = theForm.name.value;
	reason += validate(theForm.name);
	reason += validateEmail(theForm.email);
	
	if(reason != "")
	{
		alert("Please fill out the required fields correctly!");
		return false;
	}
} // end

function chkEmail(x)
{
	var reason ="";
	reason += validateEmail(x.email);
	if(reason != "")
	{
		alert("Invalid email address!");
		return false;
	}
} // end

function validate(fld)
{
	var error = "";

	if(fld.value.length == 0 || fld.value == " ")
	{
		error = "Please fill out the required fields!";
	}
	return error;
} //end


function validateEmail(fld)
{
	var error = "";

	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmailA = fld.value;
	 //check if email is valid
	if(strEmailA.search(validRegExp) == -1) 
	{
		error = "Please enter a valid email!";
	}
	return error;
} // end

function showFolders()
{
	hidePerms();
	changecss('#list_folders','visibility','visible');
}
function hideFolders()
{
	changecss('#list_folders','visibility','hidden');
}
function showPerms()
{
	hideFolders();
	changecss('#list_permissions','visibility','visible');
	changecss('#list_permissions','margin-left','-105px');
}
function hidePerms()
{
	hideFolders();
	changecss('#list_permissions','visibility','hidden');
}
function hideBoth()
{
	hideFolders();
	hidePerms();
}

function confirmMultiDelete(x)
{
	var process = x.process.value;
	if(process == "1")
	{ 
		var y = window.confirm("Do you really want to delete the file(s) selected?");
		if(y==true)
		{ return true; }
		else
		{ return false; }
	}
}

/*function deleteFolder(aid)
{
	var confirmX;
	confirmX=window.confirm("Are you sure you want to delete this album and all the files in it?\r\nNOTE: This cannot be undone!");
	if(confirmX==true)
	{
		window.location="?page=account&action=album_delete&aid="+aid;
	}
	else
	{
		return false;
	}
}*/

function setPasswordProtected()
{
	confirmPP=window.confirm("Confirm to make this album password protected?\r\nOnly those who have the password can see your album.");	
	if(confirmPP==true)	return true;
	else return false;
}

function setFilePasswordProtected()
{
	confirmFPP=window.confirm("Confirm to make your profile page password protected?\r\nOnly those who have the password can see your files.");	
	if(confirmFPP==true)	return true;
	else return false;
}

function removePasswordProtected()
{
	confirmPP=window.confirm("Confirm to remove password protection for this album?");	
	if(confirmPP==true)	return true;
	else return false;
}

function removeFilePasswordProtected()
{
	confirmFPP=window.confirm("Confirm to remove password protection for your profile page?");	
	if(confirmFPP==true) return true;
	else return false;
}

function chkContactDelete()
{
	confirmCD=window.confirm("Confirm to delete this contact?");	
	if(confirmCD==true) return true;
	else return false;
}

function deleteAlbum()
{
	confirmX=window.confirm("Are you sure you want to delete this album and all the files in it?\r\nNOTE: This cannot be undone!");	if(confirmX==true)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function showSelected(x)
{
	if(x.options[x.selectedIndex].value=='1') 
	{ hideBoth(); }
	if(x.options[x.selectedIndex].value=='2') 
	{  showFolders(); }
	if(x.options[x.selectedIndex].value=='3')
	{ showPerms(); }
}

function albumRename(aid)
{
	document.getElementById("renameField").innerHTML='<form method="post" action="?page=account&action=album_rename"><input type="hidden" name="album_id" value="'+aid+'" /><input type="text" name="new_album_name" maxlength="50" class="input" /><input type="submit" value="Rename Album" class="blueBtn" /></form>';
}

function albumRenameCancel(aid)
{
	document.getElementById("renameField").innerHTML="<a href='#' onclick='albumRename("+aid+")>Rename</a>";
}

function selectAll(a) {
    var theForm = document.myform;
    for (i=0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='file_id[]')
            theForm.elements[i].checked = a;
    }
}

function downloadAll(a) {
    var theForm = document.downloadimage;
    for (i=0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='file_id[]')
            theForm.elements[i].checked = a;
    }
}

function chkPPass(pp)
{
	var reason = "";
	var passVal = pp.pro_password.value;
	reason = validate(pp.pro_password);
	
	if(reason != "")
	{
		alert("Please fill out password field!");
		pp.pro_password.focus();
		return false;
	}
} // end

function chkFPPass(pp)
{
	var reason = "";
	var passVal = pp.file_pro_password.value;
	reason = validate(pp.file_pro_password);
	
	if(reason != "")
	{
		alert("Please fill out password field!");
		pp.file_pro_password.focus();
		return false;
	}
} // end

function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be 300 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

