var xmlHttp
var ajaxDiv

function ajaxMagic(url, divName) {
	xmlHttp=GetXmlHttpObject() 
	ajaxDiv = divName;
	if(xmlHttp==null) {
		alert("Browser does not support HTTP Request")
  		return
  	}
  	 
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
} 

function stateChanged() { 
	if(xmlHttp.readyState < 4) {
		document.getElementById(ajaxDiv).innerHTML = '';
	}
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById(ajaxDiv).innerHTML=xmlHttp.responseText;
	} 
} 

function GetXmlHttpObject() { 
	var objXMLHttp=null
	if(window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if(window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp 
}

function showCommentForm(site_id, ys_id, ys_ugc_id, ct_id) {
	var url = encodeURI('/private/includes/yoursay_response.php' +
						'?show=1' +
						'&ct_id=' + ct_id +
						'&ys_id=' + ys_id +
						'&ys_ugc_id=' + ys_ugc_id +
						'&site_id=' + site_id);
	ajaxMagic(url, 'yoursay_form');
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		 limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function isValidURL(url){
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    }
} 

function makeComment(divName, baseUrl, siteId) {
    var errorText = '';
    var formError = false;
    
    if(document.getElementById('txt_name').value == "") {
     	errorText = errorText + "Please enter your name\n";
     	formError = true;
    }
    
    if(document.getElementById('txt_location').value == "") {
     	errorText = errorText + "Please enter your suburb\n";
     	formError = true;
    }
	if(document.getElementById('txt_title').value == "") {
     	errorText = errorText + "Please enter review title\n";
     	formError = true;
    }
    if(document.getElementById('user_rating').value == "") {
	     errorText = errorText + "Please rate this product before submitting your comment\n";
	     formError = true;
	}
    if(document.getElementById('txt_comment').value == "") {
     	errorText = errorText + "Please enter your review\n";
     	formError = true;
    }
    if(document.getElementById('com_private_key').value == "") {
     	errorText = errorText + "Please enter the validation number\n";
     	formError = true;
    }
    if(isValidURL(document.getElementById('txt_title').value)){
    	errorText = errorText + "Please do not link URL in comment title field\n";
    	formError = true;
    }
    
    if(formError) {
	    window.alert(errorText);
	    return;
    };
    
	xmlHttp=GetXmlHttpObject() 
	
	var title = encodeURIComponent(document.getElementById('txt_title').value);
	var comment = encodeURIComponent(document.getElementById('txt_comment').value);
	var user_rating = encodeURIComponent(document.getElementById('user_rating').value);
	var product_url = encodeURIComponent(document.getElementById('product_url').value) + '-';
	var public_key = document.getElementById('public_key').value;
	var com_private_key = document.getElementById('com_private_key').value;
	
	
	if(xmlHttp==null)  {
  		alert ("Browser does not support HTTP Request")
  		return false;
  	} else {
	  	var url = encodeURI(baseUrl + '/ajax/getmakecommentdata' +
	  						'/title/' + title +
							'/name/' + document.getElementById('txt_name').value +
							'/location/' + document.getElementById('txt_location').value +
							'/message/' + comment +
							'/product_id/' + document.getElementById('product_id').value +
							'/user_rating/' + user_rating +
							'/product_url/' + product_url +
							'/public_key/' + public_key +
							'/com_private_key/' + com_private_key +
							'/site_id/'+ siteId);
		ajaxMagic(url, divName);
	}
	
	/*
	// set up the post variables
	var data =
		'title=' + title +
		'&name=' + document.getElementById('txt_name').value +
		'&location=' + document.getElementById('txt_location').value +
		'&message=' + comment +
		'&product_id=' + document.getElementById('product_id').value +
		'&user_rating=' + user_rating +
		'&product_url=' + product_url +
		'&public_key=' + public_key +
		'&com_private_key=' + com_private_key +
		'&site_id=' + siteId
	;

	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/ajax/getmakecommentdata/',
		data: data,
		success: function(msg) {
			$('#' + divName).html(msg);
		}

	});
	*/
	
}

//'&public_key=' + document.getElementById('public_key').value +
//'&private_key=' + document.getElementById('com_private_key').value +

makeCommentOpinion = function(commentId, vote, baseUrl) {
	var url = baseUrl + '/ajax/agree/commentId/' + commentId + '/userVote/' + vote;
	var divName;
	
	divName = "agreeDisagree" + commentId;
	
	/*switch (vote) {
		case 0 : divName = "disagreeSpan" + commentId;break;
		case 1 : divName = "agreeSpan" + commentId;break;
	}*/
	
	ajaxMagic(url, divName);
}

function commentOpinion(articles_ugc_id, rating, site_id, divName) {
	var url = encodeURI('/commentopinion.php' +
							'?rating= '+ rating +
							'&site_id=' + site_id + 
							'&articles_ugc_id=' + articles_ugc_id);

	ajaxMagic(url, divName);
}

function getComments(product_id, page, pagelimit, divName, commentCount, baseUrl) {
	var url = encodeURI(baseUrl + '/ajax/getcommentdata' +
							'/product_id/' + product_id +
							'/page/' + page +
							'/pagelimit/' + pagelimit +
							'/commentCount/' + commentCount);

	ajaxMagic(url, divName);
}

function setUpPList()
{
	$('.tooltip-target').each(function(i, obj){
		var id = obj.id;
		var name = id.split('-')[0];
		var rid = '#' +  name + '-content-' + id.split('-')[2];

		$(obj).mouseover(function() {
			$(rid).css('display', 'block');
		});

		$(obj).mouseout(function() {
			$(rid).css('display', 'none');
		});
	});
}

function getProducts(category, page, pagelimit, divName, productCount, baseUrl, orderField, sortBy, genre, sortGenre, display, digitalDownload, uri, keyword) {
	var url = encodeURI(baseUrl + '/ajax/getproductdata' +
							'/category/' + category +
							'/page/' + page +
							'/pagelimit/' + pagelimit +
							'/productCount/' + productCount +
							'/orderField/' + orderField +
							'/sortBy/' + sortBy +
							'/genre/' + genre +
							'/sortGenre/' + sortGenre +
							'/display/' + display +
							'/digitalDownload/' + digitalDownload +
							'/uri/' + uri +
							'/keyword/' + keyword
							);
	
	$('#' + divName).load(url, {}, function() {setUpPList();});
}

function popupVideo(url) {
	window.open(url,'video','height=661,width=951,top=100,left=100,scrollbars=no,toolbars=no');
}

function popupGeneric(url, width, height) {
	var top  = Math.ceil((screen.height - height)/2);
	var left = Math.ceil((screen.width - width)/2);
	window.open(url,'sbspopup','height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',scrollbars=no,toolbars=no');
}

function nRToggle(element) {
	_rsEvent('http://home.sbs.com.au/'+element, '0');
}

