function $(name)
{
	return document.getElementById(name);	
}

function trim(v)
{
	return v.replace(/^\s*|\s*$/g,'');
}

function setValues(p, n, v)
{
	if(p == '')
	{
		return n + '=' + urlEncode(v);
	}
	else
	{
		return p + '&' + n + '=' + urlEncode(v);
	}
}

function urlEncode(v)
{
	return escape(v).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

function limite_character(obj, show, limit) 
{
	var typed = 0;
	var left = 0;

	typed = obj.value.length;

	left = limit - typed;
	if (left >= 0) {
		$(show).innerHTML = typed + ' of ' + limit + ' characters';
	}
	else {
		$(show).innerHTML = 'You have exceed the limit of ' + limit + ' characters';
	}
}

function submitReview(frm)
{
	var pro_id = $('pro_id').value;
	var stars = $('stars').value;
	var short_summary = trim($('short_summary').value);
	var long_summary = trim($('long_summary').value);
	var captcha = $('captcha').value;
	
	var err_short_summary = '';
	var err_long_summary = '';
	var err_stars = '';
	
	if (stars <= 0)
	{
		err_stars = 'Error: Please select a star rating.';		
	}
	
	if (short_summary.length < 10)
	{
		err_short_summary = 'Error: Please enter at least 10 characters (you entered ' + short_summary.length + ' characters).'		
	}
	
	if (short_summary.length > 55) {
		err_short_summary = 'Error: Please enter no more than 55 characters (you entered ' + short_summary.length +' characters).'
	}
	
	if (long_summary.length < 10)
	{
		err_long_summary = 'Error: Please enter at least 10 characters (you entered ' + long_summary.length + ' characters).'		
	}
	
	if (long_summary.length > 500) {
		err_long_summary = 'Error: Please enter no more than 500 characters (you entered ' + long_summary.length +' characters).'
	}
	
	if (err_stars == '')
	{
		$('err_stars').style.display = 'none';		
	}
	else {
		$('err_stars').style.display = 'block';	
		$('err_stars').innerHTML = err_stars;
	}
	
	if (err_short_summary == '')
	{
		$('err_short_summary').style.display = 'none';		
	}
	else {
		$('err_short_summary').style.display = 'block';	
		$('err_short_summary').innerHTML = err_short_summary;
	}
	
	if (err_long_summary == '')
	{
		$('err_long_summary').style.display = 'none';		
	}
	else {
		$('err_long_summary').style.display = 'block';	
		$('err_long_summary').innerHTML = err_long_summary;
	}

	if (err_short_summary != '' || err_long_summary != '' || err_stars != '' ) {
		return false;
	} 

	var vals = setValues('', 'pro_id', pro_id);
	vals = setValues(vals, 'stars', stars);
	vals = setValues(vals, 'short_summary', short_summary);
	vals = setValues(vals, 'long_summary', long_summary);
	vals = setValues(vals, 'captcha', captcha);
	Ajax.call('comment.php', vals, submitReviewResponse, "POST")
	return false;
}

function submitReviewResponse(result, textResult)
{
	if (result.message)
    {
      	alert(result.message);
    }

    if (result.error == 0)
    {
		var layer = $('submit_review');
		
		if (layer)
		{
			layer.innerHTML = result.content;
		}
    }	
}

function updateReview()
{
	var rat_id = $('rat_id').value;	
	var long_summary = trim($('long_summary').value);
	var captcha = $('captcha').value;
	
	var err_long_summary = '';
	
	if (long_summary.length < 10)
	{
		err_long_summary = 'Error: Please enter at least 10 characters (you entered ' + long_summary.length + ' characters).'		
	}
	
	if (long_summary.length > 500) {
		err_long_summary = 'Error: Please enter no more than 500 characters (you entered ' + long_summary.length +' characters).'
	}
	
	if (err_long_summary == '')
	{
		$('err_long_summary').style.display = 'none';		
	}
	else {
		$('err_long_summary').style.display = 'block';	
		$('err_long_summary').innerHTML = err_long_summary;
	}
	
	if (err_long_summary != '') {
		return false;
	} 
	
	var vals = setValues('', 'rat_id', rat_id);
	vals = setValues(vals, 'long_summary', long_summary);
	vals = setValues(vals, 'captcha', captcha);
	Ajax.call('comment.php?act=update_review', vals, updateReviewResponse, "POST")
	return false;
}

function updateReviewResponse(result, textResult)
{
	if (result.message)
    {
      	alert(result.message);
    }

    if (result.error == 0)
    {
		var layer = $('update_review');
		
		if (layer)
		{
			layer.innerHTML = result.content;
		}
    }	
}

function gotoPage(page, id, type)
{
  	Ajax.call('comment.php?act=gotopage', 'page=' + page + '&id=' + id + '&type=' + type, gotoPageResponse, 'GET');
}

function gotoPageResponse(result)
{
  	document.getElementById("user_review").innerHTML = result.content;
}


function rateOver(e)
{
	var w;
	e = e||event;
	w = (e.pageX - document.getElementById("myrate").offsetLeft)||e.offsetX;
	var p;
	p = 'none';
	if (w <= 0) {
		p = 'none';	
	}
	else if (w >0 && w <= 17) {
		p = '20%';	
	}
	else if (w > 17 && w <= 34) {
		p = '40%';	
	}
	else if (w >34 && w <= 51) {
		p = '60%';
	}	
	else if (w > 51 && w <= 68) {
		p = '80%';	
	}
	else if (w > 68 && w <= 85) {
		p = '100%';	
	}
	if ( $('star').style.width != p && p != 'none') { 
		$('star').style.width = p;	
	}
}

function rateOut()
{
	var v = $('stars').value;
	if (v < 0) {
		v = 0;	
	}
	if (v > 5) {
		v = 5;	
	}
	$('star').style.width = v * 20 + '%';		
}

function rateIt(e)
{
	var w;
	e = e||event;
	w = (e.pageX - document.getElementById("star").offsetLeft)||e.offsetX;
	var p;
	if (w <= 0) {
		p = 0;	
	}
	else if (w >0 && w <= 17) {
		p = 1;	
	}
	else if (w > 17 && w <= 34) {
		p = 2;	
	}
	else if (w > 34 && w <= 51) {
		p = 3;
	}	
	else if (w > 51 && w <= 68) {
		p = 4;	
	}
	else if (w > 68 && w <= 85) {
		p = 5;	
	}
	else {
		p = 0;	
	}	
	$('stars').value = p;
}

function watchIt(pro_id)
{
	Ajax.call('/user/?act=watch_soft', 'id=' + pro_id, watchItResponse, 'GET');		
}

function watchItResponse(result)
{
	if (result.message)
    {
      	alert(result.message);
    }

    if (result.error == 0)
    {
		if (result.content.type == 1) {
			location.href = '../user/?back_url=' + result.content.back_url;
		}
		else {
			$('watch_list').innerHTML = '<span class="inwatch">This product is on your <a href="/user/?act=watch_list">watch list</a></span>';		
		}
    }		
}

function swtichReviewForm(id, value)
{
	var v = value || '';
	if (v == '') 
	{
		if ($(id).style.display == 'block')
		{
			$(id).style.display = 'none';		
		}
		else {
			$(id).style.display = 'block';		
		}
	}
	else {
		if (v == true)
		{
			$(id).style.display = 'block';		
		}
		else {
			$(id).style.display = 'none';		
		}
	}
	return false;
}

function cancelWatch(id)
{
	if (!confirm('Are you sure you want to remove?'))
	{
		return false;
	}
	Ajax.call('/user/?act=cancel_watch', 'id=' + id, cancelWatchResponse, 'GET');		
}

function cancelWatchResponse(result)
{
	if (result.message)
    {
      	alert(result.message);
    }

    if (result.error == 0)
    {
		if (result.content.type == 1) {
			location.href = '/user/?back_url=' + result.content.back_url;
		}
		else {
			location.href = '/user/?act=watch_list';	
		}
    }		
}


