// JavaScript Document

//
function toggle_visibility_link(id, ids, idLink, idLinks) {
	var e = document.getElementById(id);
	var a = document.getElementById(idLink);
    
	if(e.style.display == 'block') {
		e.style.display = 'none';
		a.style.fontWeight = 'normal';
	}
	else {
		e.style.display = 'block';
		a.style.fontWeight = 'bold';
	}
		
	for(var i = 0; i < ids.length; i++) {
		e = document.getElementById(ids[i]);
		a = document.getElementById(idLinks[i]);
		
		if(e.style.display == 'block') {
			e.style.display = 'none';
		a.style.fontWeight = 'normal';
		}
	}
}

// 
function toggle_visibility_select(sel) {
	var id = document.getElementById(sel).options[document.getElementById(sel).selectedIndex].title;
	var len = document.getElementById(sel).length;
	var opts = document.getElementById(sel).options;
	for(i=0; i < len; i++) {
		if(document.getElementById(opts[i].title) != null) {
			if(opts[i].title == id) {
				document.getElementById(opts[i].title).style.display = '';
			}
			else {
				document.getElementById(opts[i].title).style.display = 'none';
			}
		}
	}
}

//
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} 
	else {
		document.getElementById(limitCount).firstChild.nodeValue = limitNum - limitField.value.length;
	}
}

// Convert numbers to words
// copyright 25th July 2006, by Stephen Chapman http://javascript.about.com
// permission to use this Javascript on your web page is granted
// provided that all of the code (including this copyright notice) is
// used exactly as shown (you can change the numbering system if you wish)

// American Numbering System
var th = ['','thousand','million', 'billion','trillion'];
// uncomment this line for English Number System
// var th = ['','thousand','million', 'milliard','billion'];

var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen']; var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety']; function toWords(s){s = s.toString(); s = s.replace(/[\, ]/g,''); if (s != String(parseFloat(s))) return 'not a number'; var x = s.indexOf('.'); if (x == -1) x = s.length; if (x > 15) return 'too big'; var n = s.split(''); var str = ''; var sk = 0; for (var i=0; i < x; i++) {if ((x-i)%3==2) {if (n[i] == '1') {str += tn[Number(n[i+1])] + ' '; i++; sk=1;} else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}} else if (n[i]!=0) {str += dg[n[i]] +' '; if ((x-i)%3==0) str += 'hundred ';sk=1;} if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}} if (x != s.length) {var y = s.length; str += 'point '; for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';} return str.replace(/\s+/g,' ');}

//
function chkcontrol(name, box, maxBox) {
	var total=0;
	for(var i=0; i < document.getElementsByName(name).length; i++) {
		if(document.getElementsByName(name)[i].checked) {
			//alert("1. "+document.getElementsByName(name).item(i));
			total =total +1;
		}
		if(total > maxBox) {
			alert("Please select only "+toWords(maxBox));
			//alert("2. "+document.getElementsByName(name).item(i));
			document.getElementsByName(name)[box].checked = false ;
			return false;
		}
	}
}

//
function resetValueIfEmpty(id, val) {
	if(document.getElementById(id).value == '') {
		document.getElementById(id).value = val;
	}
}

/* Script by: www.jtricks.com
 * Version: 20070301
 * Latest version:
 * www.jtricks.com/javascript/window/box_alone.html
 */
// Moves the box object to be directly beneath an object.
function move_box(an, box)
{
    var cleft = 0;
    var ctop = 0;
    var obj = an;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += an.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

// Hides other alone popup boxes that might be displayed
function hide_other_alone(obj)
{
    if (!document.getElementsByTagName)
        return;

    var all_divs = document.body.getElementsByTagName("DIV");

    for (i = 0; i < all_divs.length; i++)
    {
        if (all_divs.item(i).style.position != 'absolute' ||
            all_divs.item(i) == obj ||
            !all_divs.item(i).alonePopupBox)
        {
            continue;
        }

        all_divs.item(i).style.display = 'none';
    }
    return;
}

// Shows a box if it wasn't shown yet or is hidden
// or hides it if it is currently shown
function show_hide_box(an, width, height, borderStyle)
{
    var href = an.href;
    var boxdiv = document.getElementById(href);

    if (boxdiv != null)
    {
        if (boxdiv.style.display=='none')
        {
            hide_other_alone(boxdiv);
            // Show existing box, move it
            // if document changed layout
            move_box(an, boxdiv);
            boxdiv.style.display='block';

            // Workaround for Konqueror/Safari
            if (!boxdiv.contents.contentWindow)
                boxdiv.contents.src = href;
        }
        else
            // Hide currently shown box.
            boxdiv.style.display='none';
        return false;
    }

    hide_other_alone(null);

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    // Add object identification variable
    boxdiv.alonePopupBox = 1;

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = borderStyle;
    boxdiv.style.textAlign = 'right';
    boxdiv.style.padding = '4px';
    boxdiv.style.background = '#FFFFFF';
    document.body.appendChild(boxdiv);

    var offset = 0;

    // Remove the following code if 'Close' hyperlink
    // is not needed.
    var close_href = document.createElement('a');
    close_href.href = 'javascript:void(0);';
    close_href.onclick = function()
        { show_hide_box(an, width, height, borderStyle); }
    close_href.appendChild(document.createTextNode('Close'));
    boxdiv.appendChild(close_href);
    offset = close_href.offsetHeight;
    // End of 'Close' hyperlink code.

    var contents = document.createElement('iframe');
    //contents.scrolling = 'no';
    contents.overflowX = 'hidden';
    contents.overflowY = 'scroll';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.contents = contents;
    boxdiv.appendChild(contents);

    move_box(an, boxdiv);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false;
}

//
function makeListFriendly(idsTemp, ids) {
	for(var i = 0; i < ids.length; i++) {
		var tempString = document.getElementById(idsTemp[i]).value;
		tempString = tempString.replace(/~/g,'');
		tempString = tempString.replace(/,/g,'~');
		document.getElementById(ids[i])
		document.getElementById(ids[i]).value = tempString;
	}
}

//
function resetMessage(id, msg, origMsg)
{
	if(document.getElementById(id).value == origMsg)
	{
		document.getElementById(id).value = msg;
	}
}

//
function showTextBox(id)
{
	if(!document.getElementById(id).readOnly)
	{
		document.getElementById(id).style.backgroundColor = "white";
		document.getElementById(id).style.border = "1px solid black";	
	}
	else
	{
		document.getElementById(id).blur();
	}
}

//
function hideTextBox(id, bckgrnd)
{	
	if(!document.getElementById(id).readOnly)
	{
		document.getElementById(id).style.backgroundColor = bckgrnd;
		document.getElementById(id).style.border = "none";
	}
}

//
function resetCheckboxes(cb) {
  for(i=0; i<cb.length-1; i++){
    cb[i].checked = false;
  }
}

//
function resetCheckbox(cb) {
  for(i=cb.length-1; i<cb.length; i++){
    cb[i].checked = false;
  }
}
