//
// !COPYRIGHT!
//                      Copyright (c) 2008 Teleformix LLC
//                       All Rights Reserved
// 
// 
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF TELEFORMIX LLC
// 
// The copyright notice above does not evidence any
// actual or intended publication of such source code.
// 
// This document contains trade secret data and proprietary information of
// Teleformix, LLC and should be treated as confidential.  No part of this 
// information may be copied, or disclosed in part or in whole as permitted 
// by Teleformix LLC
// 
// Copyright (c) 1998-2008 by Teleformix, LLC.
// All Rights Reserved
// 
// $Id: orders.js,v 1.2 2008/08/18 17:06:06 pdiverde Exp $
// !!
//

var product_id   			= getURLParam("product_id");
var promotion_id 			= getURLParam("promo_id");

var companyid				= "9999";
var etailerid				= "9999";
var productid				= "9999";					// screen play product

var umsdao		 			= "weboffers_ums";
var umslkup		 			= umsdao 	+ "@promolkup";
var bulletlkup	 			= umsdao 	+ "@bulletlkup";
var style_stp	 			= umsdao 	+ "@style_stp";
var inserttag	 			= offerdao 	+ "@insert";
var etailer_tag				= offerdao  + "@getumsoffer";
var options_list			= umsdao 	+ "@options_list";
var options_select			= umsdao 	+ "@options_select";

var server 		 			= "http://" + document.location.host + "/";
var	etailer_ref_beg  		= (new Date()).valueOf();

var arrows_folder 			= "images/arrows/";
var backgrounds_folder 		= "images/backgrounds/";
var delivered_folder 		= "images/delivered/";
var price_folder 			= "images/price/";
var great_value_folder 		= "images/price_header/";
var price_shape_folder 		= "images/price_shape/";
var product_name_folder 	= server + "images/prod_name/";
var path_to_images  		= "http://www.dollardirectonline.com/Assets/prodimg/";
var product_image2_folder 	= "http://www.dollardirectonline.com/Assets/prodimg/";
var shipping_folder 		= "images/shipping/";

var product_name;													//product name
var final_header	     	= ""; 									//subheader leave only enough characters not to break the layout; 
var product_desc_long		= "";									//text for the product
var product_image        	= path_to_images + product_id + 'L.gif';	//location of product image
var product_price;													//price of the product
var prod_shipping;													//if >0 - +shipping and handling; if 0 - delivered
var acrive;															//defines of the promotion is still active

var layout_html				= 1;									//corresponds to an array in the templates.js file to get the html code for the layout
var max_width			    = 280;									//maximum width of the product image in case someone forgot to specify it in the style table
var second_image_max_width  = 160;
var price_position			= 0;									//0 - the price image is on the top, 1-the price image is at the bottom, zero if it is not specified in the table
	
var bullet_header_color;											//color of the bullets header
var desc_line1_color;												//color of first row of product description header
var desc_line2_color;												//color of second row of product description header
var price_color_folder;												//folder where the images for generating the price are
var prod_name_color;												//product color if product header image does not exist
var subheader_color;												//subheader color
var bg_bottom_img;													//bottom background image
var bg_top_img;														//top background image
var bullet_img;														//bullet image
var price_bottom_img;												//price shape image when the image is on the bottom
var price_top_img;													//price shape image when the price is on the top
var delivered_shipped;												//shipping and handling or delivered image
var price_header_img;												//great value only image
	
var product_im2_general = product_image2_folder + "shopping.gif";	//use this general shopping image if there is none set up
var product_img2 		= product_im2_general;						//second product image
var product_name_image;  											//image of the product name

var desc_header1 		= "";										//first row description header
var desc_header2 		= "";										//second row description header

var tbl; 				  											//bullets table;
var tbl2;                 											//price table; 
var size_text; 														//size text of the subheader. If text is too long the font becomes smaller
var number_digits;													//number of digits in the price;
var isOldSystem;													//System is old if Windows < XP and browser=MSIE

var	optionsItems = [];


function preload_image(img_name)
{
	var temp = new Image();
	try 
	{
		temp.src = img_name;
	}
	catch (ex)
	{
	}
}

function setSubheader(product_desc_short)
{
	var min_symbols;
	var max_symbols;
	var num_symbols;
	var num_words;
	var next_space;
	var text = "";
  
  	if ( price_position == 0 )
    {
	 	min_symbols = 45;
		max_symbols = 60;
	} 
  	else
  	{
		min_symbols = 70;
		max_symbols = 100;
	}	 
	 
	num_symbols = product_desc_short.length;
	 
	if ( num_symbols <= min_symbols )
	{
	     final_header 	= product_desc_short;
		 size_text 		= "large";
	}
	else if ( ( num_symbols > min_symbols ) && 
		 	  ( num_symbols < max_symbols) ) 
	{
	     final_header 	= product_desc_short;
		 size_text 		= "small";
	}
	else
	{
		num_words = product_desc_short.split(' ').length;
		  
		for ( var i=1; i < num_words + 1; i++ )
		{
			next_space 	= product_desc_short.indexOf(' ');
			text 		= text + 
						  product_desc_short.substr(0, next_space + 1);
			 
			if ( text.length <= max_symbols )
			{
				final_header 	= text;
				size_text 		= "small";
			}

			product_desc_short = product_desc_short.substr(next_space + 1);
		}
		
		final_header = final_header + "...";
	}   	  
}

function setDescHeader(product_desc_head)
{
	var number_symbols;
	var max_symbols; 
	var num_rows;
	var num_words;
	var k;
	var text = "";
	var product_desc_head_temp;
	var next_space;
    var desc_header_element = document.getElementById("desc_header_table")
		max_symbols = Math.round(desc_header_element.width / 9);
		
		number_symbols 			= product_desc_head.length;
		product_desc_head_temp 	= product_desc_head;
		num_rows 				= Math.floor( number_symbols / max_symbols ) + 1;
		num_words 				= product_desc_head.split(' ').length;
  
    if ( Math.round( num_rows / 2 ) == ( num_rows / 2 ) )
      	{
			k = ( num_rows * max_symbols ) / 2;
	  	}
   	else
      	{
	    	k = ( Math.floor( num_rows / 2 ) + 1 ) * max_symbols;
	  	}

    for ( var i=0; i <= num_words; i++ )   
	{
	    next_space 	= 	product_desc_head_temp.indexOf(' ');
		text 		= 	text + 
						product_desc_head_temp.substr(0, next_space + 1);

		if ( text.length <= k )
		{
			desc_header1 = text;
		}
		else
		{
		 	if ( desc_header2 == "" )
			{
			   	desc_header2 = product_desc_head_temp;
			}
		}
		
		product_desc_head_temp = 
				product_desc_head_temp.substr( next_space + 1 );
	} 
}


function setBullets()
{
	var retval;
	var numrows;
	var tbl, tBdy, newRow, newCell;
	var bullet_cell        = document.getElementById("bullets_cell");
	var description_cell   = document.getElementById("description_cell");
	var product_image_cell = document.getElementById("product_image_cell");
  
  	daoClient.setParameter(bulletlkup + ":product_id", product_id)
  	retval = daoClient.execute(bulletlkup);
  
  	if ( !retval )
	{
		alert("ERROR: No bullets.");
		return;
	}
  
  	numrows = daoClient.getNumRows(bulletlkup);
  
  	if ( numrows > 1 )
  	{
        tbl    = document.createElement("table");
  	   	tbl.id = "bulletstable";
		
		tBdy   = document.createElement("tbody");
	  	  
	    newRow = document.createElement("tr");
		
		newCell    			= document.createElement("td");
		newCell.id 			= "bullets_header_cell";
		newCell.colSpan   	= 2;
		newCell.className 	= "bulletheader";
		newCell.innerHTML 	= 
				daoClient.getEventValue(bulletlkup + ":bullet_value", 0);
		newRow.appendChild(newCell);
		tBdy.appendChild(newRow);
		
		for ( var i = 1; i < numrows; i++ )
		{
			newRow  = document.createElement("tr");
				
			newCell = document.createElement("td");
			newCell.width = 21;
				
			newImage     = document.createElement("img");
			newImage.src = bullet_img;
			newImage.id  = "bullet_" + i;
			newCell.appendChild(newImage);
			newRow.appendChild(newCell);
				
			newCell 			= document.createElement("td");
			newCell.className 	= "txt";
			newCell.innerHTML 	= 
					daoClient.getEventValue(bulletlkup + ":bullet_value", i);
			newRow.appendChild(newCell);
				
			tBdy.appendChild(newRow);
		}
		 
		if ( price_position == 1 )
		{
			newRow  = document.createElement("tr");
				
			newCell = document.createElement("td");
			newCell.colSpan = "2";
			  
			newImage     = document.createElement("img");
			newImage.src = "images/images/spacer.gif";
			newImage.style.height = 45; 
			newCell.appendChild(newImage);
			newRow.appendChild(newCell);
			tBdy.appendChild(newRow);
		} 
			
		tbl.appendChild(tBdy);
	}
	else
	{
		tbl = "No Bullets";
	}
	
	return(tbl);
} 


function replacePng(img_name)
{
	if (isOldSystem)
	{
		num_symbols = img_name.length;
			
		if (img_name.substring(num_symbols-3, num_symbols) == 'png')
		{
			img_name = img_name.substring(0, num_symbols - 3) + 'gif';
		}
	}
	
	return img_name;	
}

function setPrice()
{
	var tBdy, newRow, newCell, newImg;
	var newImg2, newImg3, price_num, tbl2; 
	var large_price, small_price, price_length;
  
	newImg = document.createElement("img");
  
	if ( product_price.length < 2 )
  	{
		alert("Incorrect price");
		return;
	}
	
	if ( price_position == 1 )
  	{
		newImg.src = replacePng(price_bottom_img);
	}
 	else
	{
		newImg.src = replacePng(price_top_img);
	}
	
	newImg.style.width 	= 226;
	newImg.style.height = 162;
	newImg.style.zIndex = 0;
    document.getElementById("price_shape").appendChild(newImg);
  
    tbl2        		= document.createElement("table");
    tbl2.cellSpacing 	= 0;
	tbl2.cellPadding 	= 0;
	tbl2.align  		= "center";
  	tbl2.id     		= "price_table";
	  
	tBdy = document.createElement("tbody");
	  	  
	newRow      = document.createElement("tr");
		
	newCell     = document.createElement("td");
    newImg      = document.createElement("img");
	newImg.src  = replacePng( price_folder 			+ 
							  price_color_folder 	+ 
							  "/common/dollar.png");
	newImg.id   = "dollar";
	  
	newCell.appendChild(newImg);
	newRow.appendChild(newCell);
	  
	price_length = product_price.length;
	if ( price_length == 2 )
	{  
		large_price = "0";
	}
	else
	{
	    large_price = product_price.substring(0, price_length - 2) ;
	}
	
	small_price = product_price.substr(price_length - 2);
	  		
	for ( var i=0; i < large_price.length; i++ )
	{
		price_num = large_price.substring(i, i+1);
			
		newCell    = document.createElement("td");
		newImg     = document.createElement("img");
		newImg.src = replacePng(price_folder 		+ 
								price_color_folder 	+ 
								"/large/" 			+ 
								price_num 			+ 
								'.png');
		newImg.id  = "number" + ( i + 1 );
		newCell.appendChild(newImg);
		newRow.appendChild(newCell);
	}
		
	newCell    = document.createElement("td");
    newImg     = document.createElement("img");
	newImg.src = replacePng(price_folder 		+ 
							price_color_folder 	+ 
							"/common/dot.png");
	newImg.id  = "dot_image";
	  
	newCell.appendChild(newImg);
	newRow.appendChild(newCell);	
	  
	for ( var j=0; j < small_price.length; j++ )
	{
		price_num = small_price.substring(j, 1 + j);
			
		newCell    = document.createElement("td");
		newImg     = document.createElement("img");
		newImg.src = replacePng(price_folder 		+ 
								price_color_folder 	+ 
								"/small/" 			+ 
								price_num 			+ 
								'.png');
		newImg.id  = "number" + ( i + j + 1 );
			
		newCell.appendChild(newImg);
		newRow.appendChild(newCell);
	}
    
	tBdy.appendChild(newRow);
	tbl2.appendChild(tBdy);
	number_digits = i + j + 1 ;
	
	try
	{
		prod_shipping = Number(prod_shipping);
	}
	catch (ex)
	{
		prod_shipping = 0;
	}
	
	if (prod_shipping > 0)
	{
	    delivered_shipped = replacePng(shipping_img);
	}
	else  
	{
	    delivered_shipped = replacePng(delivered_img);
	}
	
	return(tbl2);
}

function setPrice_oldBrowsers()
{
	var tBdy, newRow, newCell, newImg;
	var newImg2, newImg3, price_num, tbl2; 
	var large_price, small_price, price_length;
	var temp_image;
	var shape_cell, price_shape, price_shape_table;
	
	shape_cell 			= document.getElementById("shape_cell");
	price_shape 		= document.getElementById("price_shape");
	price_shape_table 	= document.getElementById("price_shape_table");
  
	if ( product_price.length < 2 )
  	{
		alert("Incorrect price");
		return;
	}
	
  	if ( price_position == 1 )
  	{
		temp_image = replacePng(price_bottom_img);
		temp_image = 'url(' + temp_image + ')';
		shape_cell.style.backgroundImage 	= temp_image;
		shape_cell.style.backgroundRepeat 	= 'no-repeat';
		shape_cell.style.backgroundPosition = 'bottom right';		
		price_shape.style.display 			= "none";
	  	price_shape_table.style.marginBottom= 0;

	}
  	else
	{
		temp_image = replacePng(price_top_img);
		temp_image = 'url(' + temp_image + ')';
		shape_cell.style.backgroundImage = temp_image;
		price_shape.style.display = "none";
	  	price_shape_table.style.marginTop = 0;
	 }
	  
	tbl2        		= document.createElement("table");
	tbl2.cellSpacing 	= 0;
	tbl2.cellPadding 	= 0;
	tbl2.align  		= "center";
	tbl2.id     		= "price_table";
	  
	tBdy = document.createElement("tbody");
	  	  
	newRow      = document.createElement("tr");
		
	newCell     = document.createElement("td");
	newImg      = document.createElement("img");
	newImg.src  = replacePng(price_folder 		+ 
							 price_color_folder + 
							 "/common/dollar.png");
	newImg.id   = "dollar";
	  
	newCell.appendChild(newImg);
	newRow.appendChild(newCell);
	  
	price_length =  product_price.length;
	if ( price_length == 2 )
	{  
		large_price = "0";
	}
	else
	{
		large_price = product_price.substring(0, price_length - 2) ;
	}
	
	small_price = product_price.substr(price_length - 2);
	  		
	for ( var i=0; i < large_price.length; i++ )
	{
		price_num = large_price.substring(i, i+1);
			
		newCell   = document.createElement("td");
		newImg    = document.createElement("img");
		newImg.src= replacePng(price_folder 		+ 
							   price_color_folder 	+ 
							   "/large/" 			+ 
							   price_num 			+ 
							   '.png');
		newImg.id = "number" + ( i + 1 );
		newCell.appendChild(newImg);
		newRow.appendChild(newCell);
	}
		
	newCell    = document.createElement("td");
    newImg     = document.createElement("img");
	newImg.src = replacePng(price_folder 		+ 
							price_color_folder 	+ 
							"/common/dot.png");
	newImg.id  = "dot_image";
	  
	newCell.appendChild(newImg);
	newRow.appendChild(newCell);	
	  
	for ( var j=0; j < small_price.length; j++ )
	{
		price_num 	= small_price.substring(j, 1+j);
			
		newCell    	= document.createElement("td");
		newImg     	= document.createElement("img");
		newImg.src 	= replacePng(price_folder 		+ 
								 price_color_folder + 
								 "/small/"			+ 
								 price_num 			+ 
								 '.png');
		newImg.id  	= "number" + ( i + j + 1 );
			
		newCell.appendChild(newImg);
		newRow.appendChild(newCell);
	}
    
	tBdy.appendChild(newRow);
	tbl2.appendChild(tBdy);
	number_digits = i + j + 1;
	
	try
	{
		prod_shipping = Number(prod_shipping);
	}
	catch (ex)
	{
		prod_shipping = 0;
	}
	
	if ( prod_shipping > 0 )
	{
	    delivered_shipped = replacePng(shipping_img);
	}
	else  
	{
	    delivered_shipped = replacePng(delivered_img);
	}
	
	return(tbl2);
}

function optionList(attribute_id, attribute_index, attribute_value)
{
  this.attribute_id    	= attribute_id;
  this.attribute_index  = attribute_index;
  this.attribute_value  = attribute_value;
}


function setOptionsList()
{
	var retval, attribute_id, attribute_index, attribute_value;
	
	daoClient.setParameter(options_list + ":product_id", product_id);
	retval = daoClient.execute(options_list);
	
		if ( !retval )
		{
			alert("ERROR: Invalid product.");
			return;
		}
	
		numrows = daoClient.getNumRows(options_list);
		
		if ( numrows > 0 )
		{
			for ( var i = 0 ; i < numrows; i++ )
			{
				attribute_id    = daoClient.getEventValue(options_list + ":attribute_id", i);
				attribute_index = daoClient.getEventValue(options_list + ":attribute_index", i);
				attribute_value = daoClient.getEventValue(options_list + ":attribute_value", i);

  				optionsItems[i] = new optionList(attribute_id, attribute_index, attribute_value);
			}
		}
}

function searchOptionList(attribute_id, attribute_index)
{
	var value = '';
	
	if (optionsItems.length > 0 )
	{
		for ( var i = 0 ; i < optionsItems.length; i++ )
		{
			if (optionsItems[i].attribute_id == attribute_id && 
				optionsItems[i].attribute_index == attribute_index)
			{
				value = optionsItems[i].attribute_value;
			}
		}
	}
	return(value);	
}

function setOptions()
{
	var retval, option_id, attribute0, attribute1, attribute2, attribute3, attribute4, defaultOption, selectedValue;
	
	daoClient.setParameter(options_select + ":product_id", product_id);
	retval = daoClient.execute(options_select);
	
	document.getElementById("options_list_span").style.visibility = "hidden";
	
		if ( !retval )
		{
			alert("ERROR:Options Select Query Failed");
			return;
		}
	
		numrows = daoClient.getNumRows(options_select);
		
		if ( numrows > 0 )
		{
			for ( var i = 0 ; i < numrows; i++ )
			{
				var value = '';
				
				option_id 		= new Number(daoClient.getEventValue(options_select + ":option_id", i)); 
				attribute0 		= new Number(daoClient.getEventValue(options_select + ":attribute0", i));
				attribute1 		= new Number(daoClient.getEventValue(options_select + ":attribute1", i));
				attribute2 		= new Number(daoClient.getEventValue(options_select + ":attribute2", i));
				attribute3 		= new Number(daoClient.getEventValue(options_select + ":attribute3", i));
				attribute4 		= new Number(daoClient.getEventValue(options_select + ":attribute4", i));
				defaultOption 	= daoClient.getEventValue(options_select + ":default", i);
				
				if (attribute0 !== 0 )
				{
					value = value + ' ' + searchOptionList(0, attribute0);
				}
				if (attribute1 !== 0 )
				{
					value = value + ' ' + searchOptionList(1, attribute1);
				}
				if (attribute2 !== 0 )
				{
					value = value + ' ' + searchOptionList(2, attribute2);
				}
				if (attribute3 !== 0 )
				{
					value = value + ' ' + searchOptionList(3, attribute3);
				}
				if (attribute4 !== 0 )
				{
					value = value + ' ' + searchOptionList(4, attribute4);
				}
				
				if ( isBlank(value) && (numrows < 2) )
				{
					document.getElementById("options_list_span").innerHTML = "";
				}
				else
				{
					document.getElementById("options_list_span").style.visibility = "visible";
					insertSelectOption("option_id", option_id, value.toLowerCase());
					
					if ((defaultOption == 'Y') || (defaultOption == 'y'))
					{
						selectedValue = option_id;
					}
				}
			}
		}
	if(document.getElementById("option_id"))
	{
		setSelectValue("option_id", selectedValue);
		value = '';
		for (var i = 0; i < optionsItems.length; i++)
		{
			if (optionsItems[i].attribute_index == 0)
			{
				value = value + ' ' + optionsItems[i].attribute_value;
			}
		}
		document.getElementById("options_list_text").innerHTML = 	'&nbsp;&nbsp;&nbsp;Please select ' 	+ 
																	value.toLowerCase() 				+ 
																	'<br><br>' ;
	}
}


function setParameters()
{
  	var product_desc_short;
  	var product_desc_head;
	var temp_image;

	product_name       = daoClient.getEventValue(umslkup + ":product_desc_short")+ ' ';			//product name
	product_desc_short = daoClient.getEventValue(umslkup + ":product_desc_head")+ ' ';	//subheader
	product_desc_head  = '&nbsp;';	//header of the long product description
	product_desc_long  = daoClient.getEventValue(umslkup + ":product_desc_long");		//text for the product
	product_image      = path_to_images + product_id + 'L.gif';							//location of product image
	product_price 	   = daoClient.getEventValue(umslkup + ":prod_amt");				//price of the product
	prod_shipping	   = daoClient.getEventValue(umslkup + ":shipping_amt");			//if 0 - use delivered, if > 0 - use shipping and handling image
	active 			   = daoClient.getEventValue(umslkup + ":active"); 					//defines if the promotion is still active or it has expired

	max_width 			= daoClient.getEventValue(style_stp + ":max_img_size");			//max_width maximum width of the product image 
	price_position 		= daoClient.getEventValue(style_stp + ":price_position");		//0 - the price image is on the top, 1-the price image is at the bottom
	
	bullet_header_color	= daoClient.getEventValue(style_stp + ":clr_bullet_head");		//color of the bullets header
	desc_line1_color	= daoClient.getEventValue(style_stp + ":clr_desc_head1");		//color of first row of product description header
	desc_line2_color	= daoClient.getEventValue(style_stp + ":clr_desc_head2");		//color of second row of product description header
	price_color_folder	= daoClient.getEventValue(style_stp + ":clr_price");			//folder where the images for generating the price are
	prod_name_color		= daoClient.getEventValue(style_stp + ":clr_prod_name");		//product color if product header image does not exist
	subheader_color		= daoClient.getEventValue(style_stp + ":clr_subheader");		//subheader color
	bg_bottom_img 		= backgrounds_folder  + daoClient.getEventValue(style_stp + ":img_bg_bottom");		//bottom background image
	bg_top_img	 		= backgrounds_folder  + daoClient.getEventValue(style_stp + ":img_bg_top");			//top background image
	bullet_img  		= arrows_folder       + daoClient.getEventValue(style_stp + ":img_bullet");			//bullet image
	delivered_img 		= delivered_folder    + daoClient.getEventValue(style_stp + ":img_delivered");		//delivered image
	price_bottom_img	= price_shape_folder  + daoClient.getEventValue(style_stp + ":img_price_bottom");	//price shape image when the image is on the bottom
	price_top_img 		= price_shape_folder  + daoClient.getEventValue(style_stp + ":img_price_top");		//price shape image when the price is on the top
	price_header_img 	= great_value_folder  + daoClient.getEventValue(style_stp + ":img_great_value");	//great value only
	shipping_img 		= shipping_folder     + daoClient.getEventValue(style_stp + ":img_shipping");		//shipping and handling image
	
	temp_image = daoClient.getEventValue(style_stp + ":img_action"); //second product image f you need to use it from the database
	
	if (temp_image.length > 0)
	{
		if( daoClient.doesURLExist(temp_image))
		{
			product_img2 = temp_image; 
		}
	}
	
	temp_image = product_image2_folder + product_id + 'A.gif'; 
					
	if ( daoClient.doesURLExist(temp_image) )
		{
			product_img2 = temp_image; 
		}
	// end set up secondary image
	
	temp_image = daoClient.getEventValue(style_stp + ":img_header");

	if ( temp_image.length > 0 )
	{
		if ( daoClient.doesURLExist(replacePng(temp_image)) )
		{
			product_name_image	= replacePng(temp_image);//header name of product image
		}
		else 
		{
			temp_image = replacePng(product_name_folder + product_id + 'H.png');

			if ( daoClient.doesURLExist(temp_image) )
			{
				product_name_image = temp_image;
			}
			else
			{
				temp_image = product_name_folder + product_id + 'H.gif';
							
				if ( daoClient.doesURLExist(temp_image) )
				{
					product_name_image = temp_image; 
				}
				else
				{
					product_name_image = "";
				}
			}
		}
	}
	else
	{
		product_name_image = "";
	}

	setSubheader(product_desc_short);
  	setDescHeader(product_desc_head);
 	tbl  = setBullets();
	
	if ( isOldSystem )
	{
		tbl2 = setPrice_oldBrowsers();
	}
	else
	{
		tbl2 = setPrice();
	}
}

function doResizeImage()
{
	var newImage = document.getElementById("product_image_temp");

	if ((newImage.width < 50) || (newImage.width == 'undefined') || (newImage.width == ''))
	{
		i = i + 1;
		if ( i < 15 )
		{
			setTimeout("doResizeImage(" + i + ")",300);
		}
	}
	else
	{
		width = newImage.width;
		height = newImage.height;
		k = width/height;

		if ( width > max_width )
		{
			newImage.style.width  = max_width;
			newImage.style.height = Math.round(max_width/k);
		}

		document.getElementById("product_image_place").style.visibility = 'visible';
	}
}

function setLayout()
{
	//set header
	var img, width,height,k, num_words;
	var newImage;
	var isOldSystem = false;
    var header_cell;
	var hdr_line1_element;
	var hdr_line2_element;
	var subheader_element;
	var product_image_temp_element;
	var main_content_element;
	var text = '';
	var top_bg_cell_element = document.getElementById("top_bg_cell");
	var main_bg_cell_element = document.getElementById("main_bg_cell");
	var orderform_element = document.getElementById("orderfrm");
	

	top_bg_cell_element.style.backgroundImage 	= "url(" + bg_top_img + ")";
	main_bg_cell_element.style.backgroundImage	= "url(" + bg_bottom_img + ")";
	
	hdr_line1_element 	= document.getElementById("header_line1");
	hdr_line2_element 	= document.getElementById("header_line2");
	header_cell 		= document.getElementById("header_image_cell");
	
	if ( ( active =='N' ) || ( active=='n' ) )
	{
		hdr_line1_element.className   = "header_cell_text";
		hdr_line1_element.style.color = "#9F2208";
		hdr_line1_element.innerHTML   = "This promotion is no longer active";
			
		
		newImage 		= document.createElement("img");
		newImage.src 	= product_image;
		newImage.id 	= "product_image_temp";
		newImage.style.width = max_width;
			
		document.getElementById("product_image_place").appendChild(newImage);
	
		orderform_element.style.visibility = "hidden";	
	}
	else
	{
		//set product name image
		if ( ( product_name_image == '' ) || ( isOldSystem ) )
  	  	{
			if (product_name.length > 50) 
			{
				header_cell.style.fontSize = "19px";
			}
			
			if (product_name.length >= 80 )
			{
				product_name = product_name.substr(0,80);
				last_index = product_name.lastIndexOf(' ');
				product_name = product_name.substr(0, last_index) + '...';
			}
			
			header_cell.innerHTML 	= product_name;
	    	header_cell.style.color = prod_name_color;
			header_cell.className 	= "header_cell_text";		
   	   	}
    	else
   	  	{
   			newImage 	 = document.createElement("img");
			newImage.src = product_name_image;
			newImage.id  = "product_name";
		 			
			header_cell.appendChild(newImage);
			header_cell.vAlign = "bottom";
  	   	}

		//set up subheader
		subheader_element = document.getElementById("subheader");
		subheader_element.className   = "subheader";
		subheader_element.style.color = "#" + subheader_color;

		if ( size_text == "small" )
		{
			subheader_element.style.fontSize = "12";
		}
		else
		{
			subheader_element.style.fontSize = "15";
		}	
		
		subheader_element.innerHTML = final_header;
	

		//set up short descriptions
		hdr_line1_element.innerHTML   = desc_header1;
		hdr_line1_element.style.color = "#" + desc_line1_color;
	
		if (desc_header2 == '')
		{
		 	document.getElementById('desc_header_table').deleteRow(1);
		}
		else
		{	
			hdr_line2_element.innerHTML   = desc_header2;
			hdr_line2_element.style.color = "#" + desc_line2_color;
		}
		
		//set up main image
		document.getElementById("product_image_place").style.visibility = 'hidden';
		document.getElementById("product_image_place").style.width = max_width;
		document.getElementById("product_image_place").style.overflow = 'hidden';
		
		newImage 		= document.createElement("img");
		newImage.src 	= product_image;
		newImage.id 	= "product_image_temp";

		document.getElementById("product_image_place").appendChild(newImage);
		doResizeImage(0);
		
		//set up text
		main_content_element = document.getElementById("main_content");
		main_content_element.innerHTML = 	main_content_element.innerHTML + 
											product_desc_long;
	  
		//setup Price
  		if ( !isOldSystem )
		{
			document.getElementById("great_value_cell").src 	= replacePng(price_header_img);
			document.getElementById("price_cell").appendChild(tbl2);
			document.getElementById("delivered_cell").src 		= delivered_shipped;
			document.getElementById("price_shape").style.zindex = -1;
		}
		else
		{
			document.getElementById("great_value_cell").src 			= replacePng(price_header_img);
			document.getElementById("price_cell").appendChild(tbl2);
			document.getElementById("delivered_cell").src 				= delivered_shipped;
			document.getElementById("price_shape_table").style.zindex 	= 100;
		}

		//set up bullets
		if ( tbl !== "No Bullets" )
		{
			document.getElementById("bullets_cell").appendChild(tbl);
			
			newImage 	= document.createElement("img");
			newImage.id = "product_image2";
				
			if ( product_img2 == '' )
			{
				newImage.src = product_im2_general;
			}
			else
			{
				newImage.src = product_img2;
			}	
			
			newImage.style.width = second_image_max_width;	
			document.getElementById("second_image").appendChild(newImage); 
		
			if ( price_position == 1 )
			{
				document.getElementById("product_image2").style.marginBottom = 60;
			} 
		}
		orderform_element.style.visibility = "visible";	
	}
	
	document.title = product_name;
}

function setInsertParams()
{
	var fname1;

	try
	{
		fname1 = document.gerElementById("ship_fname1").value;
	}
	catch(err)
	{
		fname1='none';
	}
	
	daoClient.setParameter(inserttag + ":ip_address", ip_address) ;
	daoClient.setParameter(inserttag + ":user_agent", navigator.userAgent) ;
	daoClient.setParameter(inserttag + ":etailer_ref", etailer_ref) ;
	daoClient.setParameter(inserttag + ":etailer_cust_first", fname1) ;
	daoClient.setParameter(inserttag + ":etailerid", etailerid) ;
	daoClient.setParameter(inserttag + ":recordtype", recordtype) ;
	daoClient.setParameter(inserttag + ":offer_seq_number", offer_seq_number) ;
	daoClient.setParameter(inserttag + ":return_url", return_url) ;
	daoClient.setParameter(inserttag + ":productid", productid) ;
}

function insertOffer()
{
	recordtype = "OFF" ;

	setInsertParams() ;

	daoClient.execute(inserttag) ;
}

function insertOrder()
{
	recordtype = "ORD" ;

	setInsertParams() ;

	var email = document.getElementById("email").value;
	daoClient.setParameter(inserttag + ":email", unescape(email)) ;
	daoClient.execute(inserttag) ;

	offer_seq_number = parseInt(offer_seq_number) + parseInt("1") ;
	setValue("offer_seq_number", offer_seq_number) ;
}

function doOnload()
{
  	var retval, layout_html, numrows, text;

	document.forms[0].etailer_ref.value = etailer_ref_beg;
	etailer_ref = etailer_ref_beg;  
   
	initialize();
   
	if (! isBlank(getURLParam("etailerid")))
	{
		etailerid = getURLParam("etailerid");

		daoClient.setParameter(etailer_tag + ":etailerid", etailerid);
		daoClient.setParameter(etailer_tag + ":offer_seq_number", "1");
		retval = daoClient.execute(etailer_tag);

		if ((retval) && (daoClient.getNumRows(etailer_tag) > 0))
		{
			companyid = daoClient.getEventValue(etailer_tag + ":companyid");
			productid = daoClient.getEventValue(etailer_tag + ":productid");
			product_id = 
				daoClient.getEventValue(etailer_tag + ":ums_product_id");
			promotion_id = 
				daoClient.getEventValue(etailer_tag + ":ums_promotion_id");
		}
	}

	setValue("companyid", companyid);
	setValue("etailerid", etailerid);
	setValue("productid", productid);
	setValue("etailer_ref", etailer_ref);
	setValue("promotion_id", promotion_id);
	setValue("product_id", product_id);
	setValue("ip_address", ip_address);
	setValue("return_url", return_url);

	preload_image(product_image);

	if ( ( isBlank ( product_id ) ) || ( isBlank ( promotion_id ) ) )
	{
		document.write("No product information");
		return false;
	}

	insertOffer();
	
	isOldSystem = brCompatible();
	if ( isOldSystem == 'veryOldSystem')
	{
		text = 	'<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>' +
				'<p align = "center"><strong><font color' +
				'="#990000" size="2" face="Verdana, Aria' + 
				'l, Helvetica, sans-serif">Your browser ' +
				'or your platform are not supported by t' +
				'his application.<br> Please install a n' +
				'ewer browser and try again.</font></strong>';
			
		try 
		{
			document.write(text);
					
		}
		catch (ex)
		{
			text = 	'Your browser or your platform are not ' +
					'suppored by our application. Please us' +
					'e another browser or system.';
			alert(text);
		}
	}
	else
	{
		if (isOldSystem == 'oldSystem')
		{
			isOldSystem = true;
		}
		else if (isOldSystem == 'newSystem')
		{
			isOldSystem = false;
					
			css1 		= document.createElement("link");
			css1.rel 	= "stylesheet";
			css1.type 	= "text/css";
			css1.href 	= "styles/style_img.css";
			document.getElementsByTagName('head').item(0).appendChild(css1);
		}
		else
		{
			isOldSystem = true;
		}

		daoClient.setParameter(umslkup + ":product_id", product_id);
		daoClient.setParameter(umslkup + ":promotion_id", promotion_id);
		retval = daoClient.execute(umslkup);
	
		if ( !retval )
		{
			alert("ERROR: Invalid product.");
			return;
		}
	
		numrows = daoClient.getNumRows(umslkup);
	
		if (numrows < 1)
		{
			var text = 	'<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>' +
						'<p align = "center"><strong><font color' +
						'="#990000" size="2" face="Verdana, Aria' + 
						'l, Helvetica, sans-serif">Thisproduct d' +
						'oes not exist</font></strong>';
				document.write(text);
		}
		else 
		{
			daoClient.setParameter(style_stp + ":product_id", product_id);
			daoClient.setParameter(style_stp + ":promotion_id", promotion_id);
			//daoClient.setParameter(style_stp + ":promotion_id", promotion_id);
			retval = daoClient.execute(style_stp);

			if ( !retval )
			{
				alert("ERROR: Invalid Style Set Up.");
				return;
			}
  			
			layout_html = daoClient.getEventValue(style_stp + ":layout_html",0);     
  			//corresponds to an array in the templates.js file to get the html code for the layout from there
  			document.getElementById("layout").innerHTML = layout[layout_html].text; 
			setParameters();
  			setLayout();
			setOptionsList();
			setOptions();
		}	
	}
}
