//i define ajaxTimeOut, is time = milisecond , ajax calling time out
//should be large, since proxy.php request to API
//i use 10 seconds
//can move this var to template file, sothat can use value from site config
var ajaxTimeOut = 10000;

//define common ajaxLoading for display when waiting ajax result
var ajaxLoading = '<p style="text-align: center; text-decoration: none;"><img border="0" src="' + SITE_URL + '/images/ajax-loader.gif" width="31" height="31"><br />Loading..</p>';

//define a message for ajax calling error
var ajaxErrorMessage = 'Ajax timeout or process error. Please press F5 to reload';

function get_text(id)
{
	$.ajax({
				url: SITE_URL + '/proxy.php',
				type: "POST",
				data: getquerystring(id),
				timeout: ajaxTimeOut,
				beforeSend: function(){
					$('#tabcontent').html(ajaxLoading);
				},
				error: function(data){
					alert(ajaxErrorMessage);
				},
				success: function(data) {
					$('#tabcontent').html(data);
					//refresh the rating stars
					init_rating();
					$('div.rating/img').bind('click', changeRating);
				}
	});
}

function submit_review()
{
	var name=document.review_submit.name.value;
    var title=document.review_submit.title.value;
    var text=document.review_submit.text.value;
    var rating=document.review_submit.user_rating.value;
    var txtCaptcha=document.review_submit.txtCaptcha.value;
	 
	querystring = 'action=review_submit' + '&';
    
	querystring += 'name=' + encodeURIComponent(name) + '&';
	querystring += 'title=' + encodeURIComponent(title) + '&';
	querystring += 'text=' + encodeURIComponent(text) + '&';
	querystring += 'rating=' + encodeURIComponent(rating) + '&';
	querystring += 'txtCaptcha=' + encodeURIComponent(txtCaptcha) + '&';
	
	querystring += 'product_mpn=' + encodeURIComponent($('#product_mpn').val()) + '&';
    querystring += 'product_pn=' + encodeURIComponent($('#product_pn').val());
	
	//caling ajax
	$.ajax({
				url: SITE_URL + '/proxy.php',
				type: "POST",
				data: querystring,
				timeout: ajaxTimeOut,
				beforeSend: function(){
					$('#tabcontent').html(ajaxLoading);
				},
				error: function(data){
					alert(ajaxErrorMessage);
				},
				success: function(data) {
					$('#tabcontent').html(data);
					//refresh the rating stars
					init_rating();
					$('div.rating/img').bind('click', changeRating);
				}
	});
	
}


function getquerystring(id) {
    qstr = 'action=tabupdate' + '&';
   
    qstr += 'product_mpn=' + encodeURIComponent($('#product_mpn').val()) + '&';
    qstr += 'product_pn=' + encodeURIComponent($('#product_pn').val()) + '&';
    
    qstr += 'tab=' + encodeURIComponent(id);
    return qstr;
}
