/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function trim(str)
{
    return str.replace(/^\s+|\s+$/g, '');
}

function setCookie(name, value, expires, path, domain, secure)
{
    var curCookie = '';

    curCookie+= name + "=" + escape(value)
    curCookie+= ((expires) ? "; expires=" + expires.toGMTString() : "")
    curCookie+= ((path)    ? "; path=" + path                     : "")
    curCookie+= ((domain)  ? "; domain=" + domain                 : "")
    curCookie+= ((secure)  ? "; secure"                           : "");

    document.cookie = curCookie;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin != -1)
        begin += 2;
    else
    {
        begin = dc.indexOf(prefix);
        if (begin != 0)
            return null;
    }
    
    var end = document.cookie.indexOf(";", begin);
    
    if (end == -1)
        end = dc.length;

    return unescape(dc.substring(begin + prefix.length, end));
}

function saveScroll(pageID)
{
    var now = new Date();
    var x=window.document.documentElement.scrollLeft || window.pageXOffset || 0;
    var y=window.document.documentElement.scrollTop  || window.pageYOffset || 0;

    now.setTime(now.getTime() + 10 * 1000);
    setCookie("xy_for_"+pageID, x + "_" + y, now);
}

function loadScroll(pageID)
{
    var xy = getCookie("xy_for_"+pageID);
    var now = new Date();

    if (!xy)
        return;

    var ar = xy.split("_");

    if (ar.length == 2)
        window.scroll(parseInt(ar[0]), parseInt(ar[1]));
    
    now.setTime(now.getTime() - 10 * 1000);
    setCookie("xy_for_"+pageID, null, now);
}

function addOption(theSel, theText, theValue)
{
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    
    theSel.options[selLength] = newOpt;
}

function addOptionAtIndex(theSel, index, theText, theValue)
{
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;

    if(index>=selLength || index<0)
        theSel.options[selLength] = newOpt;
    else
    {
        for(var i=selLength;i>index;i--)
            theSel.options[i] = new Option(theSel.options[i-1].text,theSel.options[i-1].value);
        theSel.options[index] = newOpt;
    }
}

function deleteOption(theSel, theIndex)
{
    var selLength = theSel.length;
    
    if(selLength>0 && theIndex>=0 && theIndex<selLength)
        theSel.options[theIndex] = null;
}

function moveOptions(theSelFrom, theSelTo)
{
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    var i;

    // Find the selected Options in reverse order
    // and delete them from the 'from' Select.
    for(i=selLength-1; i>=0; i--)
    {
        if(theSelFrom.options[i].selected)
        {
            selectedText[selectedCount] = theSelFrom.options[i].text;
            selectedValues[selectedCount] = theSelFrom.options[i].value;
            deleteOption(theSelFrom, i);
            selectedCount++;
        }
    }

    // Add the selected text/values in reverse order.
    // This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    for(i=selectedCount-1; i>=0; i--)
        addOption(theSelTo, selectedText[i], selectedValues[i]);
}

function openGeneralWindow(path)
{
    var windowFeatures;

    windowFeatures ='status=yes,';
    windowFeatures+='width=450px,height=440px,';
    windowFeatures+='left=10px,top=250px,';
    windowFeatures+='scrollbars=yes,';
    windowFeatures+='resizable=no';

    var generalWindow=window.open(path,"windox",windowFeatures);
    generalWindow.focus();
}

function openExtraLargeImageWindow(path)
{
    var windowFeatures;

    windowFeatures ='status=yes,';
    windowFeatures+='width=520px,height=670px,';
    windowFeatures+='left=10px,top=100px,';
    windowFeatures+='scrollbars=yes,';
    windowFeatures+='resizable=no';

    var generalWindow=window.open(path,"windox",windowFeatures);
    generalWindow.focus();
}

function openInfoWindow(path)
{
    var windowFeatures;

    windowFeatures ='status=yes,';
    windowFeatures+='width=800px,height=600px,';
    windowFeatures+='left=10px,top=60px,';
    windowFeatures+='scrollbars=yes,';
    windowFeatures+='resizable=no';

    var generalWindow=window.open(path,"windox",windowFeatures);
    generalWindow.focus();
}

function getBlank (form, stdValue) {
    if (form.value == stdValue) {
        form.value = '';
        form.style.fontStyle='normal';
        form.style.fontWeight='bold';
        form.style.color='#000000';
    }
    return true;
}

function getPrompt (form, stdValue) {
    if (form.value == '') {
        form.style.fontStyle='italic';
        form.style.fontWeight='100';
        form.style.color='#999999';
        form.value = stdValue;
    }
    return true;
}

function isValidEmail(address) {
    var reg = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
    
    if(reg.test(address) == false) {
        return false;
    } else {
        return true;
    }
}
