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 = '<div style="width:100%;align:center;"><img src="/web/images/ajax-loader.gif" align="center" /></div>';
	}
	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 makeEventComment(divName) {
    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 location\n";
     	formError = true;
    }
    if(document.getElementById('txt_comment').value == "") {
     	errorText = errorText + "Please enter your message\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;
    };
    

    
	xmlHttp=GetXmlHttpObject() 
	
	var name = encodeURIComponent(document.getElementById('txt_name').value);
	var message = 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 event_url = encodeURIComponent(document.getElementById('event_url').value);
	
	if(xmlHttp==null)  {
  		alert ("Browser does not support HTTP Request")
  		return false;
  	} else {
	  	var url = encodeURI('/speedweek/ajax/get-comments' +
							'/name/' + name +
							'/location/' + document.getElementById('txt_location').value +
							'/message/' + message +
							'/event_id/' + document.getElementById('event_id').value +
							'/public_key/' + public_key +
							'/com_private_key/' + com_private_key + 
							'/event_url/' + event_url);
		ajaxMagic(url, divName);
	}
}
function makeComment(divName, baseUrl, siteId, dbName, tableName, rating) {
    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";
    	errorText = errorText + "Please enter your location\n";
     	formError = true;
    }
    
	if(document.getElementById('txt_title').value == "") {
     	//errorText = errorText + "Please enter comment title\n";
     	errorText = errorText + "Please enter your subject\n";
     	formError = true;
    }
    if(document.getElementById('txt_content').value == "") {
     	//errorText = errorText + "Please enter your comment\n";
    	errorText = errorText + "Please enter your message\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 content = encodeURIComponent(document.getElementById('txt_content').value);
	var source = encodeURIComponent(document.getElementById('txt_location').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(rating == '1') {
		var userRating = encodeURIComponent(document.getElementById('user_rating').value);
	}

	if(xmlHttp==null)  {
  		alert ("Browser does not support HTTP Request")
  		return false;
  	} else {
	  	var url = encodeURI(baseUrl + '/comment/makecomment' +
	  						'/title/' + title +
							'/name/' + document.getElementById('txt_name').value +
							'/location/' + document.getElementById('txt_location').value +
							'/source/' + document.getElementById('txt_location').value +
							'/content/' + content +
							'/id/' + document.getElementById('id').value +
							//'/article_url/' + article_url +
							'/public_key/' + public_key +
							'/com_private_key/' + com_private_key +
							'/site_id/' + siteId +
							'/dbName/' + dbName +
							'/userRating/' + userRating +
							'/tableName/' + tableName
							);
		ajaxMagic(url, divName);
	}
}


makeCommentOpinion = function(baseUrl, commentId, vote, dbName, tableName) {
	var url = baseUrl + '/comment/agree/commentId/' + commentId + '/userVote/' + vote + '/dbName/' + dbName + '/tableName/' + tableName;
	var divName;
	
	divName = "agreedisagree" + commentId;
	
	ajaxMagic(url, divName);
}

makeEventCommentOpinion = function(commentId,vote) {
	var url = encodeURI('/speedweek//ajax/agree-comment/commentId/' + commentId + '/userVote/' + vote);
	var divName;
	
	switch (vote) {
		case 0 : divName = "disagreeSpan" + commentId;
				 document.getElementById("agree" + commentId).onclick ="";
		         break;
		case 1 : divName = "agreeSpan" + commentId;
		 		 document.getElementById("disagree" + commentId).onclick ="";
                 break;
	}
	
	ajaxMagic(url, divName);
}

function showComments(dbName, tableName, contentId, articlesPerPage, numPagesToDisplay, currentPage, baseUrl, ajaxDiv) {
	var url = encodeURI(baseUrl +'/comment/getcomments' +
						'/dbName/' + dbName +
						'/tableName/' + tableName +
						'/contentId/' + contentId +
						'/articlesPerPage/' + articlesPerPage +
						'/numPagesToDisplay/' + numPagesToDisplay +
						'/currentPage/' + currentPage +
						'/baseUrl' + baseUrl +
						'/ajaxDiv/' + ajaxDiv
						);
					
	ajaxMagic(url, ajaxDiv);
}

function showJoinTheDiscussion(dbName, tableName, contentId, baseUrl, ajaxDiv) {
	var url = encodeURI(baseUrl +'/comment/getjointhediscussion' +
						'/dbName/' + dbName +
						'/tableName/' + tableName +
						'/contentId/' + contentId +
						'/baseUrl' + baseUrl +
						'/ajaxDiv/' + ajaxDiv
						);
					
	ajaxMagic(url, ajaxDiv);
}




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('/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://home.sbs.com.au/'+element, '0');
}

/**
 * submits webjunk video data
 */
function submitWebjunkVideo() 
{
	var errorText = '';
	var formError = false;
	
	var userName = document.getElementById('txt_name').value;
	var userLocation = document.getElementById('txt_location').value;
	var title = document.getElementById('txt_title').value;
	var content = encodeURIComponent(document.getElementById('txt_content').value);
	
	var private_key = document.getElementById('com_private_key').value;
	var public_key = document.getElementById('public_key').value;

	// do validation
	if(userName == "") {
		 errorText = errorText + "Please enter your name\n";
		 formError = true;
	}
	if(userLocation == "") {
		 errorText = errorText + "Please enter your location\n";
		 formError = true;
	}
	if(title == "") {
		 errorText = errorText + "Please enter a title or subject\n";
		 formError = true;
	}
	if(content == "") {
		 errorText = errorText + "Please enter your video embed code\n";
		 formError = true;
	}
	if(private_key == "") {
		 errorText = errorText + "Please enter the validation number\n";
		 formError = true;
	}

	if(formError) {
		window.alert(errorText);
		return;
	};
	
	// set up the post variables
	var data = 
		'name=' + userName +
		'&location=' + userLocation +
		'&title=' + title +
		'&embedcode=' + content +
		'&public_key=' + public_key +
		'&private_key=' + private_key
	;

	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/webjunk/submit/',
		data: data,
		success: function(msg) {
			$('#webjunk_video_form').html(msg);
		}

	});
}// end submitWebjunkVideo

