var x = 0;
function init(text)
{
	try
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(text);
	}
	catch(e)
	{
		try
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
		}
		catch(e)
		{
			alert(e.message);
			return;
		}
	}
	return xmlDoc;
}

function ajaxFunction()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		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;
			}
		}
	}
	return xmlHttp;
}


function setcredit()
{
	var attempt = Number(x) + 1;
	var xmlHttp=ajaxFunction();
	if(xmlHttp)
	{
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState==4)
			{
				if(xmlHttp.responseText)
				{
					temp = xmlHttp.responseText.split('##');

					if(temp[2] == 1 && attempt == 1)
					{
						Header();
					}
					else
					{
						document.getElementById('welcomeuser').innerHTML = ' to <strong>HelloTrade!</strong>';
						document.getElementById('nhavbcrd').style.display = "none";	
					}
				}
			}
		}

		var params="";
		var str = '/cgi-bin/ht-setcredit.mp';
		xmlHttp.open("POST",str,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}
}

function Header()
{
	var value = "";
	value = readCookie('v4iil');
	var url = document.URL;
	var redirect = escape(url);
	
	if(value == null || value == "")
	{
		if(document.getElementById('signinornot'))
		{
		document.getElementById('signinornot').innerHTML='<span id="signinornot"><a href="http://my.hellotrade.com/cgi-bin/ht-joinfree.mp?action=joinfree&modid=HELLOTD&redirect='+redirect+'" rel="nofollow">Join&nbsp;Free</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a HREF="http://my.hellotrade.com/cgi-bin/ht-usr-login.mp?login=true&modid=HELLOTD&redirect='+redirect+'" rel="nofollow" target="_top">Login</a></span>';
		}
	}
	else
	{
		if(document.getElementById('signinornot'))
		{
		document.getElementById('signinornot').innerHTML='<span id="signinornot"><A href="http://my.hellotrade.com/" target="_top">My Hellotrade</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href="http://my.hellotrade.com/cgi-bin/ht-usr-login.mp?logoff=true&modid=HELLOTD&redirect='+redirect+'" target="_top">Logout</a></span>';
		}
	}

	var user = getUserName();
	if(user != '')
	{
		document.getElementById('welcomeuser').innerHTML = '<strong>'+user+'</strong>';
	}
	else
	{
		document.getElementById('welcomeuser').innerHTML = ' to <strong>HelloTrade!</strong>';
	}
	
	var crtavl = getAvlCredit();
	
	if(crtavl != -1)
	{
		crtavl = Math.floor(crtavl / 5);
		document.getElementById('nhavbcrd').innerHTML = 'Available Balance : <strong>'+crtavl+' US$</strong>';
	}
	else if(value)
	{
		if(crtavl == -1)
		{
			setcredit();
		}
	}
	else
	{
		document.getElementById('nhavbcrd').style.display = "none";	
	}
	
}

function getUserName()
{
	var value = "";
	var value = readCookie('htusr');
	if(value == null || value == "")
	{
		value = '';
	}
	else
	{
		value = unescape(value);
	}
	return value;
}

function getAvlCredit()
{
	var value = "";
	value = readCookie('htavlcrdt');

	if(value == null || value == "")
	{
		value = -1;
	}
	else
	{
		value = unescape(value);
	}
	return value;
}

function Footer()
{
	var value = "";
	value = readCookie('v4iil');
	var url = document.URL;
	var redirect = escape(url);
	

	if(value == null || value == "")
	{
		if(document.getElementById('signinornot1'))
		{
		document.getElementById('signinornot1').innerHTML='<span id="signinornot1"><a href="http://my.hellotrade.com/cgi-bin/ht-joinfree.mp?action=joinfree&modid=HELLOTD" rel="nofollow">Join&nbsp;Free</a> | <a HREF="http://my.hellotrade.com/cgi-bin/ht-usr-login.mp?login=true&modid=HELLOTD&redirect='+redirect+'" target="_top" rel="nofollow">Login</a></span>';
		}
	}
	else
	{
		if(document.getElementById('signinornot1'))
		{
		document.getElementById('signinornot1').innerHTML='<span id="signinornot1"><a href="http://my.hellotrade.com/" target="_top">My Hellotrade</a> | <a href="http://my.hellotrade.com/cgi-bin/ht-usr-login.mp?logoff=true&modid=HELLOTD&redirect='+redirect+'" target="_top">Logout</a></span>';
		}
	}
}


function readCookie(name)
{
	var ca = document.cookie.split(';');
	var nameEQ = name + "=";
	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 check_advanced_search(flag)
{
	var str='';
        var from = '';
	if(flag == 1)
	{
		form1 = document.quick_find;
		from = 'SITEWISE TOP SEARCHBOX';
	}
	else
	{
		form1 = document.quick_find1;
		from = 'SITEWISE BOTTOM SEARCHBOX';
	}

        form1.ss.value = form1.ss.value.replace(/^\s+/g, '').replace(/\s+$/g, '');
	while(form1.ss.value.indexOf('  ')>0)
	{
		form1.ss.value = form1.ss.value.replace('  ',' ');	
	}
	form1.ss.value = form1.ss.value.replace(/[^a-zA-Z0-9+ ]/g, ' ');
	
	var temp=form1.ss.value.replace(/\s/g, '');
        if (form1.ss.value.length < 3)
        {
                alert("Enter at least three characters for search.");
                form1.ss.focus();
                return false;
        }
        else
        {
		_gaq.push(['_trackEvent', 'Search',from, form1.ss.value]);
		var a = form1.ss.value.replace(/\+/g, ' ');
		a = a.replace(/\s+/g, '+');
		str +='ss='+a;
		str = "http://www.hellotrade.com/suppliers?"+str;
		str = myReplace(str,"\\\\?\\\\&","?");
		window.location = str;
		return false;
	}
}

function myReplace(str, a, b) {
        var re = new RegExp(a, "g");
        var ret = str.replace(re,b);
        return ret;
}

