/*
* Common javascript functions on site
*
* 2010-08-19 --JA
*/

function popit(url,w,h)
{
	if(!w) w = 400;
	if(!h) h = 500;

	if (url)
	{
		$.fn.colorbox({width:w + "px", height: h + "px", iframe:true, href:url, scalePhotos:false});	
		return false;
	}
}

function showTip(item)
{
	tips = new Array();
	
	tips['quote'] = 'Request a Quote';
	tips['drawing'] = 'Request a technical drawing';
	tips['panel'] = 'Download a panel cutout';
	tips['pdf'] = 'Download a 3D PDF';
	tips['cart'] = 'Purchase this item';
	tips['email'] = 'Email this page';
	tips['none'] = '&nbsp;';
	if(tips[item] == undefined) item = 'none';
	document.getElementById('toolbartip').innerHTML = tips[item];
}

function specRequest(part)
{
	popit('/cgi-bin/site/user.cgi/request/info?part_number=' + part);
}

function quoteRequest(part)
{	
	f = document.forms[0];
	var sizes = '';
	var sizeandprice;
	for(var i = f.sizeselect.options.length > 1? 1: 0; i < f.sizeselect.options.length; i++)
	{
		sizeandprice = f.sizeselect.options[i].value.split("|"); 
		sizes += '&sizes=' + sizeandprice[0];
	}
	mapSizetoItem(f.size.options[f.size.options.selectedIndex])
	popit('/cgi-bin/site/user.cgi/request/quote?part_number=' + f.itemsize.value + sizes);
}

function downloadDoc(part, type, available, request)
{
	ww = 550;
	hh = 600;
	if(available)
	{
		if(type == 'cutout') { ww = ''; hh = ''; }
		popit('/cgi-bin/site/user.cgi/download/show' + type + 's?part_number=' + part,ww,hh);
	}
	else
	{
		
		if(request)
		{
			if($('#colorbox').css('display') != 'none') $.colorbox.close()	
			specRequest(partnum);
		}
		else
		{
			alert('No ' + type + ' available for this product');
		}
		
	}
}

function emailItem(part)
{
	popit('/cgi-bin/site/user.cgi/sendtofriend/index?part_number=' + part + '&orig_url=' + document.location.href);
}

function mapSizetoItem(size)
{
	f = document.forms[0];
	var sizeandprice = $(size).val().split("|"); 
	var backorder = $(size).attr('backorder');
	
	f.itemsize.value = sizeandprice[0];
	f.itemprice.value = sizeandprice[1];
	if(backorder)
	{
		f.itemname.value = f.iteminfo.value + ' - ' + ' * Backorder until ' + backorder + ' * - ' + sizeandprice[0];
	}
	else
	{
		f.itemname.value = f.iteminfo.value + ' - ' + sizeandprice[0];
	}

	if(backorder)
	{
		$('#buybuttontip').html(sizeandprice[0].toUpperCase());
		if($('#backorder_alert').length==0)
		{
			$('#buybuttontip').after('<div id="backorder_alert">* This size on Backorder until ' + backorder + '</div>');
		}
		else
		{
			$('#backorder_alert').show();
		}
	}
	else 
	{
		$('#buybuttontip').html(sizeandprice[0].toUpperCase());
		$('#backorder_alert').hide();
	}
}
  
function addCart()
{
	f = document.forms[0];
	if(f.sizeselect.options.length > 1 && f.sizeselect.options.selectedIndex == 0)
	{
		alert("Please select a size to order");
		return false;
	}
	mapSizetoItem(f.size.options[f.size.options.selectedIndex])
	f.submit();
}

function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );

}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


