function fieldset_highlight(obj,state) {
        for (var i=0, fieldset=obj.parentNode; i<10 && fieldset && fieldset.tagName!='FIELDSET'; i++, fieldset=fieldset.parentNode);

        if (fieldset && fieldset.tagName=='FIELDSET') {
                if (state) fieldset.className += ' highlight';
                else fieldset.className = fieldset.className.replace(/ highlight/,'');
        }
}

function fieldset_inner_highlight(obj,state) {
        for (var i=0, fieldset=obj.parentNode.parentNode; i<10 && fieldset && fieldset.tagName!='FIELDSET'; i++, fieldset=fieldset.parentNode.parentNode);

        if (fieldset && fieldset.tagName=='FIELDSET') {
                if (state) fieldset.className += ' highlight';
                else fieldset.className = fieldset.className.replace(/ highlight/,'');
        }
}

function fieldset_highlight_event(e) {
        if (!e) e = window.event;
        var obj = e.srcElement ? e.srcElement : e.target;
        var state = e.type=='focus';
        fieldset_highlight(obj,state);
}

function fieldset_init(fs) {
        var f = fs.getElementsByTagName('INPUT'), i;
        for (i=0; i<f.length; i++) {
                f[i].onfocus = fieldset_highlight_event;
                f[i].onblur = fieldset_highlight_event;
        }
        f = fs.getElementsByTagName('SELECT');
        for (i=0; i<f.length; i++) {
                f[i].onfocus = fieldset_highlight_event;
                f[i].onblur = fieldset_highlight_event;
        }
        f = fs.getElementsByTagName('TEXTAREA');
        for (i=0; i<f.length; i++) {
                f[i].onfocus = fieldset_highlight_event;
                f[i].onblur = fieldset_highlight_event;
        }
}

function fieldset_init_all() {
        var fs = document.getElementsByTagName('FIELDSET'), i;
        for (i=0; i<fs.length; i++) {
                fieldset_init(fs[i]);
        }
}

var cid_id = 1;
function phoneField(ctrl, value, required){
        var cname = ctrl;
        var id = cname;
        ctrl = document.getElementById(ctrl);
          ctrl.style.display = 'none';
          if(required) ctrl.className += ' hidden';
        var ac = document.createElement('input'), fp = document.createElement('input'), sp = document.createElement('input'), cnt = document.createElement('div');
        if(value){
                try{//Example of number: (321) 123-321
                 re = value.match(/[\(]?(\d{3})[\)\-][\s]?(\d{3})\-(\d{3,4})/); 
                 ac.value = re[1];
                 fp.value = re[2];
                 sp.value = re[3];
                 } catch(e){}
        }
        ac.next = fp;
        fp.next = sp;
        ac.type = fp.type = sp.type = 'text';
        ac.id = 'ac:' + id;
        fp.id = 'fp:' + id;
        sp.id = 'sp:' + id;
        ac.className = fp.className = sp.className = 'input-text'/* + (style ? ' ' + style : '')*/;
        ac.style["width"] = fp.style["width"] = '35px';
        sp.style["width"] = '45px';
        ac.size = fp.size = 3;
        ac.maxLength = fp.maxLength = 3;
        sp.size = 4;
        sp.maxLength = 4;
        ac.cname = fp.cname = sp.cname = cname;
//         ac.onkeydown = fp.onkeydown = sp.onkeydown = function(e){
//                 var keynum;
//                 if(window.event) {// IE
//                         keynum = event.keyCode;
//                 } else if(e.which){ // Netscape/Firefox/Opera
//                         keynum = e.which;
//                 }
//                 var keychar = String.fromCharCode(keynum);
//                 var numcheck = /\d/;
//                 return numcheck.test(keychar)
//                                 || keynum == 8
//                                 || keynum == 9
//                                 || keynum == 37
//                                 || keynum == 39
//                                 || keynum == 46;
//         }
        ac.onkeyup = fp.onkeyup = function(){
                if(this.value.length >= 3){
                     this.value = this.value.substring(0,3);
                     this.next.focus();
                }
        }
        ac.onchange = fp.onchange = sp.onchange = function(){
                var id = this.id.split(':')[1];
                if(document.getElementById('ac:'+id).value || document.getElementById('fp:'+id).value || document.getElementById('sp:'+id).value)
                {
                    document.getElementById(this.cname).value =
                        '(' + document.getElementById('ac:'+id).value + ') ' +
                        document.getElementById('fp:'+id).value + '-' +
                        document.getElementById('sp:'+id).value;
                }else
                {
                   document.getElementById(this.cname).value = ''; 
                }
        }
        
        cnt.appendChild(ac);
//        var t = document.createElement('text');
//        t.innerHTML = '-';
        var t = document.createTextNode(" - ");
        cnt.appendChild(t);
        cnt.appendChild(fp);
//        var t = document.createElement('text');
//        t.innerHTML = '-';
        var t = document.createTextNode(" - ");
        cnt.appendChild(t);
        cnt.appendChild(sp);
        ctrl.parentNode.insertBefore(cnt, ctrl);
        ac.onchange();    
        
}

