
// Generic DOM shortcuts

function el(id) {
	return document.getElementById(id)
}

function eln(id) {
	return document.getElementsByName(id)[0];
}

function show(id) {
    el(id).style.display='';
}

function hide(id) {
    el(id).style.display='none';
}

function disable(id) {
    el(id).disabled='disabled';
}

function enable(id) {
    el(id).disabled='';
}

function newtag(name) {
	return document.createElement(name);
}

function newtext(text) {
	return document.createTextNode(text);
}

function removeChildren(el) {
	while (el.childNodes.length != 0) {
		el.removeChild(el.childNodes[0]);
	}
}

function replicate(node_id, limit)
{
	node = el(node_id);
	nclone=node.cloneNode(true);
	if (node.parentNode.childNodes.length<limit)
		node.parentNode.appendChild(nclone);
}

function lite(img) {
    img.src = img.src.replace('.gif', '_on.gif');
}

function unlite(img) {
    img.src = img.src.replace('_on.gif', '.gif');
}


// Special functions

function gototab(tag, cls, id) {
    var els = document.getElementsByTagName(tag);
    for (i=0; i<els.length; i++) {
        var e = els[i];
        if (e.className.indexOf(cls)>-1) e.style.display='none';
    }
    el(id).style.display='';
    return false;
}

function summarize(tag, cls, target) {
    var els = document.getElementsByTagName(tag);
    s = 0;
    for (i=0; i<els.length; i++) {
        var e = els[i];
        if (e.className.indexOf(cls)>-1) s += parseFloat(e.value);
    }
    eln(target).value = s;
}

// Shopping cart functions

function scart_recount(is_cta) {
    
    // for all rows
    row = el('cart').firstChild;
    while (row.tagName!='TR' || row.className!='row') {
        row = row.nextSibling;
    }
    tsum = 0;
    while (row && row.className=='row' && row.id!='non') {
        if (row.firstChild.nextSibling.firstChild.tagName=='SELECT') {
            count = row.firstChild.nextSibling.firstChild.value;
            count = parseFloat(count);
        } else count=1;
        price = row.firstChild.nextSibling.nextSibling.firstChild.data;
        price = parseFloat(price.substr(1, price.length-1));
        discount = row.firstChild.nextSibling.nextSibling.nextSibling.firstChild.data;
        discount = parseFloat(discount.substr(2, discount.length-2));
        if (count==0) 
            sum=0;
        else 
            sum = price*count-discount;
        row.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.data = '$'+sum;
        tsum +=sum;
        row = row.nextSibling;
        while (row && row.tagName!='TR')
            row = row.nextSibling;
    }
    
    if (is_cta == 0){ 
        row = row.nextSibling;    
        while (row && row.tagName!='TR')
            row = row.nextSibling;
    }
    row.firstChild.nextSibling.firstChild.data = '$'+tsum;
}

function reset_price(){
    node = eln('scart')
    input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", "action");
    input.setAttribute("value", "reset");
    node.appendChild(input);
    document.scart.submit();
}

function alter_progs(sign){
    node = eln('scart')
    input = document.createElement("input");
    input.setAttribute("type", "hidden");
    input.setAttribute("name", "action");
    if (sign>0) input.setAttribute("value", "plus_prog");
    else input.setAttribute("value", "minus_prog");
    node.appendChild(input);
    document.scart.submit();
}

function plus_prog(){
    alter_progs(1);
}

function minus_prog(){
    alter_progs(-1);
}

// My settings functions

function princ2admin() {
    tags = ['input', 'textarea'];
    for (j=0; j<tags.length; j++) {
        els = document.getElementsByTagName(tags[j]);
        for (i=0; i<els.length; i++) {
            var e = els[i];
            if (e.name.indexOf('principle')==0) {
                eln('admin'+e.name.substr(9)).value = e.value;
            }
        }
    }
    return false;
}

function copy(){
    f = new Array('address', 'city', 'state', 'province', 'zip', 'country', 'phone', 'email', 'fname', 'lname');
    
    for (var i=0; i<10; i++){
        e = eln(f[i]);
        f_e = eln('b_'+f[i]);
        f_e.value = e.value;
    }
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function image_size(){
    el = eln('logo');
    im = new Image();
    im.src = el.value;
    if (im.height>120 || im.width>216)
        alert('Logo sizes smaller then 120 x 216 pixels only.');    
}

// Menu section
var delay = 500; 
var tim;
var curDivID=null;
function showMenu(el, divID, x, y){
  var pos = findPos(el);
  if (curDivID!=null) curDivID.style.display="none"; 
  curDivID = divID;
  cancelHide();
  ds = curDivID.style; 
  ds.top = pos[1]+el.offsetHeight;//el.offsetBottom+el.offsetHeight;
  ds.left = pos[0];//el.offsetLeft;
  ds.display = "block";
}

function hideMenu(){tim =setTimeout("curDivID.style.display = 'none'",delay)}

function cancelHide(){clearTimeout(tim)}

function toggle(obj){
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
  }
