// SET ROOT
//var root = 'http://office/compareGroup/public_html/';
var root = 'http://www.compare-perfume.com/';

/* ### PERFUME AJAX ### */

function checkClear( value )
{
	if( value == 'Type in a brand' )
	{
		document.getElementById( 'typeBrand' ).value = '';
	}
	if( value == '' )
	{
		document.getElementById( 'typeBrand' ).value = 'Type in a brand';
	}
	//document.getElementById( 'applePopup' ).style.display = "none";
}

function fillBrandNameSelect( type )
{
	var url = root + "index/perfume/jsPerfumeBrandNameGrab/";
	request.open( "GET", url, true );
	request.onreadystatechange = goBrandName2;
	request.send( null );
}

function fillBrandPopup( type )
{
	var url = root + "index/perfume/jsPerfumeBrandNameGrab/" + type + "/";
	request.open( "GET", url, true );
	request.onreadystatechange = goBrandName;
	request.send( null );
}

function fillProductNameSelect( dbId )
{
	if( dbId != "--" )
	{
		document.getElementById( 'applePopup' ).style.display = "block";
		var string = '<div style="width: 100%; background: #852388; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Step Two - Select A Product</h4><div style="float: right; margin-right: 16px; margin-top: -19px;"><a href="#" onclick="javascript:document.getElementById( \'applePopup\' ).style.display = \'none\';" class="closeMeLink">close this search window [x]</a></div></div>';
		string = string + '<br /><div style="width: 100%; height: 40px; text-align: center; font-family: Trebuchet MS, Arial, Serif; font-weight: bold; color: #852388;"><img src="' + root + 'js/plugins/perfume/globe.gif" style="border: none; position: relative; top: 3px; height: 15px; width: 15px;" /> Loading Products...</div>';
		document.getElementById( 'applePopup' ).innerHTML = string;
		setTimeout( 'fillProductNameSelect2( \'' + dbId + '\' );', 1000 );
	} else {
		document.getElementById( 'applePopup' ).style.display = "none";
	}
}

function fillProductNameSelect2( dbId )
{
	var url = root + "index/perfume/jsPerfumeProductNameGrab/" + dbId + "/";
	request.open( "GET", url, true );
	request.onreadystatechange = goProductName;
	request.send( null );
}

function goBrandName( ) {
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var response = request.responseText;
			
			if( response != '' )
			{
				var cats = response.split( '##' );
				document.getElementById( 'applePopup' ).style.display = "block";
				var string = '<div style="width: 100%; background: #852388; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Step One Continued...</h4><div style="float: right; margin-right: 16px; margin-top: -19px;"><a href="#" onclick="javascript:document.getElementById( \'applePopup\' ).style.display = \'none\';" class="closeMeLink">close this search window [x]</a></div></div>';
				for ( i=0; i<cats.length; i++ )
				{
					var products = cats[i].split( '|' );
					if( cats[i] != '' )
					{
						switch( i )
						{
							case 0:
								string = string + '<div style="width: 100%; background: #BD83F3; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Product Matches</h4></div>';
								break;
							case 3:
								string = string + '<div style="width: 100%; background: #BD83F3; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Other Matches</h4></div>';
								break;
							default:
								break;
						}
					}
					for ( x=1; x<products.length; x++ )
					{
						string = string + '<ul>';
							var variables = products[x].split( '::' );
							string = string + '<li style="color: #555555; padding: 5px 5px 5px 10px; width: 100%;"><a href="javascript:fillProductNameSelect( \'' + variables[0] + '\' );" class="dropDownItem">' + variables[1] + '</a></li>';
						string = string + '</ul>';
					}
				}
				if( response == '######' )
				{
					string = string + '<ul>';
						string = string + '<li style="color: #555555; padding: 5px 5px 5px 10px; width: 100%;"><strong style="color: #852388;">Your search found no results!</strong><br />Please refine your perfume search above.</li>';
					string = string + '</ul>';
				}
				
				string = string + '<div style="width: 100%; background: #852388; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #852388;">Step Two - Select A Product</h4><div style="float: right; margin-right: 16px; margin-top: -19px;"><a href="#" onclick="javascript:document.getElementById( \'applePopup\' ).style.display = \'none\';" class="closeMeLink">close this search window [x]</a></div></div>';
				
				document.getElementById( 'applePopup' ).innerHTML = string;
			} else {
				document.getElementById( 'applePopup' ).style.display = "none";
			}
			if( document.getElementById( 'typeBrand' ).value == '' )
			{
				document.getElementById( 'applePopup' ).style.display = "none";
			}
		}
	}
}

function goBrandName2( ) {
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var response = request.responseText;
			
			if( response != '' )
			{
				var cats = response.split( '##' );
				var string = '';
				var list = document.getElementById( 'selectBrand' );
				var m = document.createElement( 'option' );
	   			m.setAttribute( 'value', '--' );
   				var n = document.createTextNode( '...OR Select A Brand Here' );
   				m.appendChild( n );
   				list.appendChild( m );
   				
				for ( i=0; i<cats.length; i++ )
				{
					var products = cats[i].split( '|' );
					for ( x=1; x<products.length; x++ )
					{
						var variables = products[x].split( '::' );
						//var list = document.getElementById( 'selectBrand' );
						for ( b=1; b<variables.length; b++ )
						{
							var m = document.createElement( 'option' );
				   			m.setAttribute( 'value', variables[0] );

			   				var n = document.createTextNode( variables[1] );

			   				m.appendChild( n );
			   				list.appendChild( m );
						}
					}
				}
			}
		}
	}
}

function goProductName( ) {
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var response = request.responseText;
			
			if( response != '' )
			{
				document.getElementById( 'applePopup' ).style.display = "block";
				var string = '<div style="width: 100%; background: #852388; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Step Two - Select A Product</h4><div style="float: right; margin-right: 16px; margin-top: -19px;"><a href="#" onclick="javascript:document.getElementById( \'applePopup\' ).style.display = \'none\';" class="closeMeLink">close this search window [x]</a></div></div>';
				var products = response.split( '|' );
				string = string + '<div style="width: 100%; background: #BD83F3; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #FFFFFF;">Products & Retailers...</h4></div>';
				for ( x=1; x<products.length; x++ )
				{
					string = string + '<ul>';
						var prices = products[x].split( '##' );
						if( prices[0] != '' )
						{
							string = string + '<li style="color: #555555; padding: 5px 5px 5px 10px; width: 100%;"><span class="dropDownItem" style="font-weight: bold; color: #852388; font-size: 11px;">' + prices[0] + '</span>';
							for ( g=1; g<prices.length; g++ )
							{
								var variables = prices[g].split( '::' );
								string = string + '<br /><a href="' + variables[3] + '" target="_blank"><img src="' + root + 'js/plugins/perfume/buyNow.gif" style="border: none; position: relative; top: 2px;" /> <span style="color: #555555;">' + variables[1] + ' - </span><span style="color: #C10202;">&pound;' + variables[2] + '</span></a>';
							}
							string = string + '</li>';
						}
					string = string + '</ul>';
				}
				
				string = string + '<div style="width: 100%; background: #852388; height: 20px; padding: 3px 2px 0px 10px;"><h4 style="font-family: Trebuchet MS, Arial, Serif; font-size: 12px; font-weight: bold; color: #852388;">Step Two - Select A Product</h4><div style="float: right; margin-right: 16px; margin-top: -19px;"><a href="#" onclick="javascript:document.getElementById( \'applePopup\' ).style.display = \'none\';" class="closeMeLink">close this search window [x]</a></div></div>';
				
				document.getElementById( 'applePopup' ).innerHTML = string;
			} else {
				document.getElementById( 'applePopup' ).style.display = "none";
			}
			if( document.getElementById( 'typeBrand' ).value == '' )
			{
				document.getElementById( 'applePopup' ).style.display = "none";
			}
		}
	}
}

function goMerchantName( ) {
	if( request.readyState == 4 )
	{
		if( request.status == 200 )
		{
			var moo = request.responseText;
			var merchants = moo.split( '|' );
			var tempString = '';
			
			for ( i=1; i<merchants.length; i++ )
			{
				var merchant = merchants[i].split( '::' );
				tempString = tempString + "<a href=\"" + merchant[2] + "\" target=\"_blank\" class=\"merchantPrice\">" + merchant[0] + " - <strong>&pound;" + merchant[1] + "</strong></a><br />";
			}
			
			document.getElementById( 'perfumeProductsMerchants' ).innerHTML = tempString;
		}
	}
}

function initPerfumeBrand2( type )
{
	fillBrandPopup( type );
}

function initPerfumeBrand( )
{
	var list = document.getElementById( "selectBrand" );
	while( list.childNodes[0] )
	{
		list.removeChild( list.childNodes[0] )
	}
	fillBrandNameSelect( );
}

function initPerfumeProduct( )
{
	var brandName = document.getElementById( "selectBrand" );
	brandName.onchange = function( )
	{
	 	if( this.value != "" )
	 	{
	  		var list = document.getElementById( "selectProduct" );
	  		while( list.childNodes[0] )
	  		{
				list.removeChild( list.childNodes[0] )
			}
	  		fillProductNameSelect( this.value );
	  	}
	}
	fillProductNameSelect( brandName.value );
}

/* ### AJAX CALL ### */

	var request = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e2) {
		request = false;
	}
	}
	@end @*/
	if (!request && typeof XMLHttpRequest != 'undefined') {
		request = new XMLHttpRequest();
	}
