// JavaScript Common Functions
// By Chris Wohlsen


function preloadImages() {
    var args, a, img;
 
    args = preloadImages.arguments;
    for (a = 0 ; a < args.length ; a++) {
        img = new Image;
        img.src = args[a];
    }
}


function changeImage(id, href) {
    var img;

    img = document.getElementById(id);
    if (img) { img.src = href; }
}


function setCookie(name, value, time) {
    var expires, cookie;

    expires = new Date();
    expires.setTime(expires.getTime() + (time * 1000));
    cookie = (name + "=" + escape(value) + ";expires=" + expires.toGMTString());
    
    document.cookie = cookie;
}


function getCookie(name) {
    var cookie, cName, start, end;

    cookie = document.cookie;
    if (cookie.length > 0) {
        cName = name + "=";
        start = cookie.indexOf(cName);

        if (start != -1) {
            start += cName.length;
            end = cookie.indexOf(";", start);
            if (end == -1) { end = cookie.length; }
            
            return unescape(cookie.substring(start, end));
        }
    }

    return null;
}


function deleteCookie(name) {
    if (getCookie(name)) { document.cookie = name + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT'; }
}


var _state = 0;
var _inches = false, _metric = false;

function initUnits() {
    var rules, s, r, cookie;
    
     _inches = _metric = false;

    for (s = 0 ; !_inches && !_metric ; s++) {
        if (document.styleSheets[s].cssRules) { rules = document.styleSheets[s].cssRules; }
        else { rules = document.styleSheets[s].rules; }

        if (!rules.length) { break; }

        for (r = 0 ; r < rules.length ; r++) {
            if (rules[r].selectorText == '.inches') { _inches = rules[r]; }
            else if (rules[r].selectorText == '.metric') { _metric = rules[r]; }
        }
    }
    
    cookie = getCookie('Units');
    if (cookie != null) { _state = parseInt(cookie); }
}


function switchUnits() {
    if (!_inches || !_metric) { return; }

    if (_state) {
        _metric.style.display = 'none';
        _inches.style.display = 'inline';
        _state = 0;
    } else {
        _inches.style.display = 'none';
        _metric.style.display = 'inline';
        _state = 1;
    }
    
    setCookie('Units', _state, 60*60*24*365*5);
}


function stripeTable() {
    var args, a, table, trs, x;
 
    args = stripeTable.arguments;
    for (a = 0 ; a < args.length ; a++) {
        table = document.getElementById(args[a]);
        if (table) {
            trs = table.rows;
            for (x = args[a+2]; x < trs.length; x += 2) {
                if (trs[x].className != 'ignore') { trs[x].style.backgroundColor = args[a+1]; }
            }
        }
    }
}


function verifyEmail() {
	var sel, comps, l, i, email, result;

	sel = document.uploadform.contact_email.value;
	if (!sel.length) { return false; }

	comps = sel.split(",");
	
	l = comps.length;
	result = true;
	for (i = 0 ; i < l ; i++) {
		email = trim1(comps[i]);
		if (!email.match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$')) { result = false; }
	}

	if (!result) { alert('Please enter a valid contact email address.'); }
	return result;
}


function trim1(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
