// Returns element by specified id (for browsers compatibility)
function getElement(aID)
{
	return (document.getElementById) ?
		document.getElementById(aID) : document.all[aID];
}

function correctEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

// Returns node value of first child of element
function getNodeValue(element)
{
	return element.firstChild.nodeValue;
}

// Set nodeValue to first child of the specified element
function setNodeValue(element, nodeValue)
{
	element.firstChild.nodeValue = nodeValue;
}

function ClickBankHelpPopup()
{
	if (!window.focus)
		return true;
	window.open("http://www.gslasher.com/manuals/clickbankcsv.html",
		'ClickBankCSVHelp', 'width=400,height=500,scrollbars=yes,directories=no,' +
		'location=no,menubar=no,resizable=yes,status=yes,toolbar=no');
	return false;
}

function confirmDeleteRender(name)
{
	return confirm("Delete page \"" + name + ".html\"? The page and its statistics will be deleted permanently!");
}

function confirmDeleteCampaign(name)
{
	return confirm("Delete campaign \"" + name + "\"? The campaign and its pages will be deleted permanently!");
}

function confirmDeleteProduct(name)
{
	return confirm("Delete product \"" + name +
		"\"? The product and its statistics will be deleted permanently!");
}

function confirmDeleteUser(email)
{
	return confirm("Delete user " + email +
		"? All user's data will be deleted permanently!");
}

function confirmEnableUser(email)
{
	return confirm("ENABLE user " + email + "?");
}

function confirmDisableUser(email)
{
	return confirm("DISABLE user " + email + "?");
}

function validateCategory(templateId)
{
	var cat = getElement("categorySelector");
	if (!cat || cat.value == '')
	{
		alert("Please select category and then click on template!");
		return false;
	}
	else
	{
		elem = getElement('templateIdIE');
		elem.value = templateId;
		return true;
	}
}

function confirmChangeUserEmail(serverId, userId, currentEmail, addToUserActionUrl)
{
	var newEmail = prompt("Enter new email", currentEmail);
	if (newEmail != null && newEmail != "")
  {
  	if (currentEmail == newEmail)
  	{
  		alert("You have entered the same email value!");
  		return false;
  	}
  	else if (!correctEmail(newEmail))
  	{
  		alert("Incorrect email value!");
  		return false;
  	}
  	
  	var link = "index.php?action=users&serverid=" + serverId + "&userid=" + userId +
  		"&newemail=" + newEmail + addToUserActionUrl; 
  	
  	window.location.href = link;
  	return false; 
  }
  else
  	return false;
}

function confirmChangeUserPassword(serverId, userId, currentPassword, addToUserActionUrl)
{
	var newPassword = prompt("Enter new password", currentPassword);
	if (newPassword != null && newPassword != "")
  {
  	if (currentPassword == newPassword)
  	{
  		alert("You have entered the same password value!");
  		return false;
  	}
  	
  	var link = "index.php?action=users&serverid=" + serverId + "&userid=" + userId +
  		"&newpassword=" + newPassword + addToUserActionUrl; 
  	
  	window.location.href = link;
  	return false; 
  }
  else
  	return false;
}

function confirmRemindUserPassword(serverId, userId, email, password,
	firstName, addToUserActionUrl)
{
	var answer = confirm("Send password reminder e-mail to user " + email + "?");
	if (answer)
  {	
  	var link = "index.php?action=users&serverid=" + serverId + "&userid=" + userId +
  		"&remindpassword=" + escape(password) +
  		"&remindtoemail=" + escape(email) +
  		"&firstname=" + escape(firstName) +
  		addToUserActionUrl;
  	
  	window.location.href = link;
  	return false; 
  }
  else
  	return false;
}