﻿String.prototype.trim = function() { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }

//http://www.webreference.com/programming/javascript/onloads/
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        };
    }
}

function valAppForm() {
    var c = true;
    if (!valTextBox('fname', c)) { c = false; }
    if (!valTextBox('lname', c)) { c = false; }
    if (!valTextBox('address', c)) { c = false; }
    if (!valTextBox('city', c)) { c = false; }
    if (!valTextBox('state', c)) { c = false; }
    if (!valTextBox('zip', c)) { c = false; }
    if (!valTextBox('country', c)) { c = false; }
    if (!valTextBox('phone', c)) { c = false; }
    if (!valDropDown('position', c)) { c = false; }
    if (!valDropDown('avail', c)) { c = false; }
    if (!valCheckbox('first_shift') && !valCheckbox('second_shift') && !valCheckbox('third_shift') && !valCheckbox('day_12') && !valCheckbox('night_12')) {
        c = false;
        addCssClass(document.getElementById('shift-pref'), 'error');
    } else {
        removeCssClass(document.getElementById('shift-pref'), 'error');
    }
    if (!valTextBox('start_date', c)) { c = false; }
    if (!valCheckbox('confirm')) {
        c = false;
        addCssClass(document.getElementById('confirm').parentNode, 'error');
    } else {
        removeCssClass(document.getElementById('confirm').parentNode, 'error');
    }
    //
//    if (!valTextBox('', c)) { c = false; }
//    if (!valTextBox('', c)) { c = false; }

    return c;
}
function valCheckbox(id){
    var c = document.getElementById(id);
    if (!c) alert('Could not find checkbox ' + id);
    return (c && c.checked);
}
function valDropDown(id, curValStatus) {
    var s = document.getElementById(id);
    if (!s) alert('Could not find select ' + id);
    var ret = (s && s.selectedIndex > 0);
    if (ret) {
        removeCssClass(s, 'error');
    } else {
        addCssClass(s, 'error');
    }

    if (curValStatus && !ret) {
        s.focus();
    }
    return ret;
}
function valTextBox(id, curValStatus) {
    var tb = document.getElementById(id);
    if (!tb) alert('Could not find text box ' + id);
    var ret = (tb && tb.value.trim().length > 0)
    if (ret) {
        removeCssClass(tb, 'error');
    } else {
        addCssClass(tb, 'error');
    }

    if (curValStatus && !ret) {
        tb.focus();
    }
    return ret;
}
function addCssClass(obj, className) {
    if (!new RegExp('\\b' + className + '\\b').test(obj.className)) {
        obj.className += obj.className ? ' ' + className : className;
    }
}
function removeCssClass(obj, className) {
    var rep = obj.className.match(' ' + className) ? ' ' + className : className;
    obj.className = obj.className.replace(rep, '');
}
//function jscss(a,o,c1,c2)
//{
//  switch (a){
//    case 'swap':
//      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): <-
//      o.className.replace(c1,c2);
//    break;
//    case 'add':
//      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
//    break;
//    case 'remove':
//      var rep=o.className.match(' '+c1)?' '+c1:c1;
//      o.className=o.className.replace(rep,'');
//    break;
//    case 'check':
//      return new RegExp('\\b'+c1+'\\b').test(o.className)
//    break;
//  }
//}


sfHover = function() {
    if (!document.getElementById("top-nav")) return;
    var sfEls = document.getElementById("top-nav").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function() { this.className += " hover"; }
        sfEls[i].onmouseout = function() { this.className = this.className.replace(/hover/g, ""); }
    }
}

function changeHomeImage(img) {
    if (!img) img = '/images/product_lines/keystone_faded.jpg';
    document.getElementById('home-products-image').src = img;
}
function goToFirstChild(obj) {
    window.location = obj.getElementsByTagName('a')[0].href;
}

function swapImageNeeded() {
    var headerImage = document.getElementById('header-image');
    if (headerImage && headerImage.src && headerImage.src.indexOf('#') == headerImage.src.length - 1) {
        headerImage.src = '/images/large_photos/image-needed.jpg';
        setInterval(blinkImageNeeded, 250);
    }
}

function blinkImageNeeded() {
    var headerImage = document.getElementById('header-image');
    if (headerImage.style.borderWidth && headerImage.style.borderWidth!= '0px') {
        headerImage.style.borderWidth = '0px';
        headerImage.style.height = '160px';
        headerImage.style.width = '464px';
        headerImage.style.borderColor = '#FF0000';
        headerImage.style.borderStyle = 'solid';
    } else {
        headerImage.style.borderWidth = '4px';
        headerImage.style.height = '152px';
        headerImage.style.width = '456px';
        headerImage.style.borderColor = '#FF0000';
        headerImage.style.borderStyle = 'solid';
    }
}
/*Querystring parser*/
function qs(key) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == key) {
            return ft[1];
        }
    }
}
function openSampleRequest() {
    var w = window.open('/ProductRequest.aspx', '_blank', 'width=700,height=700');
    w.focus();
    return false;
}
addLoadEvent(sfHover);
addLoadEvent(swapImageNeeded);
