function showDashboard()
{ 
    showDiv('dashBoardBack');
    showDiv('popup_outer');
}

function hideDashboard()
{
    hideDiv('dashBoardBack');
    hideDiv('popup_outer');
}

function hideDiv(div) {
	document.getElementById(div).style.display = 'none';
}

function showDiv(div) {
	document.getElementById(div).style.display = 'inline';
}

function setVisible(id, visible)
{
    document.getElementById(id).style.display = visible;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}




function setcookie(name, value, expires, path, domain, secure) 
{
    expires instanceof Date ? expires = expires.toGMTString() : typeof(expires) == 'number' && (expires = (new Date(+(new Date) + expires * 1e3)).toGMTString());
    var r = [name + "=" + escape(value)], s, i;
    for(i in s = {expires: expires, path: path, domain: domain}){
        s[i] && r.push(i + "=" + s[i]);
    }
    return secure && r.push("secure"), document.cookie = r.join(";"), true;
}

function getcookie( check_name ) {
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
      
    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );
            
                
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        
        // if the extracted name matches passed check_name
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if ( a_temp_cookie.length > 1 )
            {
                cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
} 
function setAddtionalOptsVisible(id, visible, plus)
{
    document.getElementById(id).style.display = visible;
    if (visible=="none")
        document.getElementById(plus).innerHTML = '<img src="/images/upload/up.gif" />'; else
        document.getElementById(plus).innerHTML = '<img src="/images/upload/down.gif" />'; 
}

function setJobVisible(idList, plus)
{                  
    var ids = idList.split(",");
                               
    for (var i=0; i<ids.length; i++)
    {             
        var id = ids[i];   
        var visStyle = "";
        if (document.getElementById(id).style.display!="")
          visStyle = ""; else
          visStyle = "none";
        document.getElementById(id).style.display = visStyle;
        if (document.getElementById(id).style.display=="")
            document.getElementById(plus).innerHTML = '<img src="/images/upload/down.gif" />'; else
            document.getElementById(plus).innerHTML = '<img src="/images/upload/up.gif" />';
    } 
}

function openJobVisible(idList, plus)
{                  
    var ids = idList.split(",");
                               
    for (var i=0; i<ids.length; i++)
    {             
        var id = ids[i];   
        var visStyle = "";
        document.getElementById(id).style.display = visStyle;
        document.getElementById(plus).innerHTML = '<img src="/images/upload/up.gif" />';
    } 
}

function closeJobVisible(idList, plus)
{                  
    var ids = idList.split(",");
                               
    for (var i=0; i<ids.length; i++)
    {             
        var id = ids[i];   
        var visStyle = "none";
        document.getElementById(id).style.display = visStyle;
        document.getElementById(plus).innerHTML = '<img src="/images/upload/down.gif" />';
    } 
}

function setFolderItemVisible(id, plus)
{
    var item = document.getElementById(id);    
    var visible = item.style.display;     

    var visStyle = "";
    if (visible!="")
      visStyle = ""; else
      visStyle = "none";           
    document.getElementById(id).style.display = visStyle;
    if (visible!="")
        document.getElementById(plus).innerHTML = '<img src="/images/upload/down.gif" />'; else
        document.getElementById(plus).innerHTML = '<img src="/images/upload/up.gif" />';

}




function removeTextInput(form_name, id)
{    
  document.forms[form_name].removeChild(document.getElementById(id));
  document.forms[form_name].removeChild(document.getElementById('remove_'+id));
}


function checkShipping(select, zip, get_prices)
{
    document.getElementById(get_prices).disabled = !((document.getElementById(select).value!="") && (document.getElementById(zip).value!=""));
}  

function showHideUpload(detailDiv, imageDiv)
{
    var detail = document.getElementById(detailDiv);
    var image = document.getElementById(imageDiv);
    if (detail.style.display == 'none')
    {
        detail.style.display = '';
        image.innerHTML = '<img src="'+SERVER_NAME+'/images/upload/down.gif" align="top" />';
    } else
    {
        detail.style.display = 'none';
        image.innerHTML = '<img src="'+SERVER_NAME+'/images/upload/up.gif" align="top" />';
    }
}

function popUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 0,top = 0');");
}

function $(name)
{
    return document.getElementById(name);
}

function checkoutPayment()
{
    var a = document.createElement('a');
    a.setAttribute('href', SERVER_NAME+'store/pre-process-payment.html?ajax=true');
    a.appendChild(document.createTextNode("Checkout"));
    Shadowbox.open(a);
}

function paymentTypeChange(select)
{ 
    $('cc_num').setAttribute('cancelValidate', 'true');
    $('cc_month').setAttribute('cancelValidate', 'true');
    $('cc_year').setAttribute('cancelValidate', 'true');
    $('check_num').setAttribute('cancelValidate', 'true');
    $('check_override_code').setAttribute('cancelValidate', 'true');
    $('cash_override_code').setAttribute('cancelValidate', 'true');


    if (select.value == "")
    {
        $('promotional_code_li').style.display = 'none';
        
        $('ccFields').style.display = 'none';
        $('checkFields').style.display = 'none';
        $('cashFields').style.display = 'none';
    } else
    {
        $('promotional_code_li').style.display = '';
    }

    if (select.value == 'Check/P.O.')
    {
        $('check_num').setAttribute('cancelValidate', '');
        $('check_override_code').setAttribute('cancelValidate', '');
    
        $('ccFields').style.display = 'none';
        $('checkFields').style.display = '';
        $('cashFields').style.display = 'none';
        alert('Your order will not begin production until payment is received in full.');
    } else
    if (select.value == 'Cash')
    {
        $('cash_override_code').setAttribute('cancelValidate', '');
        
        $('ccFields').style.display = 'none'; 
        $('checkFields').style.display = 'none';
        $('cashFields').style.display = '';
        alert('Your order will not begin production until payment is received in full.');
    } else
    if (select.value != '')
    {
        $('cc_num').setAttribute('cancelValidate', '');
        $('cc_month').setAttribute('cancelValidate', '');
        $('cc_year').setAttribute('cancelValidate', '');
        
        $('ccFields').style.display = '';
        $('checkFields').style.display = 'none';
        $('cashFields').style.display = 'none';
        
        if (select.value=="American Express")
        {
            $('cc_num').setAttribute('my_maxlength', '15');
        } else
        {
            $('cc_num').setAttribute('my_maxlength', '16');
        }
        $('cc_num').onkeypress = ccNumKeyPress;
        
    } 
}

function savedCCChange()
{
    if ($('saved_cc').value=="")
    {
        $('ccFields').style.display = '';
        
        $('payment_type_li').style.display = '';
        $('cc_type').setAttribute('cancelValidate', '');
        $('cc_num').setAttribute('cancelValidate', '');
        $('cc_month').setAttribute('cancelValidate', '');
        $('cc_year').setAttribute('cancelValidate', '');
        
        $('promotional_code_li').style.display = 'none';
		
		$('check_num').setAttribute('cancelValidate', '');
		$('check_override_code').setAttribute('cancelValidate', '');
		
		$('cash_override_code').setAttribute('cancelValidate', '');
        
        $('cc_type').onchange();
    } else
    { 
        $('ccFields').style.display = 'none';
        $('checkFields').style.display = 'none';
        $('cashFields').style.display = 'none';
        
        $('payment_type_li').style.display = 'none';
        $('cc_type').setAttribute('cancelValidate', 'true');  
        $('cc_num').setAttribute('cancelValidate', 'true');
        $('cc_month').setAttribute('cancelValidate', 'true');
        $('cc_year').setAttribute('cancelValidate', 'true');
		
		$('check_num').setAttribute('cancelValidate', 'true');
		$('check_override_code').setAttribute('cancelValidate', 'true');
		
		$('cash_override_code').setAttribute('cancelValidate', 'true');
        
        $('promotional_code_li').style.display = '';
    }
    
}

function collapseUploadDiv(key)
{
    var detail = document.getElementById('details'+key);
    var image = document.getElementById('arrow'+key);
    
    detail.style.display = 'none';
    image.innerHTML = '<img src="'+SERVER_NAME+'/images/upload/up.gif" align="top" />';
}
function openUploadDiv(key)
{
    var detail = document.getElementById('details'+key);
    var image = document.getElementById('arrow'+key);
    
    detail.style.display = '';
    image.innerHTML = '<img src="'+SERVER_NAME+'/images/upload/down.gif" align="top" />';
}

function updateCartProduct(key, name, value)
{
    makeRequest(SERVER_NAME+'store/update-cart-product.html?ajax=true&update='+name+'^'+key+'&value='+value, 'aResult');
}

function newButtonClicked(key)
{
    var a = document.createElement('a');
    a.setAttribute('href', SERVER_NAME+'store/edit-shipping-address.html?ajax=true&product_key='+key);
    a.appendChild(document.createTextNode("New"));
    a.rel = "width=465;height=400;";
    Shadowbox.open(a);
    
    return false;
}

function editButtonClicked(id)
{
    var a = document.createElement('a');
    a.setAttribute('href', SERVER_NAME+'store/edit-shipping-address.html?ajax=true&id='+id);
    a.appendChild(document.createTextNode("Edit"));
    a.rel = "width=465;height=400;";
    Shadowbox.open(a);
    
    return false;
}

function dropshipButtonClicked()
{
    var a = document.createElement('a');
    a.setAttribute('href', SERVER_NAME+'store/edit-dropship-address.html?ajax=true');
    a.appendChild(document.createTextNode("Dropship"));
    a.rel = "width=690;height=400;";
    Shadowbox.open(a);    
    
    return false;
}

function shippingAddressOnChange(select, key)
{
    $('tr2'+key).style.height = '200px';

    
    if (select.selectedIndex==0)
    { 
        $('btnEdit'+key).style.display = 'none';
    }
    
    if (select.selectedIndex>0) 
    {
        $('btnEdit'+key).style.display = '';
    } 
    
	if(select.value == 'New')
	{
		eval('newButtonClicked('+key+');');
		$('btnEdit'+key).style.display = 'none';		
	}
    else
	{
		eval('updateShippingOptions'+key+'();');		
	}    
    
    
    
    
    
    makeRequest(SERVER_NAME+'store/get-shipping-more-info-ajax.html?ajax=true&value='+select.value+'&key='+key, 'shippingInfo'+key); 

    
}

function shippingProviderOnChange(select, key)
{
    if (select.selectedIndex==0 || select.value == 'New')
    {
        $('shippingAddress'+key).style.display = 'none';
        $('shippingAddress'+key+'_icon').style.display = 'none';
        $('shippingAddress'+key+'_icon_text').style.display = 'none';
        $('shippingInfo'+key).style.display = 'none';
    }
    
    if (select.selectedIndex>0 && select.value != 'New')
    {
        $('shippingAddress'+key).style.display = '';
        $('shippingAddress'+key+'_icon').style.display = '';
        $('shippingAddress'+key+'_icon_text').style.display = '';
        $('shippingInfo'+key).style.display = '';
    }
    
    if (select.options[select.selectedIndex].text == 'UPS')
    {
        $('shippingInsurance'+key).style.display = '';
    } else
    {
        $('shippingInsurance'+key).style.display = 'none';
    }
    

    if (select.options[select.selectedIndex].text == 'My Ship Account')
	{
        $('shipping_account_span').style.display = '';
    } else
    {
        $('shipping_account_span').style.display = 'none';
    }
    
    if (select.options[select.selectedIndex].text == "Pickup")
    { 
        if ($('shippingAddress'+key).options.length>2)
        { 
            $('shippingAddress'+key).selectedIndex = 1;
            makeRequest(SERVER_NAME+'store/update-cart-product.html?ajax=true&update=shipping_address^'+key+'&value='+$('shippingAddress'+key).value, 'aResult');
            $('shippingAddress'+key).onchange();
        }
    }
    
    if ($('shippingAddress'+key).selectedIndex==0)
    { 
        $('btnEdit'+key).style.display = 'none';
    }
    
    if ( ($('shippingAddress'+key).selectedIndex>0) && ($('shippingAddress'+key).value!="New") )
    {
        $('btnEdit'+key).style.display = '';
    } 
    
    
    
    eval('updateShippingOptions'+key+'();');
    makeRequest(SERVER_NAME+'store/get-shipping-provider-tooltip.html?ajax=true&id='+select.value, 'tooltip'+key);
}

function isRadioChecked(rbName, formName)
{
    var isAnyChecked = false;
    //var rbName = e.name;
    //var formName = arguments[1];


    for (var ratiosI=0; (ratiosI < document.forms[formName].elements[rbName].length); ratiosI++)
    {
       if (document.forms[formName].elements[rbName].item(ratiosI).checked)
       {
          isAnyChecked = true;
       }
    }
    
    return isAnyChecked;
}
/*
function checkIfShippingComplete(provider, address, method, id, plus, formName)
{
    var valid = true;
    
    if (provider.selectedIndex==0)
    {
        valid = false;
    }
    
    if ( (address.selectedIndex==0) || (address.selectedIndex==(address.length-1))  )
    {
        valid = false;
    }

    if ($(method)==null) return false;

    if (!isRadioChecked($(method).name, formName))
    {
        valid = false;
    }
    
    if (arguments[6]!=null)
    {
        return valid;
    }
    
	var key = formName.substr(("product").length);
    if (valid)
    {
		$('shipCompValid'+key).innerHTML = '';
        var item = document.getElementById(id);    
        var visible = item.style.display;     

        var visStyle = "none";
        document.getElementById(id).style.display = visStyle;
        document.getElementById(plus).innerHTML = '<img src="/images/upload/up.gif" />';
    }else{
		$('shipCompValid'+key).innerHTML = '!';
	}
    
    return valid;
}

function canKillShippingTimer(key)
{
    return checkIfShippingComplete('shippingProvider'+key, 'shippingAddress'+key, 'shippingOption'+key, 'productSetupShipping'+key, 'showHideShippingConf'+key, 'product'+key, true)
}
*/
function number_format( number, decimals, dec_point, thousands_sep ) 
{
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function showHideUVCoatingOption(mailing, uv, key)
{   
    var uv_select = $(uv); 
    var mailing_select = $(mailing);
    var hide = ""; 
    var exact_size = $('Must_Be_Exact_Size');
    if (exact_size==null)
    {
        exact_size = $('Must_Be_Exact_Size^'+key);
    }
    
        
    
    if (uv_select!=null)
    {  
        var to_select = uv_select.options[uv_select.selectedIndex].text;
        if (mailing_select.options[mailing_select.selectedIndex].text == "Yes Mailing Services")
        {
            hide = "UV Coating";
            
            if (uv_select.options[uv_select.selectedIndex].text == "UV Coating")
            {
                to_select = "UV 1-Side";
            }
            
            if (exact_size!=null)
            {
                for (var i=0; i<exact_size.length; i++)
                if (exact_size.options[i].text=="Yes")
                    exact_size.selectedIndex = i;
            }
            
        } else
        {
            hide = "";
        }
        
        while (uv_select.options.length>0)
        {
            uv_select.remove(0);
        }
        
        var uv_arr;
        eval("uv_arr = Coating_Type"+key+";");
       
        for (var i=0; i<uv_arr.length; i++)
        { 
            if (uv_arr[i][1]==hide) continue;
            var option = document.createElement('option');
            option.text = uv_arr[i][1];
            option.value = uv_arr[i][0];
            uv_select.options.add(option);
            
            if (to_select==uv_arr[i][1]) uv_select.selectedIndex = uv_select.options.length-1;
        }
        
    }
}

function showHideScoring(key)
{  
    if ( (key=='')  )
    { 
        var folding = $('Folding');   
        var scoring = $('Scoring');
    } else
    {
        var folding = $(key); 
        var parts = key.split('^');
        var scoring = $('Scoring^'+parts[1]);
    }

    if ( (folding!=null) && (scoring!=null) )
    { 
        var selected_folding = folding.options[folding.selectedIndex].text;
        if (selected_folding!='No Folding')
        {
            scoring.options[0].setAttribute('disabled', 'disabled');
            if (scoring.selectedIndex==0) scoring.selectedIndex = 1;
        } else
        {
            scoring.options[0].removeAttribute('disabled');
        }
        
        scoring.onchange();
    }
}

function urlencode( str ) 
{
                          
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function ccNumKeyPress(e)
{
    if (window.event) 
    {
        if ( (window.event.keyCode == 8) || (window.event.keyCode == 127) ) return true; 
    }  else
    {
        if ( (e.which == 8) || (e.which == 127) ) return true;
    }
    
    return (this.getAttribute('my_maxlength') > this.value.length ); 
}

function check_type( cardnumber )
{ 
    var cardtype = "UNKNOWN";

    len = cardnumber.length;
    if     ( len == 15 && cardnumber.substr(0, 1) == '3' )                 { cardtype = "amex"; }
    else if ( len == 16 && cardnumber.substr(0, 4) == '6011' )              { cardtype = "discover"; }
    else if ( len == 16 && cardnumber.substr(0, 1) == '5'  )                { cardtype = "mc"; }
    else if ( (len == 16 || len == 13) && cardnumber.substr(0, 1) == '4' ) { cardtype = "visa"; }

    return ( cardtype );
}
