var EDIT_TIMEOUT = null;
var EDIT_OBJ = null;
var EL_COUNTRY = null;
function formatPhone(el, id_el_coutnry)
{
    if(EDIT_TIMEOUT){
	if(EDIT_OBJ == el){
	    clearTimeout(EDIT_TIMEOUT);
	    EDIT_TIMEOUT = null;
	} else {
	    formatPhone_real();
	}
	EDIT_OBJ = null;
    }
    EDIT_OBJ = el;
	try {
		EL_COUNTRY = document.getElementById(id_el_coutnry);
	} catch(e) {
	
	}
    EDIT_TIMEOUT = setTimeout("formatPhone_real()", 750);
}


function formatPhone_real()
{
        if(!EDIT_OBJ)
            return false;

    	var el = EDIT_OBJ;

	var phone_number = el.value;
	var new_phone = '';
    phone_number = phone_number.replace(/\D*/g, "");
	phone_number = phone_number.substring(0,16);
	
	var fl_format = true;
	if (EL_COUNTRY) {
		if (EL_COUNTRY.value == '234'  || EL_COUNTRY.value == '40') { // USA OR CANADA
			fl_format = true;
		} else {
			fl_format = false;
		}
	}
	if (fl_format) {
		for (var i = 0; i < phone_number.length; i++) {
			new_phone = new_phone + phone_number.charAt(i);
			if ((i == 2 || i == 5) && phone_number.charAt(i+1) != '') {
				new_phone = new_phone + '-';
			}
			if ((i == 9 && phone_number.length > 10) && phone_number.charAt(i+1) != '') {
				new_phone = new_phone + ' ext. ';
			}
		}

		if(el.value != new_phone)
			el.value = new_phone;
	}
	return true;
}


//+++++---HINT----+++++

function getElementPos(obj){
    var l = 0;
    var t = 0;
    var w = obj.offsetWidth;
    var h = obj.offsetHeight;
    while (obj) {
        l += obj.offsetLeft;
        t += obj.offsetTop;
        if ((obj.tagName != "TABLE") && (obj.tagName != "BODY")) {
            l += (obj.clientLeft)?obj.clientLeft:0;
            t += (obj.clientTop)?obj.clientTop:0;
        }
        obj = obj.offsetParent;
    }
    var res = new Object();
    res.x = l;
    res.y = t;
    res.left = l;
    res.top = t;
    res.w = w;
    res.h = h;
    res.width = w;
    res.height = h;
    return res;
}

function showhint(obj, id_el_coutnry){
    var fl_format = true;
	try {
		EL_COUNTRY = document.getElementById(id_el_coutnry);
		if (EL_COUNTRY.value == '234'  || EL_COUNTRY.value == '40') { // USA OR CANADA
			fl_format = true;
		} else {
			fl_format = false;
		}
	} catch(e) {
	
	}
	if (fl_format) {
		var div_hint=document.createElement("div");
	    div_hint.id = 'hint';
	    div_hint.className = 'hint';
	    div_hint.innerHTML = 'Use the numbers only and formatting will be done automatically.<br />E.g.: 1231231234123456 will be formated as 123-123-1234 ext. 123456';
	    var pos = getElementPos(obj);
	    div_hint.style.top = pos.top+20 + 'px';
	    div_hint.style.left = pos.left+20 + 'px';
		div_hint.style.visibility = 'visible';
	    document.body.appendChild(div_hint);
	}
    return true;
}

function hidehint(obj){
    try {
		document.body.removeChild(document.getElementById('hint'));
	} catch (e) {
	
	}
    return true;
}