function PopupLink(link, width, height, wname) {
	if (!width)
	{
		width = Math.ceil(screen.width / 2);
		if (width < 600)
		{
			width = 600;
		}
	}
	if (width > screen.width)
	{
		width = screen.width;
	}
	if (!height)
	{
		height = Math.ceil(screen.height * 0.6);
		if (height < 400)
		{
			height = 400;
		}
	}
	if (height > screen.height)
	{
		height = screen.height;
	}
	if (!wname)
	{
		wname = "_blank";	
	}
	var left = Math.ceil((screen.width - width)/2);
	var top = Math.ceil((screen.height - height)/2);
	
	var oTarget;
	oTarget = window.open(link, wname, "height="+height+",width="+width+",scrollbars=1,resizable,left="+left+",top="+top);
	/*if (oTarget == null || !oTarget.innerWidth && !oTarget.innerHeight) { // google chrome compatible
		alert('Please turn your Popup Blockers off for this website.');	
	}*/
	return oTarget;
}

function ShowHide(divId)
{
	var id = document.getElementById(divId);
	if (id.style.display == "none")
	{
		eval("id.style.display = 'block';");
	}
	else
	{
		eval("id.style.display = 'none';");
	}
}

function CheckAll(elem)
{
  if (elem.checked)
  {
    for (i = 0; i < document.forms.list.elements.length; i++)
    {
      if (document.forms.list.elements[i].type == "checkbox")
      {
        document.forms.list.elements[i].checked = true;
      }
    }
  }
  else
  {
    for (i = 0; i < document.forms.list.elements.length; i++)
    {
      if (document.forms.list.elements[i].type == "checkbox")
      {
        document.forms.list.elements[i].checked = false;
      }
    }
  }
}

function UnCheckAll(elem)
{
  if (!elem.checked)
  {
    document.forms.list.checkall.checked = false;
  }
}

//
// removes left and right spaces from string
// Parameters:
// str - some string
// Returns:
// string without left and right spaces
//
function trim(str)
{
	str = str.replace(/^(\s)*/, "");
	str = str.replace(/(\s)*$/, "");
	return str;
}

function storeCaret(element)  
{  
	if (document.selection && document.selection.createRange)  
		element.caretPos = document.selection.createRange().duplicate();  
}  
 
function InsertText(id)  
{  
	var text = document.getElementById(id + "Tag").value;
	var element = document.getElementById(id);
	if (element == null)
	{
		var elements = document.getElementsByName(id);
		if (elements.length == 0)
		{
			alert("Textarea element is not found!");
			return;
		}
		element = elements[0];
	}

	if (text == "")
	{
		alert("Select tag!");
		return;
	}

	if (unique = document.getElementById(text) && element.value.indexOf(text, 0) != -1)
	{
		alert("This tag is already used!");
	}
	else
	{
		if (element && element.caretPos)
		{
			element.caretPos.text = text;  
		}
		else if (element && element.selectionStart + 1 && element.selectionEnd + 1)  
			element.value = element.value.substring(0, element.selectionStart) + text + element.value.substring(element.selectionEnd, element.value.length);  
		else if (element)
		{
			element.value += text;
		}
	}	
}

//
// Tests if value is found in array
// Parameters:
// value - value to find
// array - where to find
// Returns:
// If any element of array equals to value, then true is returned. Otherwise, false
// is returned.
//
function in_array(value, array) {
  var result = false;
  var n = array.length;
  var i;

  for (i = 0; i < n; i++) {
    if (array[i] == value) {
      result = true;
      break;
    }
  }

  return result;
};

function ClearOtherTestEmails(name)
{
	var i;
	jQuery('input:text').each(function(i, el) 
	{
		if (el.name && el.name.substr(0, 9) == "TestEmail" && el.name != name)
		{
			el.value = "";
		}
	});
	return true;
}

function DisplayArea(elem, id)
{
	var i, divs = new Array("html", "name", "divtag", "template", "templatetext");
	if (elem)
	{
		for (i = 0; i < divs.length; i++)
		{
			if (jQuery("#"+id+divs[i]))
				jQuery("#"+id+divs[i]).hide();
		}
	}
	else
	{
		for (i = 0; i < divs.length; i++)
		{
			if (jQuery("#"+id+divs[i]))
				jQuery("#"+id+divs[i]).show();
		}
	}
	return true;	
}

