function toggleID(id) {
	$('#'+id).toggle('fast');
}
function showOrderForm() {
    $('#orderNow').center();
    $('#orderNow').show();
}

function closeOrderForm() {
    $('#orderNow').hide();
}

function addAddress(div) {
    var id = document.getElementById('id').value;

    $("#"+div).append("<div style=\"margin-bottom: 2px;\" id='address" + id + "'>" +
      
      "<hr />" +
      "<label>Company:</label> <input name=\"data[address][" + id + "][company]\"><br />" +
      "<label>Address1:</label> <input name=\"data[address][" + id + "][address1]\"><br />" +
      "<label>Address2:</label> <input name=\"data[address][" + id + "][address2]\"><br />" +
      "<label>City:</label> <input name=\"data[address][" + id + "][city]\"><br />" +
      "<label>State:</label> <input name=\"data[address][" + id + "][state]\"><br />" +
      "<label>Zip:</label> <input name=\"data[address][" + id + "][zip]\"><br />" +
      "<label>Service:</label> <input name=\"data[address][" + id + "][service]\"><br />" +
      "<label>Bandwidth:</label> <input name=\"data[address][" + id + "][bandwidth]\"><br />" +
      
                        "<div><a href='#' onClick='removeAddress(\"#address" + id + "\"); return false;'><span style=\"width: 24px; margin-right: 4px;\"><img border=\"0\" src=\"/images/trashcan.gif\">d</span></a></div></div>");


//    $("#address" + id).animate({opacity: 0}, function(){$("#address" + id).animate({opacity: 1});});

    id = (id - 1) + 2;
    document.getElementById('id').value = id;


}

  function removeAddress(id) {
    //$(id).animate({opacity: 0}, "normal", "", function(){$(id).remove();});
    $(id).remove();
  }
  
  

function addAVAddress(div) {
    var id = document.getElementById('id').value;
    
    $("#"+div).append("<div style=\"margin-bottom: 2px;\" id='address" + id + "'>" +
                      "<div><a href='#' onClick='removeAVAddress(\"#address" + id + "\"); return false;'><span style=\"width: 24px; margin-right: 4px;\"><img border=\"0\" src=\"/images/trashcan.gif\">Remove Address</span></a></div><input class=\"av_input_l\" name=\"data[address][" + id + "][address1]\" value=\"Address\" onfocus=\"if(this.value=='Address'){this.value=''}\" onblur=\"if(this.value==''){this.value='Address'};\" /><br />" +
                      "<input class=\"av_input\" name=\"data[address][" + id + "][city]\" value=\"City\" onfocus=\"if(this.value=='City'){this.value=''}\" onblur=\"if(this.value==''){this.value='City'};\" /> <input class=\"av_input_ss\" name=\"data[address][" + id + "][state]\" value=\"State\" onfocus=\"if(this.value=='State'){this.value=''}\" onblur=\"if(this.value==''){this.value='State'};\" /> <input class=\"av_input_s\" name=\"data[address][" + id + "][zip]\" value=\"Zip\" onfocus=\"if(this.value=='Zip'){this.value=''}\" onblur=\"if(this.value==''){this.value='Zip'};\" /><br />" +
                      "</div>");


/*    $("#"+div).append("<div style=\"margin-bottom: 2px;\" id='address" + id + "'>" +
      
      "<hr />" +
      "<label>Company:</label> <input name=\"data[address][" + id + "][company]\"><br />" +
      "<label>Address1:</label> <input name=\"data[address][" + id + "][address1]\"><br />" +
      "<label>Address2:</label> <input name=\"data[address][" + id + "][address2]\"><br />" +
      "<label>City:</label> <input name=\"data[address][" + id + "][city]\"><br />" +
      "<label>State:</label> <input name=\"data[address][" + id + "][state]\"><br />" +
      "<label>Zip:</label> <input name=\"data[address][" + id + "][zip]\"><br />" +
      "<label>Service:</label> <input name=\"data[address][" + id + "][service]\"><br />" +
      "<label>Bandwidth:</label> <input name=\"data[address][" + id + "][bandwidth]\"><br />" +
      
                        "<div><a href='#' onClick='removeAVAddress(\"#address" + id + "\"); return false;'><span style=\"width: 24px; margin-right: 4px;\"><img border=\"0\" src=\"/images/trashcan.gif\">d</span></a></div></div>");
*/

//    $("#address" + id).animate({opacity: 0}, function(){$("#address" + id).animate({opacity: 1});});

    id = (id - 1) + 2;
    document.getElementById('id').value = id;


}

  function removeAVAddress(id) {
    //$(id).animate({opacity: 0}, "normal", "", function(){$(id).remove();});
    $(id).remove();
  }



  function addFormField(str, val) {
    var id = document.getElementById('id').value;
    
    $.get("/backstage/admin/orders/?action=ajaxGetProduct&sku=" + str, function(data){
      
      msgs = PHP_Unserialize(data);
      
      if (msgs['sku']) {
                   
        if (msgs['downloadable']) {
          //dl = 'Yes';
          
          dl = '<select name="item' + id + 'DL">' +
                  '<option value=0>No</option>' + 
                  '<option value=1 selected="selected">Yes</option>' + 
                  '</select>';
                  
        } else {
          //dl = 'No';
          dl = '<select name="item' + id + 'DL">' +
                  '<option value=0 selected="selected">No</option>' + 
                  '<option value=1>Yes</option>' + 
                  '</select>';
        }
        if (msgs['shipped']) {
          //dl = 'Yes';
          
          SHIPPED = '<select name="item' + id + 'SHIPPED">' +
                  '<option value=0>No</option>' + 
                  '<option value=1 selected="selected">Yes</option>' + 
                  '</select>';
                  
        } else {
          //dl = 'No';
          SHIPPED = '<select name="item' + id + 'SHIPPED">' +
                  '<option value=0 selected="selected">No</option>' + 
                  '<option value=1>Yes</option>' + 
                  '</select>';
        }

				if (val) {
					PRICE = val;
				} else {
					PRICE = msgs['price'];
				}
        $("#divTxt").append("<div style=\"margin-bottom: 2px;\" id='row" + id + "'>" +
                            "<div style=\"float: right; width: 80px;\">$ <input size=6 name=\"item" + id + "Price\" value=\"" + PRICE + "\"></div>" +
                            "<div style=\"float: right; width: 120px;\">" + dl + "</div>" +
                            "<div style=\"float: right; width: 120px;\">" + SHIPPED + "</div>" +
                            "<div style=\"width: 60px; float: right;\"><input size=2 name=\"item" + id + "Qty\" value=\"1\"></div>" +
                            "<div><input type=hidden name=\"item" + id + "\" value=\"" + msgs['sku'] + "\"><a href='#' onClick='removeFormField(\"#row" + id + "\"); return false;'><span style=\"width: 24px; margin-right: 4px;\"><img border=\"0\" src=\"/images/trashcan.gif\"></span></a>" + msgs['sku'] + ' - ' + msgs['name'] + "</div></div>");
      }
      // fader if deisred    
      $("#row" + id).animate({opacity: 0}, function(){$("#row" + id).animate({opacity: 1});});
      
      //updateTotals();
      
    });

    id = (id - 1) + 2;
    document.getElementById('id').value = id;
  }



    function listSignup(frm) {
        $('#'+frm+' .errFN').html('').hide();
        $('#'+frm+' .errEm').html('').hide();

        var jqxhr = $.post("/contact/signup", $("#"+frm).serialize(), function(data) {
            
        if (data.success == 'false') {
            if (data.err) {
                if(data.err.fullname) {
                    $('#'+frm+' .errFN').html(data.err.fullname).show();
                }
                if(data.err.email) {
                    $('#'+frm+' .errEm').html(data.err.email).show();
                }
            }
        } else if (data.success == 'true') {
                
                if (data.output) {
                    $('#'+frm).html('<h3>Thank you</h3> '+data.output);
                } else {
                    $('#'+frm).html('<h3>Thank you</h3>');
                }

        }
        }, 'json');
    }
    
    
    
    
function showAVForm() {
    $('#av').center();
    $('#av').show();
}

function closeAVForm() {
    $('#av').hide();
}

/*
 * Natural Sort algorithm for Javascript - Version 0.6 - Released under MIT license
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
 */
function naturalSort (a, b) {
	var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
		sre = /(^[ ]*|[ ]*$)/g,
		dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
		hre = /^0x[0-9a-f]+$/i,
		ore = /^0/,
		// convert all to strings and trim()
		x = a.toString().replace(sre, '') || '',
		y = b.toString().replace(sre, '') || '',
		// chunk/tokenize
		xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
		yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
		// numeric, hex or date detection
		xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
		yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null;
	// first try and sort Hex codes or Dates
	if (yD)
		if ( xD < yD ) return -1;
		else if ( xD > yD )	return 1;
	// natural sorting through split numeric strings and default strings
	for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
		// find floats not starting with '0', string or 0 if not defined (Clint Priest)
		oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
		oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
		// handle numeric vs string comparison - number < string - (Kyle Adams)
		if (isNaN(oFxNcL) !== isNaN(oFyNcL)) return (isNaN(oFxNcL)) ? 1 : -1; 
		// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
		else if (typeof oFxNcL !== typeof oFyNcL) {
			oFxNcL += ''; 
			oFyNcL += ''; 
		}
		if (oFxNcL < oFyNcL) return -1;
		if (oFxNcL > oFyNcL) return 1;
	}
	return 0;
}

jQuery.fn.dataTableExt.oSort['natural-asc']  = function(a,b) {
    return naturalSort(a,b);
};

jQuery.fn.dataTableExt.oSort['natural-desc']  = function(a,b) {
    return naturalSort(b,a);
};

jQuery.fn.dataTableExt.oSort['formatted-num-asc'] = function(x,y){
 x = x.replace(/[^\d\-\.\/]/g,'');
 y = y.replace(/[^\d\-\.\/]/g,'');
 if(x.indexOf('/')>=0)x = eval(x);
 if(y.indexOf('/')>=0)y = eval(y);
 return x/1 - y/1;
}
jQuery.fn.dataTableExt.oSort['formatted-num-desc'] = function(x,y){
 x = x.replace(/[^\d\-\.\/]/g,'');
 y = y.replace(/[^\d\-\.\/]/g,'');
 if(x.indexOf('/')>=0)x = eval(x);
 if(y.indexOf('/')>=0)y = eval(y);
 return y/1 - x/1;
}
