function createRequestObject()
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();





var timeout = 400;
var timer = 0;
var menu = 0;

function drop(obj)
{
	if(menu) { menu.style.visibility = 'hidden'; }
	menu = document.getElementById(obj);
	menu.style.visibility = 'visible';
}

function hmenu(obj)
{
	if(menu) { menu.style.visibility = 'hidden'; }	
}

function ntimer()
{
	timer = window.setTimeout(hmenu, timeout);
}

function ctimer()
{
	if(timer) { window.clearTimeout(timer); }
}

function toggle(obj)
{
	var item = document.getElementById(obj);
	if(item.style.visibility == 'visible') { item.style.visibility = 'hidden'; }
	else { item.style.visibility = 'visible'; }
}


sfHover = function() {
	// if you only have one main menu - delete the line below //
	var sfEls1 = document.getElementById("subnav").getElementsByTagName("li");
	//

	// if you only have one main menu - delete the "for" loop below //
	for (var i=0; i<sfEls1.length; i++) {
		sfEls1[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover1\\b"), "");
		}
	}
	//

}
if (window.attachEvent) window.attachEvent("onload", sfHover);






function addInvite()
{
	http.open('get', '/elements.php?mode=invite');
	element = 'invite_row';
    http.onreadystatechange = handleResponse;
    http.send(null);
}



function addProduct(add_prod, tool_type, tool_id)
{
	var add_url = escape(add_prod.add_url.value);
	var name = escape(add_prod.name.value);
	var image = escape(add_prod.image.value);
	var vendor = escape(add_prod.vendor.value);
	var dropdownIndex = add_prod.new_cat_id.selectedIndex;
	var cat_id = add_prod.new_cat_id[dropdownIndex].value;

	if(name == '')
	{
		alert("Please enter a component name");
		return;
	}
	if(vendor == '')
	{
		alert("Please enter the site name");
		return;
	}

	http.open('get', '/lists.php?op=add_prod&list_id='+tool_id+'&name='+name+'&vendor='+vendor+'&cat_id='+cat_id+'&add_url='+add_url+'&image='+image);
	element = 'list_items';

    http.onreadystatechange = handleResponse;
    http.send(null);

	var url_results = document.getElementById('url_results');
	url_results.innerHTML = "Component added to your <b>System Components</b>. Add another by pasting another URL above.";
	var url_search = document.getElementById('url_search');
	url_search.value = '';
	url_search.focus();
}



function chooseProduct(prod_id, list_id)
{
	http.open('get', '/lists.php?op=add_prod&prod_id='+prod_id+'&list_id='+list_id);
	element = 'list_items';
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function removeProduct(prod_id, list_id)
{
	http.open('get', '/lists.php?op=del_prod&prod_id='+prod_id+'&list_id='+list_id);
	element = 'list_items';
    http.onreadystatechange = handleResponse;
    http.send(null);
}



function showSearch(tool_id, tool_type)
{
	http.open('get', '/category.php?tool_id='+tool_id+'&tool_type='+tool_type+'&mode=choose');
	element = 'prod_search';
    http.onreadystatechange = handleResponse;
    http.send(null);
}



function findURL(tool_id, tool_type)
{
	var post_system = document.getElementById('post_system');
	var url = escape(post_system.url_search.value);

	if(url == '')
	{
		alert("Please enter the URL to your component");
		return;
	}

    http.open('get', '/shop/add_prod/?tool_id='+tool_id+'&tool_type='+tool_type+'&add_url='+url);
	element = 'url_results';
	
	var loading = document.getElementById(element);
	loading.innerHTML = "<center><br><br><img src='/images/loading.gif'><br><br><br><br></center>";

    http.onreadystatechange = handleResponse;
    http.send(null);
}

function findProducts(tool_id, tool_type)
{
	var post_system = document.getElementById('post_system');
	var query = escape(post_system.list_search.value);
	var dropdownIndex = post_system.search_cat_id.selectedIndex;
	var cat_id = post_system.search_cat_id[dropdownIndex].value;

	if(query == '')
	{
		alert("Please enter a product search");
		return;
	}

    http.open('get', '/shop/?tool_id='+tool_id+'&tool_type='+tool_type+'&cat_id='+cat_id+'&pmode=choose&search_query='+query);
	element = 'prod_results';

	var loading = document.getElementById(element);
	loading.innerHTML = "<center><br><br><img src='/images/loading.gif'><br><br><br><br></center>";

    http.onreadystatechange = handleResponse;
    http.send(null);
}






function toggleComponent(mode)
{
	var fromHawkee = document.getElementById('fromHawkee');
	var fromInternet = document.getElementById('fromInternet');

	if(mode == 'hawkee')
	{
		fromHawkee.style.display = 'block';
		fromInternet.style.display = 'none';
	}
	else
	{
		fromHawkee.style.display = 'none';
		fromInternet.style.display = 'block';
	}
}






function submitVote(tool_id, tool_type, vote_delete)
{
	var url = '/submit_vote.php?tool_id='+tool_id+'&tool_type='+tool_type;
	if(vote_delete)
	{
		url = url+'&op=delete';
	}
	else
	{
		url = url+'&op=save';
	}
	http.open('get', url );
	element = 'score_'+tool_id+"_"+tool_type;
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function showComment(comment_id)
{
	http.open('get', '/profile/comments.php?mode=one_comment&comment_id='+comment_id);
	element = 'comment_'+comment_id;
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function editComment(comment_id)
{
	http.open('get', '/profile/comments.php?mode=edit&comment_id='+comment_id);
	element = 'comment_'+comment_id;
	http.onreadystatechange = handleResponse;
	http.send(null);
}

var new_comment_id = 1;

function saveComment(comment_id, position)
{
	var url = '/profile/comments.php';
	var poststr = 'mode=one_comment&op=save';

	// Replace the comment div if it exists, otherwise create
	// a new div and either prepend or append it.

	if(comment_id)
	{
		var obj = document.getElementById('save_' + comment_id);
		element = 'comment_'+comment_id;
		poststr = poststr + '&comment_id=' + comment_id;
	}
	else
	{
		var obj = document.getElementById('new_comment_form');
		var comment_list = document.getElementById('comment_list');

		var new_id = 'new_comment_' + new_comment_id;
		new_comment_id++;

		var new_comment = document.createElement("div");
		new_comment.setAttribute("id", new_id);
		element = new_id;

		new_comment.innerHTML = "<center><br><br><img src='/images/loading.gif'><br><br><br></center>";

		// Append to the top or bottom of the comment list.

		if(position == "top")
		{
			var p2 = comment_list.getElementsByTagName('div')[0];

			if(p2)
			{
				p2.parentNode.insertBefore(new_comment,p2);
			}
			else
			{
				// If there are no comments yet.
    			comment_list.appendChild(new_comment);
			}
		}
		else
		{
    		comment_list.appendChild(new_comment);
		}	

		var change_textarea = true;

	}

	// Sanitize the form values for the URL.

	var comment_text;
	for( var i = 0; i < obj.elements.length; i++ ) 
 	{ 
		name = obj.elements[i].name;
		value = obj.elements[i].value;

		if(obj.elements[i].type == 'checkbox')
		{
			value = obj.elements[i].checked;
		}

		if(name == 'comment')
		{
			comment_text = value;
			poststr = poststr + "&comment=" + encodeURIComponent(value);
		}
		else
		{
			poststr = poststr + "&" + name + "=" + value;
		}
 	}

	if(comment_text.length == 0 || comment_text == 'Enter comment here..')
	{
		alert("Please enter a comment.");
		if(new_comment)
		{
			new_comment.innerHTML = null;
		}	
		return;
	}

	http.open('POST', url, true);
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", poststr.length);
    http.setRequestHeader("Connection", "close");
	http.onreadystatechange = handleResponse;
    http.send(poststr);

	if(change_textarea)
	{
		var textarea = obj.getElementsByTagName('textarea')[0];
		textarea.value = 'Enter comment here..';
	}	
}

function deleteComment(comment_id)
{
	if(confirm('Are you sure you want to permanently remove this comment?'))
	{
		http.open('get', '/profile/comments.php?op=del&comment_id='+comment_id);
		element = 'comment_'+comment_id;
		
		var spinner = document.getElementById(element);
		spinner.innerHTML = "<center><img src='/images/loading.gif'></center>";

		http.onreadystatechange = handleResponse;
		http.send(null);
	}	
}

function handleResponse()
{
    if(http.readyState == 4)
	{
        var response = http.responseText;
		var destination = document.getElementById(element);
		destination.style.height = 'auto';
		destination.innerHTML = response;
    }
}

var state = 'none';

function showhide(layer_ref)
{
	if (state == 'block') { state = 'none'; }
	else { state = 'block'; }

	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}
	if (document.getElementById &&!document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}

function showhidestatus()
{
	var comment_form = document.getElementById('new_comment_form'); 

	var state = 'none';

	for (var i = 0; i < comment_form.elements.length; i++ ) {
		if (comment_form.elements[i].type == 'checkbox') {
			if (comment_form.elements[i].checked == true) {
				state = 'block';
			}
		}
	}	

	var status_update = document.getElementById('status_update');
	status_update.style.display = state;
}

/* */
// Displays a hidden chunk of review or hides it.

var toggles = {};

function toggle_review(divID, buttonID, dotID)
{
	if(typeof toggles[divID] == 'undefined' || toggles[divID] == 'hidden')
	{
    	document.getElementById(divID).style.display = 'inline';
    	document.getElementById(dotID).style.display = 'none';
		buttonID.innerHTML = 'hide';
		toggles[divID] = 'visible';
	}
	else
	{
    	document.getElementById(divID).style.display = 'none';
    	document.getElementById(dotID).style.display = 'inline';
		buttonID.innerHTML = 'more';
		toggles[divID] = 'hidden';
	}
}

function copyToClipboard(field)
{
    var content = eval("document."+field)
    content.focus()
    content.select()
    range = content.createTextRange()
    range.execCommand("Copy")
    window.status="Contents copied to clipboard"
    setTimeout("window.status=''",1800)
}

function fnSelect(objId) {
	fnDeSelect();
	if (document.selection) {
		var range = document.body.createTextRange();
		range.moveToElementText(document.getElementById(objId));
		range.select();
	}
	else if (window.getSelection) {
		var range = document.createRange();
		range.selectNode(document.getElementById(objId));
		window.getSelection().addRange(range);
	}
    range.execCommand("Copy")
}

function fnDeSelect() {
	if (document.selection) document.selection.empty(); 
	else if (window.getSelection) window.getSelection().removeAllRanges();
}

