var codearray = [];
var codesopen = false;
var codesover = false;
var namesover = false;
var formfield = "";
var timer1;

function clickthis() {
	winhandler(this.id);
}


function mouseover() 
{
	obj = getThisElement(this.id);
	obj.style.background = '#CFDAF8';
	namesover = true;
}

function mouseout() 
{
	obj = getThisElement(this.id);
	obj.style.background = '#FFFFFF';
	namesover = false;
}

function winhandler(o) 
{
	var f = document.Form1;
	if (o) 
	{
		divid    = o.substring(o.indexOf('_')+1,o.indexOf('-'));
		dialcode = o.substring(o.indexOf('-')+1,o.length);
		f.elements[formfield].value = dialcode;
		hideDialCodes();
	}
	if (codesopen && (!codesover && !namesover)) 
	{
		timer1 = setTimeout('hideDialCodes()',100);
	} 
	else 
	{
		if (timer1) clearTimeout(timer1);
	}
}

function makeDialCodes() 
{
	var par = getThisElement('countrycodes');
	for (i=0; i<DialCodes.length; i++) {
		c = "";
		
		mainid	= 'country_' + DialCodes[i].ID + '-' + DialCodes[i].Code;
		maindiv	= document.createElement('div');
		maindiv.setAttribute('id',mainid);
		maindiv.setAttribute((browser.ie)?'className':'class','cname');
		par.appendChild(maindiv);
		
		xb.addEvent(getThisElement(mainid), 'mouseover', mouseover, false);
		xb.addEvent(getThisElement(mainid), 'mouseout',  mouseout,  false);
		xb.addEvent(getThisElement(mainid), 'click',     clickthis, false);
		
		c += "<table style=\"width:210px;\"><tr>";
		c += "<td class=\"cname left\">"   + DialCodes[i].Name + "</td>";
		c += "<td class=\"ccode right\">+" + DialCodes[i].Code   + "</td>";
		c += "</tr></table>";
		
		getThisElement(mainid).innerHTML = c;
	}
}

function showDialCodes(o) 
{
	formfield = o;
	codesopen = true;
	if (browser.dom) 
	{
		var obj		= getThisElement(o);
		var ccodes	= getThisElement('countrycodes');
		var coords	= getThisPosition(obj);
		setPosition(ccodes,coords[0],coords[1]+obj.offsetHeight);
		setGeometry(ccodes,225,200);
		setVisibility(ccodes,true);
	}
}

function hideDialCodes() 
{
	obj = getThisElement('countrycodes');
	setPosition(obj,0,0);
	setGeometry(obj,0,0);
	setVisibility(obj,false);
}

function setPosition(o,x,y) 
{
	o.style.left	= setPix(x);
	o.style.top	= setPix(y);
}

function setGeometry(o,w,h) 
{
	o.style.width	= setPix(w);
	o.style.height	= setPix(h);
}

function setVisibility(o,v) 
{
	if (v) o.style.visibility = 'visible';
	else o.style.visibility = 'hidden';
}

function setPix(n) 
{
	return n + 'px';
}

function getThisElement(o) 
{
	return document.getElementById(o);
}

function getThisPosition(o) 
{
	var curleft = curtop = 0;
	if (o.offsetParent) {
		curleft = o.offsetLeft
		curtop = o.offsetTop
		while (o = o.offsetParent) {
			curleft += o.offsetLeft
			curtop += o.offsetTop
		}
	}
	return [curleft,curtop];
}

function strip(str) {
	var newstring = "";
	for (i=0; i < str.length; i++) {
		chr = str.charAt(i);
		if (/\d/.test(chr)) newstring += chr;
	}
	return newstring;
}

