var xmlHttp
var ajaxDiv


function ajaxMagic(url, divName) {
	
	$('#'+divName).load(url);
	
} 

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 showSchedules(articlesPerPage, numPagesToDisplay, currentPage, baseUrl, ajaxDiv) {
	var url = encodeURI(baseUrl +'/schedule/getschedules' +
						'/articlesPerPage/' + articlesPerPage +
						'/numPagesToDisplay/' + numPagesToDisplay +
						'/currentPage/' + currentPage +
						'/ajaxDiv/' + ajaxDiv
						);
					
	ajaxMagic(url, ajaxDiv);
}


function makeComment(divName, baseUrl, siteId, dbName, tableName, active) {
    var errorText = '';
    var formError = false;
    
    if($('#txt_name').val() == "") {
     	errorText = errorText + "Please enter your name\n";
     	formError = true;
    }
    
    if($('#txt_location').val() == "") {
     	errorText = errorText + "Please enter your suburb\n";
     	formError = true;
    }
    
	if($('#txt_title').val() == "") {
     	errorText = errorText + "Please enter comment title\n";
     	formError = true;
    }
    if($('#txt_content').val() == "") {
     	errorText = errorText + "Please enter your comment\n";
     	formError = true;
    }
    if($('#com_private_key').val() == "") {
     	errorText = errorText + "Please enter the validation number\n";
     	formError = true;
    }
    if(isValidURL($('#txt_title').val())){
    	errorText = errorText + "Please do not link URL in comment title field\n";
    	formError = true;
    }
    
    if(formError) {
	    window.alert(errorText);
	    return;
    };
    
	var title = encodeURIComponent($('#txt_title').val());
	var content = encodeURIComponent($('#txt_content').val());
	var source = encodeURIComponent($('#txt_source').val());
	
	//var article_url = encodeURIComponent(document.getElementById('article_url').value) + '-';
	var public_key = $('#public_key').val();
	var com_private_key = $('#com_private_key').val();
	//var rating = encodeURIComponent(document.getElementById('user_rating').value);
	
  	var url = encodeURI(baseUrl + '/comment/makecomment/');
  	var dataString = 'title='+title+
		'&name=' + $('#txt_name').val() +
		'&location=' + $('#txt_location').val() +
		'&source=' + source +
		'&content=' + content +
		'&id=' + $('#id').val() +
		'&public_key=' + public_key +
		'&com_private_key=' + com_private_key +
		'&site_id=' + siteId +
		'&dbName=' + dbName +
		'&tableName=' + tableName +
		'&active=' + active;
	
  	$.ajax({
  		type: "POST",
  		url: url,
  		data: dataString,
  		success : function(msg) {
  			$('#'+divName).html(msg);	
  		}
  	});
}


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);
}

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 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 nRToggle(element) {
	_rsEvent('http://www.sbs.com.au/yourlanguage/'+element, '0');
}
	
function ajaxNielsen(description, identifier) {
	_rsPageEvent('page_'+description+'_http://www.sbs.com.au/yourlanguage/'+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 postEvent(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_submit_by_email').value == "") {
     	errorText = errorText + "Please enter your email address\n";
     	formError = true;
    }
    if(document.getElementById('txt_event').value == "") {
     	errorText = errorText + "Please enter the name of your event\n";
     	formError = true;
    }
    if(document.getElementById('txt_description').value == "") {
     	errorText = errorText + "Please a description of your event\n";
     	formError = true;
    }
    if(document.getElementById('txt_location_address').value == "") {
     	errorText = errorText + "Please address of name of your event\n";
     	formError = true;
    }
    if(document.getElementById('txt_location_city').value == "") {
     	errorText = errorText + "Please city of your event\n";
     	formError = true;
    }
    if(document.getElementById('slct_location_state').value == "") {
     	errorText = errorText + "Please the state event\n";
     	formError = true;
    }
    if( (document.getElementById('slct_startdate_day').value == "") || 
    		(document.getElementById('slct_startdate_month').value == "") || 
    		(document.getElementById('slct_startdate_year').value == "") || 
    		(document.getElementById('slct_startdate_hours').value == "") || 
    		(document.getElementById('slct_startdate_ampm').value == "") ) {
     	errorText = errorText + "Please select the start time and date\n";
     	formError = true;
    }
    if( (document.getElementById('slct_enddate_day').value == "") || 
    		(document.getElementById('slct_enddate_month').value == "") || 
    		(document.getElementById('slct_enddate_year').value == "") || 
    		(document.getElementById('slct_enddate_hours').value == "") || 
    		(document.getElementById('slct_enddate_ampm').value == "") ){
     	errorText = errorText + "Please select the end time and date\n";
     	formError = true;
    }
    if(document.getElementById('ch_t_and_c').checked == false) {
     	errorText = errorText + "Please read and agree to the terms and conditions\n";
     	formError = true;
    }
    
    if(formError) {
	    window.alert(errorText);
	    return false;
    }else{ 
	    var language_id = encodeURIComponent(document.getElementById('language_id').value);
		var submit_by_name = encodeURIComponent(document.getElementById('txt_name').value);
        var submit_by_email = encodeURIComponent(document.getElementById('txt_submit_by_email').value);
        var submit_by_phone = encodeURIComponent(document.getElementById('txt_submit_by_phone').value); 
        var event = encodeURIComponent(document.getElementById('txt_event').value); 
        var description = encodeURIComponent(document.getElementById('txt_description').value); 
        var contact_name = encodeURIComponent(document.getElementById('txt_contact_name').value); 
        var contact_phone = encodeURIComponent(document.getElementById('txt_contact_phone').value); 
        var contact_email = encodeURIComponent(document.getElementById('txt_contact_email').value); 
        var contact_website = encodeURIComponent(document.getElementById('txt_contact_website').value); 
        var location_name = encodeURIComponent(document.getElementById('txt_location_name').value); 
        var location_address = encodeURIComponent(document.getElementById('txt_location_address').value); 
        var location_city = encodeURIComponent(document.getElementById('txt_location_city').value); 
        var location_state = encodeURIComponent(document.getElementById('slct_location_state').value); 
        var t_and_c = encodeURIComponent(document.getElementById('ch_t_and_c').value); 
        var slct_startdate_day = encodeURIComponent(document.getElementById('slct_startdate_day').value);
        var slct_startdate_month = encodeURIComponent(document.getElementById('slct_startdate_month').value);
        var slct_startdate_year = encodeURIComponent(document.getElementById('slct_startdate_year').value);
        var slct_startdate_hours = encodeURIComponent(document.getElementById('slct_startdate_hours').value);
        var slct_startdate_minutes = encodeURIComponent(document.getElementById('slct_startdate_minutes').value);
        var slct_startdate_ampm = encodeURIComponent(document.getElementById('slct_startdate_ampm').value);
        var slct_enddate_day = encodeURIComponent(document.getElementById('slct_enddate_day').value);
        var slct_enddate_month = encodeURIComponent(document.getElementById('slct_enddate_month').value);
        var slct_enddate_year = encodeURIComponent(document.getElementById('slct_enddate_year').value);
        var slct_enddate_hours = encodeURIComponent(document.getElementById('slct_enddate_hours').value);
        var slct_enddate_minutes = encodeURIComponent(document.getElementById('slct_enddate_minutes').value);
        var slct_enddate_ampm = encodeURIComponent(document.getElementById('slct_enddate_ampm').value);
        
	  	var url = encodeURI(baseUrl +
		        '/event/' + event +  
		        '/description/' + description +  
	        	'/language_id/' + language_id + 
		        '/location_name/' + location_name +  
		        '/location_address/' + location_address +  
		        '/location_city/' + location_city +  
		        '/location_state/' + location_state +  
	        	'/submit_by_name/' + submit_by_name + 
		        '/submit_by_email/' + submit_by_email + 
		        '/submit_by_phone/' + submit_by_phone +  
		        '/contact_name/' + contact_name +  
		        '/contact_phone/' + contact_phone +  
		        '/contact_email/' + contact_email +  
		        '/contact_website/' + contact_website +
		        '/slct_startdate_day/' + slct_startdate_day +  
		        '/slct_startdate_month/' + slct_startdate_month +  
		        '/slct_startdate_year/' + slct_startdate_year +  
		        '/slct_startdate_hours/' + slct_startdate_hours +  
		        '/slct_startdate_minutes/' + slct_startdate_minutes +  
		        '/slct_startdate_ampm/' + slct_startdate_ampm +  
		        '/slct_enddate_day/' + slct_enddate_day +  
		        '/slct_enddate_month/' + slct_enddate_month +  
		        '/slct_enddate_year/' + slct_enddate_year +  
		        '/slct_enddate_hours/' + slct_enddate_hours +  
		        '/slct_enddate_minutes/' + slct_enddate_minutes +  
		        '/slct_enddate_ampm/' + slct_enddate_ampm 
		        );
	  	//alert(url);
		ajaxMagic(url,divName);
    }
}	

function setLocation(divName,baseUrl,appendUrl){
	var errorText = '';
    var formError = false;
    
    if(document.getElementById('location').value == "") {
     	errorText = errorText + "Please select a location\n";
     	formError = true;
    }
    
    if(formError) {
	    window.alert(errorText);
	    return;
    }else{ 
	    var location = encodeURIComponent(document.getElementById('location').value);

	  	var url = encodeURI(baseUrl +
		        '/location/' + location
		        + appendUrl);
		ajaxMagic(url,divName);
    }
}	

function turnOnOff(s){
	element = document.getElementById(s);
	if(element.style.display != 'block' && element.style.display != 'none'){
		element.style.display = "none";
	}
	else if(element.style.display == 'block'){
	  element.style.display = "none";
	}
	else{
		element.style.display = "block";
	}
}


function reloadAndJump(anchor)
{
	/// Essential Crutchware for I.E. ///
	if(location.hash!="")             ///
	location.href=location.hash;     ///
	///////////////////////////////////// 

	location.href=location.href.split(/\?|#/)[0] + '#' + anchor;   
	location.reload(true);

	return false;
}


listCities = function (divName, baseURL, countryCode ) {
	var url = encodeURI(baseURL + '/languages/ajax/list-cities' +
							'/country_code/' + countryCode +
							'/city_list/' + cityArr);

	$('#'+divName).load(url);
}

var cityArr = [];
selectCity = function (divName, baseURL,city, displayFlag) {
	var now = new Date();
  	now.setMonth(now.getMonth() + 1 );

	if (cityArr.length>=5){
		alert("You can only select a maximum of 5 cities");
		return;
	}

	for (j =0; j < cityArr.length; j++) {
    	if (cityArr[j] == city) {
        	alert("You cannot select the same city twice");
			return;
        }
    }

	cityArr.push(city);

	document.cookie = "YoulanguageWeatherForecast=" + cityArr + ";" + "expires=" + now.toGMTString() + ";"
	
	var url = encodeURI(baseURL + '/languages/ajax/select-city' +
							'/city_code/' + city +
							'/city_list/' + cityArr +
							'/display/' + displayFlag );
	
	//alert(cityArr);
	$('#'+divName).load(url);
}

	
submitPoll = function(pollId, answerId) {
	$("#opinion_poll").load("/yourlanguage/languages/ajax/poll-ajax/pollid/"+pollId+"/ansid/" + answerId );
}
	
function postContactUs(divName, langId ) {
    var errorText = '';
    var formError = false;
    
    if($('#txt_name').val() == "") {
     	errorText = errorText + "Please enter your name\n";
     	formError = true;
    }
    
    if($('#txt_location').val() == "") {
     	errorText = errorText + "Please enter your location\n";
     	formError = true;
    }
    
    if($('#txt_submit_by_phone').val() == "") {
     	errorText = errorText + "Please enter your contact number\n";
     	formError = true;
    }
    
    if($('#txt_submit_by_email').val() == "") {
     	errorText = errorText + "Please enter your email address\n";
     	formError = true;
    }
    
    if($('#txt_description').val() == "") {
     	errorText = errorText + "Please enter your message\n";
     	formError = true;
    }
    
    if (!($('#ch_t_and_c').attr('checked'))) {
    	errorText = errorText + "Please tick the Terms and Conditions.\n";
     	formError = true;
    }
    
    
    if(formError) {
	    window.alert(errorText);
	    return;
    };
    
	var name = encodeURIComponent($('#txt_name').val());
	var address = encodeURIComponent($('#txt_location').val());
	var email = encodeURIComponent($('#txt_submit_by_email').val());
	var message = encodeURIComponent($('#txt_description').val());
	
  	var url = encodeURI(baseUrl + '/ajax/contact-us/');
  	var dataString = 'name='+name+
  		'&contactfor='+ $('#txt_contact_name').val() +
  		'&languageId=' + langId +
	  	'&address=' + address +
		'&suburb=' + $('#txt_location_city').val() +
		'&state=' + $('#slct_location_state').val() +
		'&postcode=' + $('#txt_location_postcode').val() +
		'&email=' + email +
		'&contact_number=' + $('#txt_submit_by_phone').val() +
		'&age=' + $('#txt_submit_age').val() +
		'&gender=' + $('#txt_submit_gender').val() +
		'&message=' + message;
	
  	$.ajax({
  		type: "POST",
  		url: url,
  		data: dataString,
  		success : function(msg) {
  			$('#'+divName).html(msg);	
  		}
  	});
}	

function getEpisodes(divName, page, program, podcast, inLanguage)
{ 
	// set up the post variables
	var data =
		'divName=' + divName +
		'&page=' + page +
		'&program=' + program +
		'&podcast=' + podcast +
		'&inLanguage=' + inLanguage
	;

	// send the url via ajax to a php script
	// and refresh content after success
	jQuery.ajax({
		type: 'POST',
		url: baseUrl + '/' + 'ajax' + '/episode-list/',
		data: data,
		success: function(msg) {
			$('#' + divName).html(msg);
		}
	});
}
	
	
