var doc = document;
var docFrm = doc.forms[0];

function CreateDir()
{
	dirName = prompt('Type the name of the directory you want to create:', '');

	if ((dirName) && (dirName!=""))
	{
		document.forms['BrowseFiles'].elements['funcParam'].value = dirName;
		__doPostBack('CreateDir', '');
	}
}

function DeleteItem()
{
	if (confirm('Do you want to delete the selected folder(s) and/or file(s)?'))
		__doPostBack('Delete','');
}

function Rename(path)
{
	newName = prompt('Type the new name for this file/folder:','');

	if ((newName) && (newName!=""))
	{
		document.forms['BrowseFiles'].elements['funcParam'].value = path;
		document.forms['BrowseFiles'].elements['funcExtraParam'].value = newName;
		__doPostBack('Rename', '');
	}
}

function FormatBool(tableId, columnIndex)
{
	var t = doc.getElementById(tableId);
	
	for(var i=0; i < t.rows.length; i++)
	{
		if(t.rows[i].children[columnIndex].innerText.toLowerCase() == "true" || 
			t.rows[i].children[columnIndex].innerText.toLowerCase() == "1")
		{
			t.rows[i].children[columnIndex].innerHTML = "&uuml;";
			t.rows[i].children[columnIndex].className = "GridLabelSymbolGreen";	
		}
		else
		{
			t.rows[i].children[columnIndex].innerHTML = "&ucirc";
			t.rows[i].children[columnIndex].className = "GridLabelSymbolRed";	
		}
		i++;
	}
}

function AddRandomNumber()
{
	return "r="+Math.round(Math.random()*1000000);
}

function SortColumn(field, column)
{
	var order = docFrm.Order.value;

	if(field == docFrm.Sort.value)
	{
		if(order != "Desc")
			order = "Desc";
		else
			order = "";
	}
	else
		order = "";
	
	docFrm.Sort.value = field;
	docFrm.Order.value = order;
	
	docFrm.submit();
}

function ResizeColumns()
{
	var firstRow = InnerTable.children[0].children[0];
	
	if(typeof(firstRow) == "undefined")return;
	
	firstRow.children[0].style.width = HeadTable.children(0).children(0).children(0).clientWidth;
	firstRow.children[1].style.width = HeadTable.children(0).children(0).children(1).clientWidth;
	firstRow.children[2].style.width = HeadTable.children(0).children(0).children(2).clientWidth - 20;
}

function FilterList(param)
{
	docFrm.Filter.value = param;
	docFrm.submit();
}

function ShowUsersDetails(userId)
{
	var path = "UserDetails.aspx?UserId=" + userId + "&d=1&" + AddRandomNumber();
	var rv = OpenWindow(path, 420, 520, window);
}

function ShowPopup(path, title, width, height, top, left)
{
	var leftPosition;
	var topPosition;
	
	height = typeof height != "undefined" ? height : 400;
	width = typeof width != "undefined" ? width : 400;
	
	if(typeof top == "undefined")
		topPosition = (screen.height) ? (screen.height-height)/2 : 0;
	else
		topPosition = top;
		

	if(typeof left == "undefined")
		leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	else
		leftPosition = left;

	window.open(path, title, "scrollbars=yes,height="+ height +",width="+ width +",status=no,toolbar=no,menubar=no,location=no,resizable=no, top=" + topPosition + ", left=" + leftPosition);
}

function OpenWindow(path, width, height, args)
{
	args[1] = path;
	var features = "dialogWidth:" + width + "px;dialogHeight:" + height + "px; center:yes; help:no;";
	features += "resizable:no; scroll:no; status:no";
	var rv = window.showModalDialog(path, args, features);
	
	//var leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	//var topPosition = (screen.height) ? (screen.height-height)/2 : 0;
	
	//var rv = window.open(path, null, "height=" + height + ", width=" + width + ",status=no,toolbar=no,menubar=no,location=no,resizable=no, top=" + topPosition + ", left=" + leftPosition);
	return rv;
}

function Delete(msg)
{
	if(!confirm(msg))return;
	var elem = doc.createElement("<input type='hidden' value='1' name='Delete'>");
	docFrm.insertBefore(elem);
	docFrm.submit();
}

function SubmitEnquiry(validate, pathAbove)
{
	if(typeof validate != "undefined" && validate == true)
		var valid = ValidateForm();

	if(!valid) return;

	if(typeof pathAbove != "undefined" && pathAbove == true)
		docFrm.action = "../EmailPage.aspx";
	else
		docFrm.action = "EmailPage.aspx";

	docFrm.submit();
}

/* Validation */
var success = true;

function InvalidateElement(elem)
{
	success = false;
	elem.style.backgroundColor = "red";
	elem.style.color = "white";
	elem.style.fontWeight = "bold";
}

function ValidateElement(elem)
{
	elem.style.backgroundColor = "";
	elem.style.color = "";
	elem.style.fontWeight = "";
}

function ValidateForm()
{
	success = true;
	for(var i=0; i < docFrm.length; i++)
	{
		if(docFrm[i].name.substring(0, 1) != "r")continue;

		if(typeof docFrm[i].validate != "undefined" && docFrm[i].validate != "")
		{
			switch(docFrm[i].validate)
			{
				case "Required":
					if(docFrm[i].value.length < 1)
						InvalidateElement(docFrm[i]);
					else if(docFrm[i].value == "-1")
						InvalidateElement(docFrm[i]);
					else
						ValidateElement(docFrm[i]);
				break;
				case "Required, Number":
					if(docFrm[i].value.length < 1)
						InvalidateElement(docFrm[i]);
					else if(isNaN(docFrm[i].value))
						InvalidateElement(docFrm[i]);
					else
						ValidateElement(docFrm[i]);
				break;
				case "Required, Email":
					if(docFrm[i].value.length < 1)
						InvalidateElement(docFrm[i]);
					else if(!IsEmail(docFrm[i].value))
						InvalidateElement(docFrm[i]);
					else
						ValidateElement(docFrm[i]);
				break;
				case "Number":
					if(isNaN(docFrm[i].value) && docFrm[i].value.length > 0)
						InvalidateElement(docFrm[i]);
					else
						ValidateElement(docFrm[i]);
				break;
				case "Email":
					if(!IsEmail(docFrm[i].value) && docFrm[i].value.length > 0)
						InvalidateElement(docFrm[i]);
					else
						ValidateElement(docFrm[i]);
				break;
			}
		}
	}

	return success;
}

function IsEmail(email)
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

	return re.test(email)
}
/* End validation */