//-----------------------------------------------------------------------------
function onKeyupPhone(evt)
{
	var elt = ie ? evt.srcElement : evt.target;
	var key = String.fromCharCode(evt.keyCode);
	if (/[_ -]/.test(key))
		return false;
	if ( /^[a-zA-Z0-9]$/.test(key) && elt.value.length >= elt.size)
		document.forms.Contacts[next[elt.name]].focus();
	return true;
}
//-----------------------------------------------------------------------------
function onKeydownPhone(evt)
{
	if (/[_ -]/.test(String.fromCharCode(evt.keyCode)))
		return false;
}
//-----------------------------------------------------------------------------
function RoundDigits(amount,digits)
{
  if (!Number(amount)) 
  	return 0;
   return Math.round(Number(amount)*Math.pow(10,digits))/Math.pow(10,digits);
}
//-----------------------------------------------------------------------------
function CutDigits(amount,digits)
{
  if (!Number(amount)) 
  	amount = "0";
  var res=String(Math.round(Number(amount)*Math.pow(10,digits))/Math.pow(10,digits));
  if (res.indexOf(".")==-1) {
    res+=".";
    for (i=0;i<digits;i++) res+="0";
  }
  else {
    var kl=res.length-res.indexOf(".") - 1;
    for(i=kl;i<digits;i++) res+="0";
  }
  return(res);
}
//-----------------------------------------------------------------------------
function hide(elt)
{
	var div = elt.desc;
	div.style.display="none";
	showPulldowns();
}
//-----------------------------------------------------------------------------
function show(elt,divname)
{
	var div = elt.desc;
	if (!div)
	{
		div = document.getElementById(divname);
		div.style.position = 'absolute';
		div.style.backgroundColor= 'white';
		div.style.border= '1px';
		div.style.borderColor= 'black';
		div.style.borderStyle= 'solid';
		div.style.zIndex= '10001';
		elt.desc = div;
	}
	var top =  GetY(elt) + elt.offsetHeight;
	var left = window.event.clientX;
	div.style.top = top  + "px"; 
	div.style.left =left + "px"; 
	div.style.display="block";
	hidePulldowns(left,top,left + div.offsetWidth,top + div.offsetHeight)
}
//-----------------------------------------------------------------------------
function getText(elt)
{
	if (typeof(elt.getText) == "function")
		return elt.getText();
	switch(elt.nodeName.toLowerCase())
	{
	case "input":
	case "textarea":
		return elt.value;
	case "select":
		return elt.options[elt.selectedIndex].innerText;
	default:
		return elt.innerText
	}

}
//-----------------------------------------------------------------------------
function getValue(elt)/*{{{*/
{
	if (typeof(elt.getValue) == "function")
		return elt.getValue();
	if (!elt.nodeName && elt.length)
		return GetCheckValue(elt);
	switch(elt.nodeName.toLowerCase())
	{
	case "textarea":
	case "input":
 		switch(elt.type)
		{
		case "checkbox":
		case "radio":
			return GetCheckValue(elt);
		default:
			return elt.value;
		}
		break;
	case "select":
		return GetSelectValue(elt);
	default:
		return elt.innerText
	}
}/*}}}*/
//-----------------------------------------------------------------------------
function setValue(elt,value)/*{{{*/
{
	if (typeof(elt.setValue) == "function")
		return elt.setValue(value);
	switch(elt.nodeName.toLowerCase())
	{
	case "textarea":
	case "input":
 		switch(elt.type)
		{
		case "checkbox":
		case "radio":
			return SetCheckValues(elt,value);
		default:
			return elt.value = value;
		}
		break;
	case "select":
		return SetSelectValue(elt,value);
		break;
	default:
		return elt.innerText = value
	}
}/*}}}*/
//-----------------------------------------------------------------------------
function getAncestorByTagName(elt,tagName,level) {/*{{{*/
	level = level || 1;
	var i = 0;
	var elt_prev = elt;
	while(elt && i < level)
	{
		elt = elt.parentNode;
		if (!elt)
			elt = elt_prev.parentElement;
		if (elt && elt.nodeName == tagName)
			i++;

		elt_prev = elt;
	}
	return elt;
}/*}}}*/
//-----------------------------------------------------------------------------
function map1(arr) {/*{{{*/
	var o=new Object;
	for(var i=0;i<arr.length;i++)
	{
		o[String(arr[i])]=1;
	}
	return o;
}/*}}}*/
//-------------------------------------------------------------------------
function GetRadioValue(radio) {/*{{{*/
	for (var i=0;i<radio.length;i++)
	{
		if (radio[i].checked)
			return radio[i].value;
	}
}/*}}}*/
//-------------------------------------------------------------------------
function SetRadioValue(radio,value) {/*{{{*/
	for (var i=0;i<radio.length;i++)
	{
		if (radio[i].value == value)
		{
			radio[i].checked=1;
			return;
		}
	}
}/*}}}*/
//-------------------------------------------------------------------------
function GetSelectValue(what) {/*{{{*/
	var v=new Array();
	var obj=what.options;
	for (i=0;i<obj.length;i++)    
	{
		if (obj[i].selected)
			v[v.length]=obj[i].value;
	}
	return v.join(",");
}/*}}}*/
//-------------------------------------------------------------------------
function SetSelectValue(what,value) {/*{{{*/
	var obj=what.options;
	var o=map1(value.split(", "));
	for (var i=0;i<obj.length;i++)
	{
		if (o[obj[i].value] != null)
			obj[i].selected=1;
	}
}/*}}}*/
//-------------------------------------------------------------------------
function GetCheckValue(check) {/*{{{*/
	var v=new Array();
	if (!check.length)//not an array
	{
		if (check.checked)
			return check.value;
		return "";
	}
	//array
	for (i=0;i<check.length;i++)
	{
		switch(check[i].type)
		{
		case "checkbox":
		case "radio":
			if (check[i].checked)
				v[v.length]=check[i].value;
			break;
		default:
			v[v.length]=check[i].value;
		}
	}
	return v.join(", ");
}/*}}}*/
//-------------------------------------------------------------------------
function SetCheckValues(check,value) {/*{{{*/
	if (typeof(check) !="object")
		return ;
	if (!check.length)
	{
		check.checked = check.value==value;
		return;
	}
	var o=map1(value.split(/,\s*/));
	for (var i=0;i<check.length;i++)
	{
		if (o[check[i].value])
			check[i].checked=true;
		else
			check[i].checked=false;
	}
}/*}}}*/
//-------------------------------------------------------------------------
function dump(obj,name) {/*{{{*/
	var txt="";
	switch (typeof (obj))
	{
	case "object":
		for (var  i in obj)
		{
			if (typeof(obj[i]) == "function")
				continue;
			if (typeof(obj[i]) == "object")
				txt+= dump(obj[i],name + "[" + i + "]") + "\n";
			else
				txt+=name + "[" + i + "]=" + String(obj[i]) + "\n";
		}
		break;
	default:
		txt +=String(obj);
	}
	return txt;
}/*}}}*/
//-------------------------------------------------------------------------
function dump_one(obj,name) {/*{{{*/
	var txt="<pre>";
	switch (typeof (obj))
	{
	case "object":
		for (var  i in obj)
			txt+=name + "." + i + "=" + obj[i] + "\n";
		break;
	case "array":
		for (var  i=0;i<obj.length;i++)
			txt+=name + "[" + i + "]=" +obj[i]  + "\n";
		break;
	default:
		txt +=String(obj);
	}
	return txt + "</pre>";
}/*}}}*/
//-------------------------------------------------------------------------
function SetCheckValues(check,value){/*{{{*/
	if (typeof(check) !="object")
		return ;
	if (!check.length) {
		check.checked = check.value==value;
		return;
	}
	var o=map1(value.split(/,\s*/));
	for (var i=0;i<check.length;i++) {
		if (o[check[i].value])
			check[i].checked=true;
		else
			check[i].checked=false;
	}
}/*}}}*/
//-------------------------------------------------------------------------
function checkAll(checks,checked){/*{{{*/
	if (!checks)
		return
	if (!checks.length)
		checks.checked = checked
	else
		for (var i = 0;i<checks.length;i++)
			checks[i].checked = checked
}/*}}}*/
//-------------------------------------------------------------------------
function checkFieldsAreEqual(form,name1,name2){/*{{{*/
	if (getValue(form[name1]) != getValue(form[name2]))
		return {'error':1,'name':name1};
}/*}}}*/
//-------------------------------------------------------------------------
function checkFieldConditionally(form,name,conditions){/*{{{*/
	// {
	// 		'nationality_other':/\S/
	// 		'nationality':/^\$$/
	// }
	// if name == 'nationality_other' then ok if 
	// nationality other than '$' or nationality_other isn't empty
	// if name == 'nationality' then ok if nationality_other is not empty 
	// or nationality is '$'
	var cond,val,ms,matches=true;
//	alert(name + " | " + conditions.dump());
	for (var i in conditions){
		cond = conditions[i];
//		alert(cond + " | " + cond.constructor);
		if ( cond.constructor != RegExp)
			continue;
// silently return one of fields not present
		if (!form[i]) return;
		val = getValue(form[i]);
		ms = cond.test(val);
//		alert(i + " | " + name + " | " + val + " | " + matches);
		if (!ms && name != i)
			return;
		if ( i == name ) matches = ms;
	}
	if (!matches)
		return {'error':1,'name':name};
}/*}}}*/
//-------------------------------------------------------------------------
function OpenWindow(url, wwidth, wheight, scrolls)
{
  usrwidth=screen.width;
  usrheight=screen.height;
  winwidth=wwidth;
  winheight=wheight;
  var X=(usrwidth-winwidth)/2;
  var Y=(usrheight-winheight)/2;
  properties="toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width="+winwidth+",height="+winheight+",";
  checkie=navigator.appName.indexOf("icrosoft");
  if (checkie==-1) properties = properties+"screenX="+X+",screenY="+Y; else properties = properties+"left="+X+",top="+Y;
  if (scrolls==1) properties=properties+",scrollbars=yes"; else properties=properties+",scrollbars=no";
  var NW=window.open(url,"",properties);
  NW.focus();
}


function popForm(id, mode)
{
	var frm = document.getElementById(id);

	if (mode == "close"){
		frm.style.zIndex = '0'; 
		frm.style.display = 'none';
	}
	else{
		frm.style.zIndex = '100'; 
		frm.style.display = 'block';
	
		var scrollTop = 0;
		var clientHeight = 0;
		var clientWidth = 0;
		if (document.body){
			scrollTop = document.body.scrollTop;
			clientHeight = document.body.clientHeight;
			clientWidth = document.body.clientWidth;
		}

		//alert(clientHeight + " " + frm.offsetHeight);

		frm.style.top = scrollTop + Math.floor((clientHeight/2) - (frm.offsetHeight/2)) + 'px';
		frm.style.left = Math.floor((clientWidth/2) - (frm.offsetWidth/2)) + 'px';
	}

	return false;
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
