var selectedManu = "";
var leftHA = "";
var rightHA = "";

var manuArray = [];
var attrArray = [];
var selManuArray = [];
var styleArray = [];



	function getUrlVars(vble) {
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			vars[key] = value;
			});
		return vars[vble];
	}



function product_go(reqType, article) {
	$.ajaxSetup({async:false});
	jQuery.post('/hacontroller/getProduct',
			 {'reqType':reqType, 'article':article },

			 function( response ) {
				 	var rows = response.rows;
				 	for(var i =0; i< rows.length; i++) {
				 		var c = rows[i].cell;
				 		manuArray.push(new manu(c));
				 	}
				 	var tempA= new Array(0,"Channels","channels","Ch","<b>Channels</b> or bands are similar concepts that refer to sound frequencies or groups of frequencies that can be adjusted for your hearing loss. Generally, more channels/bands provide increased flexibility to fine-tune hearing aids for your hearing. While the most advanced hearing aids typically have the most channels, a greater number of channels does not necessarily equate to a better hearing aid.");
				 	attrArray.push(new feature(tempA));
				 	tempA = new Array(0,"Programs","programs","Pr","<b>Programs</b> are different settings or memories for different situations such as quiet places, noisy places, and telephone use. Advanced hearing aids can have memories that are automatic or a combination of both manual and automatic.");
				 	attrArray.push(new feature(tempA));
				 	jQuery.post('/hacontroller/getAttrs',{}, function (attrs) {
				 		var attr = attrs.rows;
					 	for(var j =0; j< attr.length; j++) {
					 		var a = attr[j].cell;
					 		attrArray.push(new feature(a));
					 	}
					 	loadStyles();
				 	},'json');
					jQuery('#hl').html(article);
					makeProduct();
					loadManus();
					
	
			 },'json');

}

function loadManus() {
	jQuery.post('/hacontroller/getCompare',
			 { },

			 function( response ) {
				 	var rows = response.rows;
				 	for(var i =0; i< rows.length; i++) {
				 		var c = rows[i].cell;
				 		selManuArray.push(new manu(c));
				 		
				 	}
					makeManuSelect();
	
			 },'json');
}

function loadStyles() {
	jQuery.post('/hacontroller/getStyles',
		 { },

		 function( response ) {
		 	var rows = response.rows;
		 	for(var i =0; i< rows.length; i++) {
		 		var c = rows[i].cell;
		 		styleArray.push(new styleStruct(c));
		 		dw_Tooltip.content_vars[c[4]] = c[6];
		 	}

		 },'json');
}

function goforit() {
	
}

function checkAttr(attr, attributes) {
	if (attr=="") {
		return(false);
	}
	var haystack = attributes.split(",");

	var res = jQuery.inArray(attr,haystack);
	if ( res >= 0) {
		return(true);
	}
 
	return (false);
}

function hasAttrOption(attr) {
	return(checkAttr(attr,this.optional));
}

function hasAttrStandard(attr) {
	return(checkAttr(attr,this.standard));
}

function feature(cell) {
	this.id = cell[0];
	this.name = cell[1];
	this.slug = cell[2];
	this.abbr = cell[3];
	this.tooltip = cell[4];

}

function styleStruct(cell) {
	this.id = cell[0];
	this.name = cell[1];
	this.slug = cell[2];
	this.abbr = cell[4];
	this.tooltip = cell[6];
	this.fullname = cell[2];
	this.hidden = cell[5];
}

//0a.id, 1a.manu, 2a.name,3a.slug, 4a.styles,5a.channels, 6a.programs,7a.standard,8a.optional ,9a.price,10a.stockable, 11.photo_full, 12.photo_thumb, 13.description, 14display_order
	function manu (cell ) {
		this.isStocked = cell[10];
		this.manufacturer = cell[1];
		this.model = cell[2];
		this.channels = cell[5];
		this.programs = cell[6];
		this.slug = cell[3];
		this.styles = cell[4];
		this.standard = cell[7];
		this.optional = cell[8];
		this.price = cell[9];
		this.photo_full = cell[11];
		this.photo_thumb = cell[12];
		this.description = cell[13];
		this.display_order = cell[14];
		this.hasAttrOption = hasAttrOption;
		this.hasAttrStandard = hasAttrStandard;
	}
	
	function getStyleAbbr (name) {
		if (name=="") {
			return(false);
		}
		for (var i in styleArray) {
			if (styleArray[i].name  == name) {
				return(styleArray[i].abbr);
			}
		} 
		return ("");
	}
	
	function getHAByName (name) {
		if (name=="") {
			return(false);
		}
		for (var i in manuArray) {
			if (manuArray[i].model  == name) {
				return(manuArray[i]);
			}
		} 
		return (false);
	}
	
	function getHABySlug (name) {
		if (name=="") {
			return(false);
		}
		for (var i in manuArray) {
			var s = manuArray[i].slug ;
			if (manuArray[i].slug  == name) {
				return(manuArray[i]);
			}
		} 
		return (false);
	}

  

    
    function getIconForHAAttr(haName, abbr) {
    	var ha = getHAByName(haName);
    	if (ha && ha.hasAttrStandard(abbr)) {
    		return('<img src="icon_circle_blue_std_11x11.gif" width="11" height="11" /> ');
    	} else if (ha && ha.hasAttrOption(abbr)) {
    		return('<img src="icon_circle_white_opt_11x11.gif" width="11" height="11" />');
    	} else {
    		return('-');
    	}
    } 
    
    function makeManuSelect () {

        var orderA = new Array();
        for (var i in selManuArray) {
            if (selManuArray[i].isStocked != "0" && selManuArray[i].manufacturer != "") {
            	  if ( jQuery.inArray(selManuArray[i].manufacturer, orderA) == -1 ) {
            		  orderA.push(selManuArray[i].manufacturer);
  	            }
                
            }
        }
        orderA.sort();
        var create = '  <form action="../"><select onChange="window.open(this.options[this.selectedIndex].value,\'_top\')" class=copysmall>';
        create += '<option value="">Select...</option>';
        for (var i in orderA) {
        	var selected = "";
        	create += ' <option value="hearing_aids_'+orderA[i].toLowerCase().replace(/ /g,'_')+'.htm">'+orderA[i]+'</option>';
        }
        create += '</select></form>';
        jQuery("#selectManu").html(create);
 
    }
    
    function fillStyles(attributes) {
    	if (attributes=="") {
    		return("");
    	}
    	var haystack = attributes.split(",");
    	var styles="";
    	
    	for (var i=0; i < haystack.length; i++) {
    		if (styles != "") {
    			styles += ',';
    		}
    		abbr = getStyleAbbr(haystack[i]);
    		styles += '<span class=tip><a href="javascript:void(0)" class="showTip '+abbr+'">'+abbr+'</a></span>';
    	}
    
    	return (styles);
    }
    

    function fillAttrs(ha) {
    	var op = '<CENTER><TABLE cellpadding=0 cellspacing=0 border="0" bgcolor=#999999><tr><td><TABLE cellpadding=2 cellspacing=1 border="0" class=small width=450>';
    	var col = 0;
    	var row = 0;
    	var atitle = "";
    	var aYN = "";
    	var aTip = "";
    	for (var i =0; (i*3) < attrArray.length; i++) {
    		op += '<tr>';
    		for (var j=0; j < 3; j++) {
    			var ind = i+(j*3);
    			if (typeof attrArray[ind] == "undefined") {
    				atitle = "";
    		    	aYN = "";
    		    	aTip = "";
    			} else {
    				atitle = attrArray[ind].name;
    		    	aTip = attrArray[ind].tooltip;
    		    	if (atitle== 'Channels') {
    		    		aYN=manuArray[ha].channels;
    		    	} else if (atitle== 'Programs') {
    		    		aYN=manuArray[ha].programs;
    		    	} else  if (checkAttr(attrArray[ind].abbr, manuArray[ha].standard)  || checkAttr(attrArray[ind].abbr, manuArray[ha].optional)) {
    		    		aYN="YES";
    		    	} else {
    		    		aYN = "NO";
    		    	}
			
    			}
    		
	            op += '<td bgcolor=#EFEFEF valign=middle><CENTER><span class=tip><a href="javascript:void(0)" title="'+aTip+'">'+atitle+'</a></span></td>';
	            op += '<td bgcolor=#FFFFFF valign=middle width=50><CENTER>'+aYN+'</td>';
    		}
    		op += '</tr>';
    	}
    	
    	op += "</TABLE> </td> </tr> </TABLE>";
    	return(op);
    }
    
    function makeProduct() {
        var orderA = new Array();
        var op = "";
        
for (var i in manuArray) {
     if (manuArray[i].isStocked != "0" && manuArray[i].model != "") {
    	var photo = '/uploads/no_picture.jpg';
    	var thumb = '/uploads/no_picture_sm.jpg';
    	var priceVal = "";
    	
    	if (manuArray[i].photo_full != "") {
    		photo = "/uploads/"+manuArray[i].photo_full;
    	}
    	
    	if (manuArray[i].photo_thumb != "") {
    		thumb = "/uploads/"+manuArray[i].photo_thumb;
    	}
    	
    	if (manuArray[i].price != "0.00") {
    		priceVal = "$"+manuArray[i].price;
    	}
    	
        op += "<p>";
        op += '<TABLE cellpadding=0 cellspacing=0 border="0">';
        op += "<tr>";
        op += '    <td><img src="corner_ul.gif" width=13 height=13></td>';
        op += '    <td background="bg_top.gif"><img src="spacer.gif" width=13></td>';
        op += '    <td><img src="corner_ur.gif" width=13 height=13></td>';
        op += '</tr>';
        op += "<tr>";
        op += '    <td background="bg_left.gif">&nbsp;</td>';
        op += '    <td valign=top>';
            
        op += '   <TABLE cellpadding=5 cellspacing=1 border="0" class=copy width=100%>';
        op += '    <tr>';
        op += '    <td width=107 valign=top><span title="cssbody=[dogvdvbdy] cssheader=[dogvdvhdr] header=[<img src=\'icon_zoom.gif\'> Product Zoom] body=[<img src=\''+photo+'\'>] " style="font-family:arial;font-size:10px;font-weight:bold;color:#ABABAB;cursor:pointer"><img src="'+thumb+'"></span></td>';
        op += '    <td width=580 valign=top>';
        op += '    <TABLE cellpadding=0 cellspacing=0 border="0" width=100% class=copy>';
        op += '    	<tr>';
        op += '     <td valign=top width="59%"><H2>'+manuArray[i].model+' by '+manuArray[i].manufacturer+'</H2>';
      //  op += '      <span class="subhead">'++'</span><br><b>Styles:</b>';
        op += fillStyles(manuArray[i].styles);
        op += '     </td>';
        
        op += '     <td valign=top align=right width="41%"><H3>Our Price:</b> '+priceVal+'</H3></td>';
        op += '     </tr></TABLE>';

        op += '      <TABLE cellpadding=0 cellspacing=0 border="0" width=100% class=copy> <tr> <td>';
        op += '     <div>';
        op += '     <a href="javascript:void(0)" onClick="toggleBlock(\'below'+i+'\', \'linkid'+i+'\');" id="linkid'+i+'" class=copy>View Details</a>';
        op += '     </div>';
        op += ' 	<td align=right><img src="spacer.gif" width=1 height=5><br> <span class=small><i>'+manuArray[i].description+'</i></span></td></tr></table>';
        op += '     <div id="below'+i+'" style="display:none; ">';
        op += '     <img src="spacer.gif" height=15 width=1><br>';
        op += fillAttrs(i);
        op += '     </div></td>';

        	
        op += ' </tr> </TABLE></td><td background="bg_right.gif">&nbsp;</td> </tr> <tr> <td><img src="corner_bl.gif" width=13 height=13></td><td background="bg_bottom.gif"><img src="spacer.gif" width=13></td> <td><img src="corner_br.gif" width=13 height=13></td></tr></TABLE>';
                
    }
}
	jQuery('#makeProducts').html(op);
    }

