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 || 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;
    }
} 

checkUsername = function(baseUrl) {
	var username = document.getElementById('login').value;
	var url = baseUrl + '/ajax/checkuser/username/' + username;
	var divName = "username";
	
	ajaxMagic(url, divName);
}


function makeComment(divName, baseUrl) {
    var errorText = '';
    var formError = false;
    
	alert ('running post recipe comment JS');
    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 comment title\n";
     	formError = true;
    }
    */
    if(document.getElementById('txt_comment').value == "") {
     	errorText = errorText + "Please enter your comment\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 article_url = encodeURIComponent(document.getElementById('article_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 +
							'/article_id/' + document.getElementById('article_id').value +
							'/article_url/' + article_url +
							'/public_key/' + public_key +
							'/com_private_key/' + com_private_key +
							'/site_id/25');
		alert (url);					
		ajaxMagic(url, divName);
	}
}

//'&public_key=' + document.getElementById('public_key').value +
//'&private_key=' + document.getElementById('com_private_key').value +

makeCommentOpinion = function(recipe_ugc_id,vote,baseUrl) {
	var url = baseUrl + '/rating/recipecomment/recipe_ugc_id/' + recipe_ugc_id + '/userVote/' + vote;
	var divName;
	
	switch (vote) {
		case 0 : divName = "disagreeSpan_" + recipe_ugc_id;
				 document.getElementById("agree" + recipe_ugc_id).onclick ="";
				 break;
		case 1 : divName = "agreeSpan_" + recipe_ugc_id;
				 document.getElementById("disagree" + recipe_ugc_id).onclick ="";
				 break;
	}
	ajaxMagic(url, divName);
	/*
	$.ajax({
		url: url,
		type: 'POST',
		timeout: 1000,
		error: function(){
		    alert('Error loading content, please try again');
		},
		success: function(res){
			$('#'+divName).html(res);
	}
	/*
	new Ajax.Request ( url, {
		method: 'post',
		onSuccess: function(req) {
			$(divName).update(req.responseText);
		}
	});
	*/
}
makeBlogCommentOpinion = function(article_ugc_id,vote,baseUrl) {
	
	var url = baseUrl + '/rating/blogcomment/article_ugc_id/' + article_ugc_id + '/userVote/' + vote;
	var divName;
	
	switch (vote) {
		case 0 : divName = "disagreeSpan_" + article_ugc_id;
				 document.getElementById("agree" + article_ugc_id).onclick ="";
				 break;
		case 1 : divName = "agreeSpan_" + article_ugc_id;
				 document.getElementById("disagree" + article_ugc_id).onclick ="";
				 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(article_id, page, pagelimit, divName, commentCount) {
	var url = encodeURI('/tdf/ajax/getcommentdata' +
							'/article_id/' + article_id +
							'/page/' + page +
							'/pagelimit/' + pagelimit +
							'/commentCount/' + commentCount);

	ajaxMagic(url, divName);
}

function popupVideo(url) {
	window.open(url,'video','height=661,width=951,top=100,left=100,scrollbars=no,toolbars=no');
}

function popupGeneric(url, width, height) {
	window.open(url,'sbspopup','height=' + height + ',width=' + width + ',top=100,left=100,scrollbars=no,toolbars=no');
}

function nRToggle(element) {
	_rsEvent('http://www.sbs.com.au/food/'+element, '0');
}

function ajaxNielsen(description, identifier) {
	_rsPageEvent('page_'+description+'_http://www.sbs.com.au/food/'+identifier);
}

function ajaxNielsenRefine(description, identifier) {
	_rsPageEvent(description+identifier);
}


function changeFontSize(divname){
       var classname = document.getElementById(divname).className;
       if(classname=='normal'){
      	 document.getElementById(divname).style.fontSize = '14px';
      	 document.getElementById(divname).className = 'large';
       }
       else{
       		document.getElementById(divname).style.fontSize = '12px';
      		document.getElementById(divname).className = 'normal';
       }
}
	function postComment(divName,baseUrl){
		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_comment').value == "") {
	     	errorText = errorText + "Please enter your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('txt_comment_title').value == "") {
	     	errorText = errorText + "Please enter a title for your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('user_rating').value == "") {
	     	errorText = errorText + "Please rate this recipe before submitting your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('com_private_key').value == "") {
	     	errorText = errorText + "Please enter the validation number\n";
	     	formError = true;
	    }
	   
	    if(formError) {
		    window.alert(errorText);
		    return;
	    }
		else{ 
			//submitting
			var recipe_id = encodeURIComponent(document.getElementById('recipe_id').value);
			var comment_title = encodeURIComponent(document.getElementById('txt_comment_title').value);
			var user_rating = encodeURIComponent(document.getElementById('user_rating').value);
			var name = encodeURIComponent(document.getElementById('txt_name').value);
			var location = encodeURIComponent(document.getElementById('txt_location').value);
			var comment = encodeURIComponent(document.getElementById('txt_comment').value);
			var public_key = document.getElementById('public_key').value;
			var com_private_key = document.getElementById('com_private_key').value;
		
		  	var url = encodeURI(baseUrl +
		  						'/Recipe/postcomment' +
								'/name/' + name +
								'/location/' + location +
								'/comment_title/' + comment_title +
								'/user_rating/' + user_rating +
								'/comment/' + comment +
								'/recipe_id/' + recipe_id +
								'/public_key/' + public_key +
								'/com_private_key/' + com_private_key +
								'/site_id/25');

			ajaxMagic(url,divName);
			
		}
	}

	function postArticleComment(divName,baseUrl){

//		alert ('running post article JS');
		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_comment').value == "") {
	     	errorText = errorText + "Please enter your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('txt_comment_title').value == "") {
	     	errorText = errorText + "Please enter a title for your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('com_private_key').value == "") {
	     	errorText = errorText + "Please enter the validation number\n";
	     	formError = true;
	    }
	   
	    if(formError) {
		    window.alert(errorText);
		    return;
	    }
		else{ 
	
		var article_id = encodeURIComponent(document.getElementById('article_id').value);
		var comment_title = encodeURIComponent(document.getElementById('txt_comment_title').value);
		var name = encodeURIComponent(document.getElementById('txt_name').value);
		var location = encodeURIComponent(document.getElementById('txt_location').value);
		var comment = encodeURIComponent(document.getElementById('txt_comment').value);
		var public_key = document.getElementById('public_key').value;
		var com_private_key = document.getElementById('com_private_key').value;
		
		  	var url = encodeURI(baseUrl +
		  						'/Article/postcomment' +
								'/name/' + name +
								'/location/' + location +
								'/comment_title/' + comment_title +
								'/comment/' + comment +
								'/article_id/' + article_id +
								'/public_key/' + public_key +
								'/com_private_key/' + com_private_key +
								'/site_id/25');
//			alert (url);
			ajaxMagic(url,divName);
		}
	}

	function postBlogArticleComment(divName,baseUrl){

//		alert ('running post article JS');
		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_comment').value == "") {
	     	errorText = errorText + "Please enter your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('txt_comment_title').value == "") {
	     	errorText = errorText + "Please enter a title for your comment\n";
	     	formError = true;
	    }
	    if(document.getElementById('com_private_key').value == "") {
	     	errorText = errorText + "Please enter the validation number\n";
	     	formError = true;
	    }
	   
	    if(formError) {
		    window.alert(errorText);
		    return;
	    }
		else{ 
	
		var article_id = encodeURIComponent(document.getElementById('article_id').value);
		var comment_title = encodeURIComponent(document.getElementById('txt_comment_title').value);
		var name = encodeURIComponent(document.getElementById('txt_name').value);
		var location = encodeURIComponent(document.getElementById('txt_location').value);
		var comment = encodeURIComponent(document.getElementById('txt_comment').value);
		var public_key = document.getElementById('public_key').value;
		var com_private_key = document.getElementById('com_private_key').value;
		
		  	var url = encodeURI(baseUrl +
		  						'/BlogArticle/postcomment' +
								'/name/' + name +
								'/location/' + location +
								'/comment_title/' + comment_title +
								'/comment/' + comment +
								'/article_id/' + article_id +
								'/public_key/' + public_key +
								'/com_private_key/' + com_private_key +
								'/site_id/25');
//			alert (url);
			ajaxMagic(url,divName);
		}
	}





	
	function updateregion(baseUrl,state){
	
		var url = encodeURI(baseUrl+'/restaurantregion/state/'+state);
		ajaxMagic(url,'regions');
	}
	

	 function popup_window(url,w,h)
	 {
	  var width=w;
	  var height=h;
	  var from_top=350;
	  var from_left=200;
	  var toolbar='no';
	  var location='no';
	  var directories='no';
	  var status='no';
	  var menubar='no';
	  var scrollbars='no';

	  var resizable='no';
	  var atts='width='+width+'show,height='+height+',top='+from_top+',screenY=';
	  atts+= from_top+',left='+from_left+',screenX='+from_left+',toolbar='+toolbar;
	  atts+=',location='+location+',directories='+directories+',status='+status;
	  atts+=',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable;
	  window.open(url,'win_name',atts);
	 }
	
	 function popup_window2(url,w,h)
	 {
	  var width=w;
	  var height=h;
	  var from_top=250;
	  var from_left=200;
	  var toolbar='no';
	  var location='no';
	  var directories='no';
	  var status='no';
	  var menubar='no';
	  var scrollbars='yes';

	  var resizable='yes';
	  var atts='width='+width+'show,height='+height+',top='+from_top+',screenY=';
	  atts+= from_top+',left='+from_left+',screenX='+from_left+',toolbar='+toolbar;
	  atts+=',location='+location+',directories='+directories+',status='+status;
	  atts+=',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable;
	  window.open(url,'win_name',atts);
	 }
	