﻿
$(document).ready(function(){
    //08-01-08 avc >> added check so it doenst bomb when this is included
    //and a control may not have the element...
    if($('#ptype').length > 0)
    {
        getFamilyListType3(document.getElementById('ptype').value)
    }
});
var i=1;
var ShowThreadKit=false;
function selectBoxChange(control) 
{
    getFamilyListType3(document.getElementById(control.id).value);
    getProductImageName(document.getElementById(control.id).value);
}

function getFamilyListType3(pid) 
{
    getFamilyListType7(pid);
    
    var retF = '';
    var output = '';
    var usedSalePrice = false;
    var numOfColumns = 4;
    var retB = '</tbody>';
    retB += '</table>';
    
    
    
    $.ajax({
        url: '/webservices/product.asmx/ViewFamilyInformation?pk_pid=' + pid + '&familytype=3',
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            document.getElementById("relatedList").innerHTML='<strong>There was a error displaying the related "for this" information.  This error appears for testing purposes.</strong>';
        },
        success: function(xml){
            document.getElementById("relatedList").innerHTML='';
            $(xml).find('NewDataSet').find('ProductFamily').each(function(){
                var item_pid = $('PK_PID', this).text();
                var item_sku = $('SKU', this).text();
                var item_designator = $('Designator', this).text();
                var item_name = $('Name', this).text();
                var item_saleprice = $('SalePrice', this).text();
                var item_price = $('RegularPrice', this).text();
                var item_retailprice = $('SuggestedRetail', this).text();
                var item_url = $('SEO_URL', this).text();
                
	            output += '<tr class=\"odd\">';
                output += '<td><a href="/product/' + item_url + '.aspx">' + item_name + '</a></td>';
                if (item_price=='')
                {
                    output += '<td></td>' + Environment.NewLine;
                }
                else
                {
                    output += '<td>' + formatCurrency(item_price) + '</td>';
                }
                if (item_saleprice!='')
                {
                    if (parseInt(item_saleprice) > 0)
                    {
                        usedSalePrice = true;
                        retVal += '<td><span class="SalePrice">' + formatCurrency(item_price) + '</span></td>';
                    }
                }
                output += '<td>' + item_designator + item_sku + '</td>';
                output += '<td align=right>Qty: <input type="text" name="txtRelatedQty'+i+'" + size="2" /></td>';
                output += '<input type="hidden" name="txtProductId'+i+'" value="' + item_pid + '" />';
                output += '</tr>';
                i=i+1;
            });
            
            if (usedSalePrice) numOfColumns = 5;
            retF = '<i><b>Additional Compliments</b></i>';
            retF += '<table id="ForThisProd" width="100%" cellpadding="4" cellspacing="0">';
            retF += '<tbody>';
            /*
            retF += '<tr class="head">';
            retF += '<td colspan="' + numOfColumns + '"><i>Additional Compliments</i></td>';
            retF += '</tr>';
            */
            /*
            retF += '<tr class="subhead">';
            retF += '<td>Item Name</td>';
            retF += '<td>Our Price</td>';
            if (usedSalePrice) retF += '<td>Sale Price</td>';
            retF += '<td>SKU</td>';
            retF += '<td>Qty.</td>';
            retF += '</tr>';
            */
            if (output!='') output = retF + output + retB;
            //document.getElementById('txtJSInfo').value = output;
            $('<div></div>')
                    .html(output)
                    .appendTo('#relatedList');
        }
    });
}

function getFamilyListType7(pid) 
{
    var retF = '';
    var output = '';
    var usedSalePrice = false;
    var numOfColumns = 4;
    var retB = '</tbody>';
    retB += '</table>';
    var last='even';
    ShowThreadKit=false;
    $.ajax({
        url: '/webservices/product.asmx/ViewFamilyInformation?pk_pid=' + pid + '&familytype=7',
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            document.getElementById("relatedOptions7").innerHTML='<strong>There was a error displaying the related "for this" information.  This error appears for testing purposes.</strong>';
        },
        success: function(xml){
            resetDivs();
            document.getElementById("relatedOptions7").innerHTML='';
            
            $(xml).find('NewDataSet').find('ProductFamily').each(function(){
                var item_pid = $('PK_PID', this).text();
                var item_sku = $('SKU', this).text();
                var item_designator = $('Designator', this).text();
                var item_name = $('Name', this).text();
                var item_saleprice = $('SalePrice', this).text();
                var item_price = $('RegularPrice', this).text();
                var item_retailprice = $('SuggestedRetail', this).text();
                var item_url = $('SEO_URL', this).text();
                if(last=='even')
                {
	                output += '<tr class=\"odd\">';
                    last="odd"
	            } else {
	                output += '<tr class=\"even\">';
                    last="even"
	            }
	            output += '<td>' + item_name + '</td>';
	            output += '<td class="borderright"><table border="0" cellpadding="0" cellspacing="2" width="100%">';
                output += '<tr>';
                output += '<td>' + item_designator + item_sku + '</td>';
//                output += '</tr>';
//                output += '<tr>';
                output += '<TKER>';
                if (item_saleprice!='')
                {
                    if (parseInt(item_saleprice) > 0)
                    {
                        usedSalePrice = true;
                        retVal += '<td><span class="SalePrice">' + formatCurrency(item_saleprice) + '</span></td>';
                    } 
                    else if (item_price=='')
                    {
                        output += '<td></td>' + Environment.NewLine;
                    }
                    else
                    {
                        output += '<td>' + formatCurrency(item_price) + '</td>';
                    }
                } else if (item_price=='')
                {
                    output += '<td></td>' + Environment.NewLine;
                }
                else
                {
                    output += '<td>' + formatCurrency(item_price) + '</td>';
                }
                output += '<td align="right">'
                output += '<td align=right>Qty: <input type="text" name="txtRelatedQty'+i+'" size="2" />';
                output += '<input type="hidden" name="txtProductId'+i+'" value="' + item_pid + '" /></td>';
                output += '</tr>'
                output += '</table></td>'
                
                i=i+1;
                output += AttachThreadKit(item_pid);
                output += '</tr>';
            });
            
            if (usedSalePrice) numOfColumns = 5;
            //retF = '<i><b>Finished Size</b></i>'
            retF += '<table id="ForThisProd" width="100%" cellpadding="4" cellspacing="0">';
            retF += '<tbody>';
            /*
            retF += '<tr class="head">';
            retF += '<td colspan="' + numOfColumns + '"><i><b>Finished Size</b></i></td>';
            retF += '</tr>';
            */
            /*
            retF += '<tr class="head">';
            retF += '<td>Finished Size</td>';
            retF += '<td>SKU</td>';
            retF += '<td>Price</td>';
            if (usedSalePrice) retF += '<td>Sale Price</td>';
            retF += '<td></td>';
            if (usedSalePrice)
            {
                retF += '<td colspan=4>DMC Thread Kit</td>';
            } else {
                retF += '<td colspan=3>DMC Thread Kit</td>';
            }
            retF += '</tr>';
            */
            retF += '<tr class="head">';
            if (usedSalePrice)
            {
                retF += '<td colspan="2"><i><b>Finished Size</b></i></td>';
                if(ShowThreadKit)
                {
                    retF += '<td colspan="4" align="center"><i><b>DMC Thread Kit</b></i></td>';
                    var regEx = new RegExp ('<TKER>', 'gi') ;
                    output = output.replace(regEx,'</tr><tr>')
                } else {
                    var regEx = new RegExp ('<TKER>', 'gi') ;
                    output = output.replace(regEx,'')
                }
                
            } else {
                retF += '<td colspan="2"><i><b>Finished Size</b></i></td>';
                if(ShowThreadKit)
                {
                    retF += '<td colspan="4" align="center"><i><b>DMC Thread Kit</b></i></td>';
                    var regEx = new RegExp ('<TKER>', 'gi') ;
                    output = output.replace(regEx,'</tr><tr>')
                } else {
                    var regEx = new RegExp ('<TKER>', 'gi') ;
                    output = output.replace(regEx,'')
                }
            }
            retF += '</tr>';
            
            if (output!='') output = retF + output + retB;
            //document.getElementById('txtJSInfo').value = output;
            $('<div></div>')
                    .html(output)
                    .appendTo('#relatedOptions7');
        }
    });
}


function AttachThreadKit(pid) 
{
   
    var output = '';
    var usedSalePrice = false;
    var numOfColumns = 4;
    
    
    $.ajax({
        url: '/webservices/product.asmx/ViewFamilyInformation?pk_pid=' + pid + '&familytype=8',
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
		async: false,
        error: function(){
            //document.getElementById("relatedOptions7").innerHTML='<strong>There was a error displaying the related "for this" information. (type 8) This error appears for testing purposes.</strong>';
            return "";
        },
        success: function(xml){
            resetDivs();
            
            $(xml).find('NewDataSet').find('ProductFamily').each(function(){
                var item_pid = $('PK_PID', this).text();
                var item_sku = $('SKU', this).text();
                var item_designator = $('Designator', this).text();
                var item_name = $('Name', this).text();
                var item_saleprice = $('SalePrice', this).text();
                var item_price = $('RegularPrice', this).text();
                var item_retailprice = $('SuggestedRetail', this).text();
                var item_url = $('SEO_URL', this).text();
                
                output += '<td><table border="0" cellpadding="0" cellspacing="2">';
                output += '<tr>';
                output += '<td>' + item_designator + item_sku + '</td>';
                output += '</tr>';
                output += '<tr>';
                if (item_saleprice!='')
                {
                    if (parseInt(item_saleprice) > 0)
                    {
                        usedSalePrice = true;
                        retVal += '<td><span class="SalePrice">' + formatCurrency(item_saleprice) + '</span></td>';
                    } 
                    else if (item_price=='')
                    {
                        output += '<td></td>' + Environment.NewLine;
                    }
                    else
                    {
                        output += '<td>' + formatCurrency(item_price) + '</td>';
                    }
                } else if (item_price=='')
                {
                    output += '<td></td>' + Environment.NewLine;
                }
                else
                {
                    output += '<td>' + formatCurrency(item_price) + '</td>';
                }
                output += '<td align=right>Qty: <input type="text" name="txtRelatedQty'+i+'" size="2" />';
                output += '<input type="hidden" name="txtProductId'+i+'" value="' + item_pid + '" /></td>';
                output += '</tr></table></td>';
                ShowThreadKit=true;
                i=i+1;
            });
            
            if (usedSalePrice) numOfColumns = 5;
            
            //document.getElementById('txtJSInfo').value += output;
            
            
        }
    });
	return output;
}


/**********
****  Removed 7/28/08 RLL per Jeff changing to grid as Alan suggested :)
***********
function getFamilyListType7(pid) 
{
    var output = '<select id="ddRelatedOption" name="ddRelatedOption" onchange="getProductInformationByControl(this)">';
    var usedSalePrice = false;
    var i = 0;
    var first = true;
    
    $.ajax({
        url: '/webservices/product.asmx/ViewFamilyInformation?pk_pid=' + pid + '&familytype=7',
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            document.getElementById("relatedOptions7").innerHTML='<strong>There was a error displaying the related "drop down type 7" information.  This error appears for testing purposes.</strong>';
        },
        success: function(xml){
            resetDivs();
            $(xml).find('ProductFamily').each(function(){
                var item_pid = $('PK_PID', this).text();
                var item_name = $('Name', this).text();
                if(first) { getProductInformation(item_pid); first = false; }
	            output += '<option value="' + item_pid + '">' + item_name + '</option>';
	            i++;
            });
            output += '</select>';
            if(i==0) output = '';
            $('<div></div>')
                    .html(output)
                    .appendTo('#relatedOptions7');
        }
    });
}
*/

function getProductInformationByControl(control) 
{
    var pid = document.getElementById(control.id).value;
    getProductInformation(pid);
}

function getProductInformation(pid) 
{
    var usedSalePrice = false;
    
    $.ajax({
        url: '/webservices/product.asmx/ViewProductInformation?pk_pid=' + pid,
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            document.getElementById("pricing").innerHTML='<strong>There was a error displaying the price information.  This error appears for testing purposes.</strong>';
        },
        success: function(xml){
            //document.getElementById("pricing").innerHTML='';
            $(xml).find('Detail').each(function(){
                var item_saleprice = $('SalePrice', this).text();
                var item_price = $('RegularPrice', this).text();
                var item_retailprice = $('SuggestedRetail', this).text();
                
	            document.getElementById("pricing").innerHTML='Our Price: ' + formatCurrency(item_price);
            });
        }
    });
}

function resetDivs() {
    //document.getElementById("pricing").innerHTML='';
    //08-01-08 avc >> updated to the jquery way... :)
    //document.getElementById("relatedOptions7").innerHTML='';
    $('#relatedOptions7').html('');
}
function formatCurrency(num){num=num.toString().replace(/\$|\,/g,'');if(isNaN(num))num="0";sign=(num==(num=Math.abs(num)));num=Math.floor(num*100+0.50000000001);cents=num%100;num=Math.floor(num/100).toString();if(cents<10)cents="0"+cents;for(var i=0;i<Math.floor((num.length-(1+i))/3);i++)num=num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));return(((sign)?'':'-')+'$'+num+'.'+cents)}

//08-01-08 avc >> grab product information and allows user to
//specify the callback function upon success/error. This allows
//you to genrically use this to grab product details and then
//call a custom function to process the returned data which should be
//on the control that has the html. NOTE: could also make the url a
//param...
function GrabProdInfo(pid, myCallBack) 
{
    $.ajax({
        url: '/webservices/product.asmx/ViewProductInformation?pk_pid=' + pid,
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            myCallBack('ERROR<strong>There was a error displaying the product information.  This error appears for testing purposes.</strong>');
        },
        success: function(xml){
            myCallBack(xml);
        }
    });
}

//08-01-08 avc >> grab family information and allows user to
//specify the callback function upon success/error. This allows
//you to genrically use this to grab product details and then
//call a custom function to process the returned data which should be
//on the control that has the html.
function GrabFamilyInfo(pid, type, myCallBack) 
{
    $.ajax({
        url: '/webservices/product.asmx/ViewFamilyInformation?pk_pid=' + pid + '&familytype=' + type,
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            myCallBack('ERROR<strong>There was a error displaying the color options.  This error appears for testing purposes.</strong>');
        },
        success: function(xml){
            myCallBack(xml);
        }
    });
}

//08-01-08 avc >> grab parent id information and allows user to
//specify the callback function upon success/error. This allows
//you to genrically use this to grab product details and then
//call a custom function to process the returned data which should be
//on the control that has the html.
function GrabParentId(pid, isAsync, myCallBack) 
{    
    $.ajax({
        url: '/webservices/product.asmx/GetParentId?pk_pid=' + pid,
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        async: isAsync,
        error: function(){
            myCallBack('ERROR<strong>There was an error getting the parent information.  This error appears for testing purposes.</strong>');
        },
        success: function(xml){
            myCallBack(xml);
        }
    });
}