// Set default value for item_term
var item_term = 'false';

function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
			obj["e"+type+fn] = null;
	}
}


// Add event listeners to each itemrow order button


function popupBuyButton(partno) {
	
	var popupBuyButton2 = function (oXML) { 

		//alert('test: ' + oXML.responseText);
	
		var currentrow = 'row-' + partno;
		
		// Get offset left and top of row
		row_x = findPosX($(currentrow)) + 20; 
		row_y = findPosY($(currentrow)) + 40;
		
		// Generate code to throw in after row
		
		var newcode = "<div id=\"buyrow-" + partno + "\"  class=\"buyrow\" style=\"z-index:1000;display:none;cursor:move;position:absolute;left:"+row_x+"px;top:"+row_y+"px;font-family: sans-serif;width: 630px;	height: 120px;	text-align: center;	background: url(quoterow-background.png) top left no-repeat;	color: #fff;\">";
		
		newcode += "<script type=\"text/javascript\">new Draggable('buyrow-" + partno + "', { scroll: window });</script>";
		
		newcode += '<img src="quoterow-close.png" alt="Close" class="quoteclosebutton" style="position:absolute;top:5px;left:5px;cursor:pointer;" id="buyclosebutton-' + partno + '" onclick="javascript:closePopup(\'buyrow-'+partno+'\'); "/>';
		
		newcode += '<form class="quotebox_form" id="itemorder_' + partno + '">';
		
		// Determine item description
		var item_description = $(currentrow).cells[1].innerHTML;
		item_description = item_description.replace(/&(lt|gt);/g, function (strMatch, p1){
			return (p1 == "lt")? "<" : ">";
		});
		item_description = item_description.replace(/<\/?[^>]+(>|$)/g, "");
		newcode += '<span style="font-size:12pt;"><strong>Model ' + partno + '</strong>: ' + item_description + '</span>';
		// was typeof item_term == "undefined" || item_term != 'true' || showdb == 'true'  below -2-13-09
		if ( showdb == 'true') {
			newcode += '<div id="quotebox_message" style="font-size:12px;">Please select from the available dB values below</div>';
		}
		
		newcode +='<span id="quotedetails">';
		newcode +='<div id="quotedetailsrow" style="margin-top:10px;">';

		// Input - dB
		if (typeof showdb != "undefined" && showdb == 'true') {
			newcode += '<label for="db_' + partno + '" style="font-weight:bold;">dB Value:&nbsp;</label><select id="db_' + partno + '" style="width: 80px;"  onChange="javascript:if (this.value==\'other\') {  showOtherDbBox(\'' + partno + '\'); } else { hideOtherDbBox(\'' + partno + '\'); }">';
			
			//alert(' oXML.responseText.split("|").length: ' + oXML.responseText.split("|").length);
			
			// Determine which dB values to show
			if ( oXML.responseText.split("|").length > 1 ) {
			
				// Use values from database
				
				for (i=1; i < oXML.responseText.split("|").length ;i++) {
					newcode += '<option>' + oXML.responseText.split("|")[i] + '</option>';
				}
				
				newcode += '</select>';
				
			
			} else {
			
				// Use default values
				//newcode += '<option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>20</option><option>30</option><option>40</option><option>50</option><option>60</option><option value="other">Other...</option></select><span id="holder-OtherDbBox"></span>';
				newcode += '<option>01</option><option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>20</option><option>30</option><option>40</option><option>50</option><option>60</option></select>';
			
			};
			
			
			
			
			
			
			
		} else {
			newcode += '<input type="hidden" id="db_' + partno + '" value="-1">';
		}

		newcode += '</div> <!-- #buydetailsrow -->';
		
		
		
		newcode +='<input type="button" id="additem_' + partno + '" value="Continue on to Purchase"  style="margin-top: 5px;" onclick="javascript:completeBuy(\'' + partno + '\');" /><span id="status_' + partno + '"></span>';

		
		newcode += '</form>';
		
		newcode += '</span> <!-- #quotedetails -->';
		
		newcode += '</div>';
		
		
		// Insert row
		//new Insertion.After('row-' + partno, newcode);  
		new Insertion.Before (  $('itempopupholder'), newcode );

		// Show row
		var buyrow = 'buyrow-' + partno;
		Effect.Appear( $(buyrow) );	
	
	};
	
	
	var postingvariables = 'function=getDbValues&rnum=' +  Math.random() + '&model=' + partno;	
	//alert('test: ' + postingvariables);
	var myConn = new XHConn();		
	myConn.connect("functions/functions_buy.asp", "POST", postingvariables, popupBuyButton2);
		
	
}

function completeBuy(partno) {
	
	var db = '';
	var dbother = '';
	
	var db_form = 'db_' + partno;
	var db = $F(db_form);
	
	if ( db == 'other' ) {
		var db_form = 'otherdb_' + partno;
		var dbother = $F(db_form);
	}
	

	else if (db == 'other' && dbother <= 0) {
		alert('Please enter an other dB level before submitting quote request.');		
	}
	else {
		// addItem(partno);	

		
		// Redirect to http://www.rell.com/pages/Search-Results.aspx?partNumber=xxxxxx
		
		
		var rellURL = "http://www.rell.com/pages/Search-Results.aspx?partNumber=" + partno + "-" + db;
		//alert("rellURL: " + rellURL);
		
		location.href = rellURL;
		
		
	}
	

}


function expandOrderRow(partno) {
	
	// Test
	//alert ('expandOrderRow thrown\n\nnpartno: ' + partno);
	
	var currentrow = 'row-' + partno;
	
	// Get offset left and top of row
	row_x = findPosX($(currentrow)) + 20; 
	row_y = findPosY($(currentrow)) + 40;
	
	// Generate code to throw in after row
	
	var newcode = "<div id=\"quoterow-" + partno + "\"  class=\"quoterow\" style=\"z-index:1000;display:none;cursor:move;position:absolute;left:"+row_x+"px;top:"+row_y+"px;font-family: sans-serif;width: 630px;	height: 120px;	text-align: center;	background: url(quoterow-background.png) top left no-repeat;	color: #fff;\">";
	
	newcode += "<script type=\"text/javascript\">new Draggable('quoterow-" + partno + "', { scroll: window });</script>";
	
	//closePopup
	
	// newcode += '<img src="quoterow-close.png" alt="Close" class="quoteclosebutton" style="position:absolute;top:5px;left:5px;cursor:pointer;" id="quoteclosebutton-' + partno + '" onclick="javascript:Effect.Fade( $(\'quoterow-'+partno+'\') ); "/>';
	newcode += '<img src="quoterow-close.png" alt="Close" class="quoteclosebutton" style="position:absolute;top:5px;left:5px;cursor:pointer;" id="quoteclosebutton-' + partno + '" onclick="javascript:closePopup(\'quoterow-'+partno+'\'); "/>';
	
	newcode += '<form class="quotebox_form" id="itemorder_' + partno + '">';
	
	// Determine item description
	var item_description = $(currentrow).cells[1].innerHTML;
	item_description = item_description.replace(/&(lt|gt);/g, function (strMatch, p1){
 	 	return (p1 == "lt")? "<" : ">";
 	});
	item_description = item_description.replace(/<\/?[^>]+(>|$)/g, "");
	newcode += '<span style="font-size:12pt;"><strong>Model ' + partno + '</strong>: ' + item_description + '</span>';
	// was typeof item_term == "undefined" || item_term != 'true' || showdb == 'true'  below -2-13-09
	if ( showdb == 'true') {
		newcode += '<div id="quotebox_message" style="font-size:10px;">Refer to DataSheet for Std dB Values</div>';
	}
	
	newcode +='<span id="quotedetails">';
	newcode +='<div id="quotedetailsrow" style="margin-top:10px;">';
	
	// Input - quantity
	newcode += '<label for="quantity_' + partno + '" style="font-weight:bold;">Quantity: </label><input type="text" style="width:50px;margin-right:30px;" maxlength="4" id="quantity_' + partno + '" class="item_quantity_input" />';
	
	// Input - dB
	if (typeof showdb != "undefined" && showdb == 'true') {
		newcode += '<label for="db_' + partno + '" style="font-weight:bold;">dB Level:</label><select id="db_' + partno + '" style="width: 80px;"  onChange="javascript:if (this.value==\'other\') {  showOtherDbBox(\'' + partno + '\'); } else { hideOtherDbBox(\'' + partno + '\'); }">';
		newcode += '<option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>20</option><option>30</option><option>40</option><option>50</option><option>60</option><option value="other">Other...</option></select><span id="holder-OtherDbBox"></span>';
	} else {
		newcode += '<input type="hidden" id="db_' + partno + '" value="-1">';
	}
	
	
	// Input - Gender
	if (typeof item_term != "undefined" && item_term == 'true') {
		newcode += '<label for="gender_' + partno + '" style="font-weight:bold;">Gender: </label><select id="gender_' + partno + '" style="width: 80px;"><option value="">--Select--</option><option value="F">Female</option><option value="M">Male</option></select><br />';
	} else {
		newcode += '<input type="hidden" id="gender_' + partno + '" value=" " />';
	}
	
	
	newcode += '</div> <!-- #quotedetailsrow -->';
	
	
	
	newcode +='<input type="button" id="additem_' + partno + '" value="Add to Quote"  style="margin-top: 5px;" onclick="javascript:checkAddItem(\'' + partno + '\');" /><span id="status_' + partno + '"></span>';
	//(8/5/07)newcode += '<label for="quantity_' + partno + '">Quantity: </label><input type="text" maxlength="4" id="quantity_' + partno + '" class="item_quantity_input" /><input type="button" id="additem_' + partno + '" value="Add to Quote" onclick="javascript:checkAddItem(\'' + partno + '\');" /><span id="status_' + partno + '"></span>';
	//newcode += '<div id="otherdbbox-' + partno + '" class="otherdbbox" style="display:none;"><label for="otherdb' + partno + '">Enter other DB value:<input type="text" maxlength="3" id="otherdb' + partno + '" /></div>';
	
	newcode += '</form>';
	
	newcode += '</span> <!-- #quotedetails -->';
	
	newcode += '</div>';
	
	//newcode = '<div id="quoterow-' + partno + '" class="quoterow" style="z-index:1000;display:none;position:absolute;left:' + row_x + ';top:' + row_y + ';border:solid 1px black;background-color: #ccc;"><form id="itemorder_' + partno + '"><label for="db_' + partno + '">dB Level</label><select id="db_' + partno + '"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>other...</option></select><label for="quantity_' + partno + '">Quantity: </label><input type="text" maxlength="4" id="quantity_' + partno + '" class="item_quantity_input" /><input type="button" id="additem_' + partno + '" value="Add to Quote" onclick="javascript:addItem(\'' + partno + '\');" /><span id="status_' + partno + '"></span></form></div>';
	
	
	//newcode = '<tr id="quoterow-' + partno + '" class="quoterow" style="display: none;"><td colspan="7"><form id="itemorder_' + partno + '"><label for="db_' + partno + '">DB Level</label><select id="db_' + partno + '"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>other...</option></select><label for="quantity_' + partno + '">Quantity: </label><input type="text" maxlength="4" id="quantity_' + partno + '" class="item_quantity_input" /><input type="button" id="additem_' + partno + '" value="Add to Quote" onclick="javascript:addItem(\'' + partno + '\');" /><span id="status_' + partno + '"></span></form></td></tr>';
	
	// Insert row
	//new Insertion.After('row-' + partno, newcode);  
	new Insertion.Before (  $('itempopupholder'), newcode );

	// Show row
	var quoterow = 'quoterow-' + partno;
	Effect.Appear( $(quoterow) );
	
}


function checkAddItem(partno) {
	
	var db = '';
	var dbother = '';
	
	var db_form = 'db_' + partno;
	var db = $F(db_form);
	
	if ( db == 'other' ) {
		var db_form = 'otherdb_' + partno;
		var dbother = $F(db_form);
	}
	
	
	var quantity_form = 'quantity_' + partno;
	var quantity = $F(quantity_form);
	
	if (quantity <= 0) {
		alert('Please enter a quantity before submitting quote request.');		
	}
	else if (db == 'other' && dbother <= 0) {
		alert('Please enter an other dB level before submitting quote request.');		
	}
	else {
		addItem(partno);		
	}
	

}

function addItem( partno ) {
	
	// alert('function addItem launched');
	
	// Clear contents of status box
	var	status = 'status_' + partno;
	$(status).innerHTML = '';
	
	
	// Ready ajax call
	var randomnum = Math.random();

	var db_form = 'db_' + partno;
	var db = $F(db_form);
	
	if ( db == 'other' ) {
		var db_form = 'otherdb_' + partno;
		var db = $F(db_form)
	}
	
	var quantity_form = 'quantity_' + partno;
	var quantity = $F(quantity_form);
	// Add Gender as an input 8/5/07
	var gender_form = 'gender_' + partno;
	var gender = $F(gender_form);
//8/5/07 add gender variable	
	var postingvariables = 'function=additem&rnum=' + randomnum + '&part_number=' + partno + '&db=' + db + '&quantity=' + quantity + '&gender=' + gender
	
	var myConn = new XHConn();
	
	var fnWhenDone_addItem = function (oXML) { 
		
		// Show message that item was added
		
		// Old
		/*
		var	status = 'status_' + partno;
		$(status).innerHTML = 'Record was added for:' + partno;
		$(status).innerHTML += "<input type=\"button\" value=\"View/Complete Quote Request\" onclick=\"javascript:location.href='quote.htm';\"/>";
		*/
		
		// New
		
		var status = 'Record was added for:' + partno;
		status += "<p /><input type=\"button\" value=\"View/Complete Quote Request\" onclick=\"javascript:location.href='quote.htm';\"/>";
		status += "<input type=\"button\" value=\"Continue Creating Quote Request\" onclick=\"javascript:closePopup('quoterow-"+partno+"'); \" />";
		$('quotedetails').innerHTML = status;
	
	};
	
	myConn.connect("functions/functions_quote.asp", "POST", postingvariables, fnWhenDone_addItem);
	


}

function showOtherDbBox(partno) {

	otherbox = 'otherdbbox-' + partno;
	quoterow = 'quoterow-' + partno;
		
	newcode = '<label for="otherdb_' + partno + '" style="font-weight:bold;margin-left: 30px;" >Enter other dB value:<input type="text" style="width:80px;" maxlength="10" class="otherdbbox_input" id="otherdb_' + partno + '" />';
	
	// Old code
	/*
	newcode = '<div id="' + otherbox + '" class="otherdbbox" style="display:none;"><label for="otherdb_' + partno + '">Enter other dB value:<input type="text" maxlength="10" class="otherdbbox_input" id="otherdb_' + partno + '" /></div>';
	new Insertion.Bottom (  $(quoterow), newcode );
	$(otherbox).style.display = 'block';
	*/

	document.getElementById('holder-OtherDbBox').innerHTML = newcode;
	
}

function closePopup(popupid) {
	
	Effect.Fade( $(popupid) );
	$(popupid).remove();	
	
}

function hideOtherDbBox(partno) {
	otherbox = 'otherdbbox-' + partno;
	
	// Look and see if element exists, if so, delete it.
	if ( $(otherbox) ) {	
		Element.remove( otherbox );
	}
}


function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
