// JScript File

function expandResult(product_id)
{
	document.getElementById('result_'+product_id+'_expand').style.display = 'none';
	document.getElementById('result_'+product_id+'_collapse').style.display = 'inline';
	document.getElementById('result_'+product_id+'_toggle').style.display = 'block';
	
	var width = Math.round((document.getElementById('searchResult_'+product_id).clientWidth - 40) / 4);
	if (width < 0)
	{
		width = 205;
	}
	 
	if (document.getElementById('result_'+product_id+'_toggle').innerHTML[0] != '<')
	{
		document.getElementById('result_'+product_id+'_toggle').innerHTML =
			'<div style="position:relative; left:0px; top:0px; background-color: White; padding: 2px; display: block" id="graph_menu_holder">'+ 
			'<table><tr align="center" style="padding: 3px">'+
			'<td><img id="graph_'+product_id+'_grav" src="/graph.php?id='+product_id+'&param=grav&maxdays=90&width='+width+'&height=200" border="0"/></td>'+
			'<td><img id="graph_'+product_id+'_eps" src="/graph.php?id='+product_id+'&param=eps&maxdays=90&width='+width+'&height=200" border="0"/></td>'+
			'<td><img id="graph_'+product_id+'_referred" src="/graph.php?id='+product_id+'&param=referred&maxdays=90&width='+width+'&height=200" border="0"/></td>'+
			'<td><img id="graph_'+product_id+'_commission" src="/graph.php?id='+product_id+'&param=commission&maxdays=90&width='+width+'&height=200" border="0"/></td>'+
			'</tr></table>'+
			'<div style="position:absolute; left:30px; top:2px; padding: 0px; display: block; font-size: x-large; opacity: 0.6; filter: alpha(opacity=60);" id="graph_menu_button" onmouseover="document.getElementById(\'graph_menu_'+product_id+'\').style.display=\'block\'; return true;">*</div>'+
			'<div style="position:absolute; left:0px; top:0px; background-color: White; padding: 2px; display: none" id="graph_menu_'+product_id+'" onmouseout="setFavMenuFadeTimeout(\''+product_id+'\'); return true;" onmouseover="clearFavMenuFadeTimeout(\''+product_id+'\'); return true;">'+
			'<ul class="cascadingMenu">'+
			'	<li class="cascadingMenuItem cascadingMenuItemFirst" style="position:absolute; top: 0px; left: 0px; width: 150px"><a href="#" onclick="setFavGraphTimespan(30, \''+product_id+'\'); return false;">1 month</a></li>'+
			'	<li class="cascadingMenuItem cascadingMenuItemNotFirst" style="position:absolute; top: 22px; left: 0px; width: 150px"><a href="#" onclick="setFavGraphTimespan(90, \''+product_id+'\'); return false;">3 months</a></li>'+
			'	<li class="cascadingMenuItem cascadingMenuItemNotFirst" style="position:absolute; top: 43px; left: 0px; width: 150px"><a href="#" onclick="setFavGraphTimespan(365, \''+product_id+'\'); return false;">1 year</a></li>'+
			'	<li class="cascadingMenuItem cascadingMenuItemNotFirst" style="position:absolute; top: 64px; left: 0px; width: 150px"><a href="#" onclick="setFavGraphTimespan(10000, \''+product_id+'\'); return false;">Max timespan</a></li>'+
			'</ul>'+
			'</div>'+
			'</div>'+
			'</div>';
	}
}

function collapseResult(product_id)
{
	document.getElementById('result_'+product_id+'_expand').style.display = 'inline';
	document.getElementById('result_'+product_id+'_collapse').style.display = 'none';
	document.getElementById('result_'+product_id+'_toggle').style.display = 'none';
}


function trackclick(linkid)
{
    if(document.images)
    { 
        (new Image()).src="/outclick.php?link=" + encodeURIComponent(linkid); 
    } 
    return true;
}

function init()
{
    if (window.location.href.indexOf("#") == -1)
    {
        document.searchForm.id.focus();
    }
    onQueryChange();
}

function cloakHoplink(cloak)
{
    var xmlHttp;
    var hopLink = document.getElementById("hopLink").value;
    
    if (!hopLink.match(/^http:\/\/\w+\.\w+\.hop.clickbank.net/))
    {
        alert("Initial value is not a hoplink");
        return false;
    }
    
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp= new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById("hopLink").value = "http://moveto.ws/" + xmlHttp.responseText;
        }
    }
    document.getElementById("hopLink").value = "Cloaking...";
    xmlHttp.open("GET", "/generate-link-api.php?url=" + escape(hopLink) + "&cloakreferer=" + cloak, true);
    xmlHttp.send(null);
    
    return false;
}

/// Advanced search options
function setOperator(operatorName, operatorValue)
{
	queryTextBox = document.forms.searchForm.searchQuery;
	tags = queryTextBox.value.split(" ");
	
	newTags = Array();
	curr = 0;
	
	if (operatorValue == "")
	{
		set = true;
	}
	else
	{
		set = false;
	}
	
	for (i in tags)
	{
		currTag = tags[i];
		if (i == "")
		{
			continue;
		}
		parts = currTag.split(":");
		if (parts.length > 1 && parts[0] == operatorName)
		{
			if (operatorValue != "")
			{
				newTags[curr++] = operatorName + ":" + operatorValue;
			}
			set = true;  
		}
		else
		{
			newTags[curr++] = currTag;
		}
	}
	
	if (!set)
	{
		newTags[curr++] = operatorName + ":" + operatorValue;
	}
	
	newText = newTags.join(" ");
	queryTextBox.value = newText;
}


function onRecurringBillingChange()
{
	form = document.forms.searchForm;
	if (form.recurring_yes.checked)
	{
		setOperator("r", "yes");
	}
	else if (form.recurring_no.checked)
	{
		setOperator("r", "no");
	}
	else
	{
		setOperator("r", "");
	}
}

function onGravChange()
{
	form = document.forms.searchForm;
	if (form.grav_min.value == "" && form.grav_max.value == "")
	{
		setOperator("g", "");  
	}
	else
	{
		setOperator("g", form.grav_min.value.toString() + ":" + form.grav_max.value.toString());
	}
}

function onGravDeltaChange()
{
	form = document.forms.searchForm;
	if (form.grav_delta_min.value == "" && form.grav_delta_max.value == "")
	{
		setOperator("gd", "");  
	}
	else
	{
		setOperator("gd", form.grav_delta_min.value.toString() + ":" + form.grav_delta_max.value.toString());
	}
}

function onEpsChange()
{
	form = document.forms.searchForm;
	if (form.eps_min.value == "" && form.eps_max.value == "")
	{
		setOperator("e", "");  
	}
	else
	{
		setOperator("e", form.eps_min.value.toString() + ":" + form.eps_max.value.toString());
	}
}

function onPrChange()
{
	form = document.forms.searchForm;
	if (form.pr_min.value == "" && form.pr_max.value == "")
	{
		setOperator("pr", "");  
	}
	else
	{
		setOperator("pr", form.pr_min.value.toString() + ":" + form.pr_max.value.toString());
	}
}

function onTrafChange()
{
	form = document.forms.searchForm;
	if (form.traf_min.value == "" && form.traf_max.value == "")
	{
		setOperator("t", "");  
	}
	else
	{
		setOperator("t", form.traf_min.value.toString() + ":" + form.traf_max.value.toString());
	}
}

function onGoogleSeoChange()
{
	form = document.forms.searchForm;
	if (form.google_seo_min.value == "" && form.google_seo_max.value == "")
	{
		setOperator("sg", "");  
	}
	else
	{
		setOperator("sg", form.google_seo_min.value.toString() + ":" + form.google_seo_max.value.toString());
	}
}

function onMsnSeoChange()
{
	form = document.forms.searchForm;
	if (form.msn_seo_min.value == "" && form.msn_seo_max.value == "")
	{
		setOperator("sm", "");  
	}
	else
	{
		setOperator("sm", form.msn_seo_min.value.toString() + ":" + form.msn_seo_max.value.toString());
	}
}

function onQueryChange()
{
	form = document.forms.searchForm;
	query = form.searchQuery.value;
	
	tags = query.split(" ");
	operators = Array();
	
	for (i in tags)
	{
		currTag = tags[i];
		if (i == "")
		{
			continue;
		}
		
		
		parts = currTag.split(":");
		
		if (parts.length < 2)
		{
			continue;
		}
		
		operatorName = parts[0];
		
		operatorValues = Array();
		operatorValues[0] = parts[1];

		if (parts.length > 2)
		{
			operatorValues[1] = parts[2];
		}
		
		operators[operatorName] = operatorValues;
	}
	
	all_operators = Array();
	all_operators["g"] = "grav";
	all_operators["gd"] = "grav_delta";
	all_operators["e"] = "eps";
	all_operators["pr"] = "pr";
	all_operators["t"] = "traf";
	all_operators["sg"] = "google_seo";
	all_operators["sm"] = "msn_seo";
	
	for (op in all_operators)
	{
		opControlPrefix = all_operators[op];
		opValues = operators[op];
		if (opValues == null)
		{
			opMin = "";
			opMax = "";
		}
		else
		{
			opMin = opValues[0];
			if (opValues.length > 1)
			{
				opMax = opValues[1];
			}
			else
			{
				opMax = "";
			}
		}
		
		form[opControlPrefix + "_min"].value = opMin;
		form[opControlPrefix + "_max"].value = opMax;
	}
	
	if (operators["r"] != null)
	{
		switch (operators["r"][0])
		{
			case "yes":
				form.recurring_yes.checked = true;
				break;
			case "no":
				form.recurring_no.checked = true;
				break;
			default:
				form.recurring_either.checked = true;
				break;
		}
	}
	else
	{
		form.recurring_either.checked = true;
	}
}

function setMenuFadeTimeout()
{
	if (typeof(menuFadeTimer) != "undefined" && menuFadeTimer != '')
	{
		clearTimeout(menuFadeTimer);
		menuFadeTimer = '';
	}
	
	menuFadeTimer = setTimeout("document.getElementById('graph_menu').style.display='none'", 750);
}

function clearMenuFadeTimeout()
{
	if (typeof(menuFadeTimer) != "undefined" && menuFadeTimer != '')
	{
		clearTimeout(menuFadeTimer);
		menuFadeTimer = '';
	}
}

function setGraphTimespan(ts, product_id)
{
	document.getElementById('graph_grav').src="/graph.php?id=" + product_id + "&param=grav&maxdays="+ts+"&width=345&height=200"; 
	document.getElementById('graph_eps').src="/graph.php?id=" + product_id + "&&param=eps&maxdays="+ts+"&width=345&height=200";
	document.getElementById('graph_referred').src="/graph.php?id=" + product_id + "&&param=referred&maxdays="+ts+"&width=345&height=200";
	document.getElementById('graph_commission').src="/graph.php?id=" + product_id + "&&param=commission&maxdays="+ts+"&width=345&height=200";
	
	document.getElementById('graph_menu').style.display='none';
}

function setCbiGraphTimespan(ts, product_id)
{
	document.getElementById('graph_cbi').src="/graph.php?id=" + product_id + "&param=cbindex&maxdays="+ts+"&width=300&height=250"; 
	
	document.getElementById('graph_menu').style.display='none';
}

function setFavMenuFadeTimeout(product_id)
{
	if (typeof(menuFadeTimer) != "undefined" && menuFadeTimer != '')
	{
		clearTimeout(menuFadeTimer);
		menuFadeTimer = '';
	}
	
	menuFadeTimer = setTimeout("document.getElementById('graph_menu_"+product_id+"').style.display='none'", 750);
}

function clearFavMenuFadeTimeout(product_id)
{
	if (typeof(menuFadeTimer) != "undefined" && menuFadeTimer != '')
	{
		clearTimeout(menuFadeTimer);
		menuFadeTimer = '';
	}
}

function setFavGraphTimespan(ts, product_id)
{
	document.getElementById('graph_'+product_id+'_grav').src="/graph.php?id=" + product_id + "&param=grav&maxdays="+ts+"&width=205&height=200"; 
	document.getElementById('graph_'+product_id+'_eps').src="/graph.php?id=" + product_id + "&&param=eps&maxdays="+ts+"&width=205&height=200";
	document.getElementById('graph_'+product_id+'_referred').src="/graph.php?id=" + product_id + "&&param=referred&maxdays="+ts+"&width=205&height=200";
	document.getElementById('graph_'+product_id+'_commission').src="/graph.php?id=" + product_id + "&&param=commission&maxdays="+ts+"&width=205&height=200";
	
	document.getElementById('graph_menu_'+product_id).style.display='none';
}

function getGraphHtml(product_id, title)
{
	document.getElementById('graph_menu').style.display='none';
	
	if (document.getElementById('graph_code_grav').checked)
	{
		id = 'graph_grav';
	}
	else if (document.getElementById('graph_code_eps').checked)
	{
		id = 'graph_eps';
	}
	else if (document.getElementById('graph_code_referred').checked)
	{
		id = 'graph_referred';
	}
	else if (document.getElementById('graph_code_commission').checked)
	{
		id = 'graph_commission';
	}
	else
	{
		id = 'graph_grav';
	}
	
	document.getElementById('graph_html_code').style.display='block';			
	document.getElementById('graph_html_code_text').value =
		'<a href="http://www.cb-analytics.com/product.php?id=' + product_id + '&" title="<?=htmlentities($title)?>">' + 
		'<img src="' + document.getElementById(id).src + '" alt="' + title + '"/></a>';
}

function selectAllText(control_id)
{
	var text_val=document.getElementById(control_id);
	text_val.focus();
	text_val.select();
}

function addToFavorites(productId, add)
{
	try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp= new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    
    xmlHttp.onreadystatechange=function()
    {
        if(xmlHttp.readyState==4)
        {
            document.getElementById("nrOfFavorites").innerHTML = xmlHttp.responseText;
            document.getElementById("addToFavorites_" + productId).style.color = add?'Red':'Black';
        }
    }
    xmlHttp.open("GET", "/personalize-api.php?mode=add_"+add+"&product=" + productId + "&nc=" + Math.random(), true);
    xmlHttp.send(null);
	
}

function contactEmail()
{
	document.write('<a href="mailto:'+'admin'+'@'+'cb-analytics.com'+'">'+'admin'+'@'+'cb-analytics.com</a>');
}
