function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
	setTimeout("location.reload(true);",0);
}
function removeItem(name) {
	alert(name);
}
function updateValues() {
	setTimeout("location.reload(true);",0);
}
function addQuote(sku, description, price, qty, refresh){

	strCookie = sku + "|" + description + "|" + qty + "|" + price;

  	var y = readCookie('quote');
	skuMatch = 0;
	if (y) { 
		y = y.replace(/^\s+|\s+$/g, '') ;	
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// See if the sku already exists
		var i=0;
		while (i < items.length) {
			// If sku matches, update quantity
			if (sku == items[i]){
				items[i+2]=qty;
				skuMatch = 1;
			}		
			i = i + 4;
		}		
		// Rewrite Cookie
		if (skuMatch == 1){
			var i=1;
			strCookie = items[0];
			while (i < items.length) {
			  strCookie = strCookie + "|" + items[i];
			  i = i + 1; 
			}
		}
		// Append Existing Cookie
		else {
			strCookie = y + "|" + strCookie;	
		}
		createCookie('quote',strCookie,0);
	} 
	else { 
		// Create New Cookie
		createCookie('quote',strCookie,0);
	}	
	// Refresh Quote Page
	if (refresh==1)
	{
			setTimeout("location.reload(true);",0);
	}
}
function displayCookie(name){
	// Table Heading
	document.write('<table border="0"  align="center"><tr><td align="right">'); 
	document.write('<form name="formItems">');
	document.write('<table cellspacing="0" cellpadding="1" border="1" width="550">'); 
	document.write('<tr class="orange"><td colspan="5" align="center"><h2>Quote Request Contents</h2></td></tr>'); 
	document.write('<tr class="orange">'); 
	document.write('<td nowrap><b>Remove</b></td>'); 
	document.write('<td nowrap><b>NTI Part #</b></td>'); 
	document.write('<td nowrap><b>Description</b></td>'); 
	document.write('<td><b>Qty</b></td>'); 
	document.write('<td><b>List Price (Each)</b></td>'); 
	document.write('</tr>'); 
  	var y = readCookie(name);
  	if (y) {	
		y = y.replace(/^\s+|\s+$/g, '') ;					
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// Get each array value (sku, description, qty, price) and display in table
		var i=0;
		var j=0;
		var itemno = 0;
		var itemsExist = 0;
		while (i < items.length) {
			var item = items[i];
			// Create New Record Cell?
			// Sku
			if (j==0) {
			  // Display if quantity is not equal to zero	
			  if (items[i+2]!=0){	
			  	document.write('<tr onMouseOver="this.bgColor=\'#ffffcc\';" onMouseOut="this.bgColor=\'\';">');
				document.write('<td>');
				document.write('<input type="checkbox" value="0" onClick="addQuote(\'' + items[i] +'\', \'' + items[i+1] + '\', \'' + items[i+2] + '\', \'0\', \'1\');">');
				document.write('</td><td nowrap>');
			    document.write(item);
			    document.write('</td>');				
				itemsExist = 1;
			  }
			}
			// Description
			if (j==1) {
			  // Display if quantity is not equal to zero	
			  if (items[i+1]!=0){	
			    document.write('<td>');
			    document.write(item);
			    document.write('</td>');
			  }
			}
			// Qty
			if (j==2) {
			  // Display if quantity is not equal to zero	
			  if (items[i]!=0){	
			  	document.write('<td>');				  			 
			    document.write('<input name="item1" type="text" value="'+ items[i] +'" size="4" class="box-yellow" onChange="addQuote(\'' + items[i-2] +'\', \'' + items[i-1] + '\', \'' + items[i+1] + '\', this.value, \'0\');">');
			    document.write('</td>');
				itemno = itemno + 1;
			  }
			}	
			// Price	
			if (j==3) { 	
			  // Display if quantity is not equal to zero	
			  if (items[i-1]!=0){	
			    document.write('<td>');
			    document.write(item);
			    document.write('</td>');
				document.write('</tr>');	
			  }					  		  
			}			
		j=j+1;
		// Reset j if end of record
		if (j==4) { 				
			  j=0;
		}
		i=i+1;
		}		
	}
	// Number of Items
	document.write('<input type="hidden" name="itemno" value="'+ itemno +'">');
	// Table Closing
	document.write('</table></form>');	
	if (itemsExist == 1){
		// Display Update and Clear Contents Button if Items exist
		document.write('<br><input type="button" name="update" value="Update Values" onClick="updateValues()">&nbsp;<input type="button" name="clear" value="Remove All" onClick="eraseCookie(\'quote\')">'); 
	}	
}
function formCookie(name){
	// Table Heading
  	var y = readCookie(name);
  	if (y) {	
		y = y.replace(/^\s+|\s+$/g, '') ;					
		// Split on | to get individual values into an array
		var items = y.split('|');		
		// Get each array value (sku, description, qty, price) and display in table
		var i=0;
		var j=0;
		var itemno = 0;
		var itemsExist = 0;
		while (i < items.length) {
			var item = items[i];
			// Create New Record Cell?
			// Sku
			if (j==0) {
			  // Add if quantity is not equal to zero	
			  if (items[i+2]!=0){	
			  	document.write('<input type="hidden" name="sku'+ itemno+'" value="'+ item +'">');	
				itemsExist = 1;
			  }
			}
			// Description
			if (j==1) {
			  // Add if quantity is not equal to zero	
			  if (items[i+1]!=0){	
			    document.write('<input type="hidden" name="description'+ itemno+'" value="'+ item +'">');			    
			  }
			}
			// Qty
			if (j==2) {
			  // Add if quantity is not equal to zero	
			  if (items[i]!=0){	
			    document.write('<input type="hidden" name="qty'+ itemno+'" value="'+ item +'">');				
			  }
			}	
			// Price	
			if (j==3) { 	
			  // Add if quantity is not equal to zero	
			  if (items[i-1]!=0){	
			    document.write('<input type="hidden" name="price'+ itemno+'" value="'+ item +'">');
				itemno = itemno + 1;
			  }					  		  
			}			
		j=j+1;
		// Reset j if end of record
		if (j==4) { 				
			  j=0;
		}
		i=i+1;
		}		
	}
	// Number of Items
	document.write('<input type="hidden" name="itemno" value="'+ itemno +'">');

}


