/* New version of page */

function changeOption( value )
{
    option = value;
    
    if( $("cp_image") != undefined )
    {
        $("cp_image").style.width = "auto";
        $("cp_image").src = '/resources/images/cp_cust.gif';    
    }
    
    $("shape_id").options[0].selected = true;
    $("corners").options[0].selected = true;    
    $("label_id").options[0].selected = true;
    
    $("width").options[0].selected = true;
    $("height").options[0].selected = true;    
    $("bleed_yes").checked = false;
    $("bleed_no").checked = true;
    
    $("material_id").options[0].selected = true;
    
    if( $("label_qty") != undefined )
        $("label_qty").value = "";
        
    if( $("label_qty").options != undefined )
        $("label_qty").options[0].selected = true;        
        
    $("rush_id").options[0].selected = true;
    
    $("proof_yes").checked = false;
    $("proof_no").checked = true;
    
    if( option == "standard" )
    {
        $('standard_opt').style.display = "block";
        $('custom_opt').style.display   = "none";
    }
    
    if( option == "custom" )
    {
        $('standard_opt').style.display = "none";
        $('custom_opt').style.display   = "block";
    }
    
    if( $("price_labels") != undefined )
    {
        $( "price_labels").innerHTML = '0.00';
        $( "price_label" ).innerHTML = '0.00';
    }
    
    //doCalc( option );
    
    if( $('cp_artwork') != undefined )
        reloadArtwork( 'standard' );

    return false;
}


function changeCustomSize()
{
    customCalc();
    
    if( $("width").value > 0 && $("height").value > 0 )
    {
        checkBleedCust();
        if( $("cp_image") != undefined )
        {
            $("cp_image").style.width = "auto";
            $("cp_image").src = "/printing/label.php?width=" + $("width").value + "&height=" + $("height").value;
        }
    }
    
    url = '/printing/ajax.php?mode=get_material_list&width=' + $("width").value + "&height=" + $("height").value + '&selected=' + $('material_id').value;
    loadXMLDoc2( url );    
        
    if( $('cp_artwork') != undefined )
        reloadArtwork( 'custom' );
}

function getImage( value )
{
    value = value.split( "_" ); value = value[0];
    var url =  '/printing/ajax.php?mode=get_label_image&label_id=' + value;
    loadXMLDoc4( url );
}

function getImageResponse( empty , response )
{
    if( $("cp_image") == undefined )
        return;
    
    if( response )
        $( "cp_image" ).src = response;
    else
        $( "cp_image" ).src = "/resources/printing/cp_cust.gif";
        
    $( "cp_image" ).style.width = "153px";
}

function setLabelsList( area )
{
    var shape_id = $('shape_id').value;
    
    if( shape_id == 1 || shape_id == 5 )
    {
        $('corners').disabled = true;
        var corners = 0;
    }
    else
    {
        $('corners').disabled = false;
        var corners = $('corners').value;
    }
    
    url = '/printing/ajax.php?mode=get_labels_list&shape_id=' + shape_id + '&corners=' + corners + '&selected=' + $('label_id').value + '&area=' + area; //prompt( "" , url );
    loadXMLDoc3( url );     
}

function setLabelsListResponse( empty , response )
{
    var aData = eval('(' + response + ')');
    var select = $('label_id');
    
    if ( select.hasChildNodes() )
        while ( select.childNodes.length >= 1)
            select.removeChild( select.firstChild );
            
    var tmpElement = document.createElement('option');
        tmpElement.value = "";
        tmpElement.text  = "choose..."; 
            
    select.options.add( tmpElement );          
       
    var selected = 0;
       
    for( elem in aData['items'] )
    {
        tmpElement = document.createElement('option');
        tmpElement.value = elem;
        tmpElement.text  = aData['items'][elem];
            
        if( elem == aData['selected'] )
        {
            //alert( elem );
            tmpElement.selected = "selected";
            selected = 1;
        }

        select.options.add( tmpElement );
    }
    
    if( selected == 0 && $("cp_image") != undefined )
        $("cp_image").src = '/resources/images/cp_cust.gif';
}

function setMaterialsList()
{
    url = '/printing/ajax.php?mode=get_material_list&label_id=' + $('label_id').value + '&selected=' + $('material_id').value;
    loadXMLDoc2( url );    
}

function setMaterialsListResponse( empty , response )
{
    var aData = eval('(' + response + ')');
    var select = $('material_id');
    
    if ( select.hasChildNodes() )
        while ( select.childNodes.length >= 1)
            select.removeChild( select.firstChild );
            
    var tmpElement = document.createElement('option');
        tmpElement.value = "";
        tmpElement.text  = "choose..."; 
            
    select.options.add( tmpElement );          
       
    for( elem in aData['items'] )
    {
        tmpElement = document.createElement('option');
        tmpElement.value = elem;
        tmpElement.text  = aData['items'][elem];
            
        if( elem == aData['selected'] )
            tmpElement.selected = "selected";

        select.options.add( tmpElement );
    }
    
    if( option == 'standard' ) standardCalc();
    if( option == 'custom' )   customCalc();    
}

function changeMaterial( option , value )
{
    checkNoColorPrint( value , 'cpna' );
    if( option == 'standard' ) standardCalc();
    if( option == 'custom' )   customCalc();
}

function doCalc( option )
{
    if( option == 'standard' ) standardCalc();
    if( option == 'custom' )   customCalc();
}

function standardCalc( mode )
{
    if( $('product_info') != undefined )
        $('product_info').innerHTML = "";
    
    if( $("price_labels") != undefined )
    {        
        $( "price_labels").innerHTML = '0.00';
        $( "price_label" ).innerHTML = '0.00';
    }
        
    if( !$("label_id").value )    return false;
    if( !$("material_id").value ) return false;
    if( !$("label_qty").value )   return false;

    if( $("cp_loading") != undefined )
        $("cp_loading").style.display = 'inline';
    
    if( $( "standard_nbsp" ) != undefined  ) 
        $( "standard_nbsp" ).style.display    = 'none';   
    
    var label_qty   = $("label_qty").value;  
    var material_id = $("material_id").value; material_id = material_id.split( "_" ); material_id = material_id[0];
    var label_id    = $("label_id").value;    label_id    = label_id.split( "_" ); label_id = label_id[0];
    //var bleed       = $("bleed_yes").checked;
    
    if( $("proof_yes") != undefined  )
        var proof       = $("proof_yes").checked ? 1 : 0;
    else
        var proof        = 0;
        
        
    var bleed       = 0;

    url =  '/printing/ajax.php?mode=standard_calc&label_id=' + label_id + '&proof=' + proof +
                          '&material_id=' + material_id + '&label_qty=' + label_qty + '&bleed=' + bleed + '&rush_id=' + $("rush_id").value;

    //prompt( "" , url );
    
    loadXMLDoc( url ); 
}

function standardCalcResponse( empty , response )
{
    response = response.split( "_" );
    $("price_labels").innerHTML = response[0];
    $("price_label").innerHTML  = response[1];
    
    if( $("cp_loading") != undefined )
        $("cp_loading").style.display = 'none';
        
    if( $("standard_nbsp") != undefined  ) 
        $("standard_nbsp").style.display = 'inline';
        
    if( $('product_info') != undefined )
        $('product_info').innerHTML = $("label_qty").value + ' Printed Labels ' + getSelectedText("label_id") + "<br />" + getSelectedText("material_id") +
                                  "<br />" + getSelectedText("rush_id");
                                  
    if( $('proof_yes').checked )
        $('product_info').innerHTML = $('product_info').innerHTML + ' Hardcopy Proof';
}


function checkNoColorPrint( select , note_id )
{
    value = select.split( "_" ); value = value[1];
    
    if( value > 0 )
        $(note_id).style.display = "block";
    else
        $(note_id).style.display = "none";
}

function checkRushNote( select , note_id )
{
    for( i = 0; i <= select.nextSibling.childNodes.length ; i++ )
        if( select.nextSibling.childNodes[i] != undefined )
            $(select.nextSibling.childNodes[i].id).style.display = "none";
    
    if( $(note_id) )
        $(note_id).style.display = "block";
}


function checkBleedStnd( value )
{
    return false;
    
    value = value.split( "_" ); value = value[1];
    
    if( value > 0 )
    {
        $( "bleed_yes" ).disabled = false;
        $( "bleed_no" ).checked   = false;
        $( "bleed_yes_note" ).style.display = 'block';
        $( "bleed_no_note" ).style.display  = 'none';
    }
    else
    {
    	$( "bleed_yes" ).disabled = true;
        $( "bleed_no" ).checked   = true;
        $( "bleed_yes_note" ).style.display = 'none';
        $( "bleed_no_note" ).style.display  = 'block';        
    }
        
}

function checkBleedCust()
{
    var width       = $("width").value;
    var height      = $("height").value;
    
    if( width == 8.5 && height >= 10.5 )
    {
    	$( "bleed_yes" ).disabled = true;
        $( "bleed_no" ).checked   = true;
    }
    else
    {
        $( "bleed_yes" ).disabled = false;
        $( "bleed_no" ).checked   = false;
    }
}

function customCalc( reload )
{
    if( $('product_info') != undefined )    
        $('product_info').innerHTML = "";
    
    if( !$("width").value )       return false;
    if( !$("height").value )      return false;
    
    $( "price_labels" ).innerHTML = '0.00';
    $( "price_label" ).innerHTML  = '0.00';    

    if( $("proof_yes") != undefined )
        var proof       = $("proof_yes").checked ? 1 : 0;
    else
        var proof        = 0;

    
    if( $( "cp_nbsp" ) != undefined  ) 
        $( "cp_nbsp" ).style.display = 'none';
    
    var width       = $("width").value;
    var height      = $("height").value;
    var label_qty   = $("label_qty").value;  
    var material_id = $("material_id").value; material_id = material_id.split( "_" ); material_id = material_id[0];
    var bleed       = $("bleed_yes").checked;
    
    if( !width || !height || !label_qty || !material_id )
        return false;
    
    if( $("cp_loading") != undefined )
        $( "cp_loading" ).style.display = 'inline';
    
    var url         =  '/printing/ajax.php?mode=custom_calc&width=' + width + '&height=' + height + '&proof=' + proof + 
                       '&material_id=' + material_id + '&label_qty=' + label_qty + '&bleed=' + bleed + '&rush_id=' + $("rush_id").value + '&reload=' + reload;

    //prompt( "" , url );
    
    loadXMLDoc( url );
}

function customCalcResponse( empty , response )
{
    response = response.split( "_" );
    $( "price_labels" ).innerHTML = response[0];
    $( "price_label" ).innerHTML  = response[1];
    
    reload = response[2];
    
    if( $('cp_artwork') && reload == 'reload' )
        reloadArtwork( 'custom' )
    
    if( $("cp_loading") != undefined )        
        $( "cp_loading" ).style.display = 'none';

    if( $( "cp_nbsp" ) != undefined )
        $( "cp_nbsp" ).style.display = 'inline';
        
    if( $('product_info') != undefined )
    {
        $('product_info').innerHTML = $("label_qty").value + ' Printed Labels ' + $("width").value + '" X ' + $("height").value + '"';

        if( $("bleed_yes").checked )
            $('product_info').innerHTML = $('product_info').innerHTML + ' Bleed';
        
        $('product_info').innerHTML = $('product_info').innerHTML + '<br />' + getSelectedText("material_id") +
                                  "<br />" + getSelectedText("rush_id");
    }                                
                                  
    if( $('proof_yes').checked && $('product_info') != undefined )
        $('product_info').innerHTML = $('product_info').innerHTML + ' Hardcopy Proof';    
}

function placeOrder( option )
{
    if( option == 'custom' && hasEmptyFields( "height width c_material_id c_label_qty" , "Please complete the form." ) )
        return false;

    if( option == 'standard' && hasEmptyFields( "label_id material_id label_qty" , "Please complete the form." ) )
        return false;
    
    $( option + '_printing' ).submit();
        
    return false;
}

function uploadFile( option )
{
    if( option == 'standard' && $("label_id") && hasEmptyFields( "label_id" , "Please choose a label size first.") )
        return false;
        
    var file = document.getElementById("image").value.split(".");
    var ext = file[file.length-1].toLowerCase(); 

    if( ext != "pdf" && ext != "ai"   && ext != "psd"  && ext != "eps" &&
        ext != "jpg" && ext != "jpeg" && ext != "tiff" && ext != "tif" )
    {
        alert("At this time we only accept the following file types: .pdf, .ai, .psd, .eps, .jpg and .tiff\nPlease call us at 1.888.388.2502 if you have any questions or difficulty uploading.");
        return false;
    }
    
    document.getElementById("uploadForm").submit();
}

function checkAddToCart( option )
{
	if( $('cp_note_form') == undefined )
	{
		alert('Please upload your artwork first.');
		return false;
	}
		
    if( option == "standard" )
        if( hasEmptyFields('label_id material_id label_qty' , 'Please complete the form.' ) )
            return false;
        else
            return true;
            
    if( option == "custom" )
        if( hasEmptyFields('width height material_id label_qty' , 'Please complete the form.') )
            return false;
        else
            return true;            
            
    return false;
}

function reloadArtwork( option )
{
    if( $('cp_artwork') == undefined )
        return false;
    
    if( option == 'standard')
    {
        $('cp_artwork').src='/resources/images/loader.gif';
        url = '/printing/ajax.php?mode=create_image&option=standard';
        loadXMLDoc2( url );
    }
    
    if( option == 'custom')
    {
        $('cp_artwork').src='/resources/images/loader.gif';
        url = '/printing/ajax.php?mode=create_image&option=custom';
        loadXMLDoc2( url );
    }    
}

function reloadArtworkResponse( empty , response )
{
    $('cp_artwork').src='/printing/ajax.php?random=' + Math.random() + '&mode=show_image';
}