// Keywords ajax functions
function addKeywordInReport(keyword,depth) {
	var rqaddKeyword = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=addKeyword&keyword='+keyword+'&depth='+depth,
		onComplete: updateKeywordsList }
	);
	function updateKeywordsList(requete) {
		var endColorTR;
		var dblRes = requete.responseText.split('~');
		// table
		var res = dblRes[0].split('|');
		$('listReportKeywords').innerHTML = res[1];
		document.getElementById('inputKeyword').value = 'Mot-clé';
		document.getElementById('depthKeyword').value = '100';
		if(document.getElementById('queryId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
		else { endColorTR = '#eeeeee'; }
		new Effect.Highlight('queryId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
		// amounts
		var resa = dblRes[1].split('|');
		$('monthlyPtsAmount').innerHTML = resa[0];
		$('yearlyPtsAmount').innerHTML = resa[1];
		$('monthlyEurAmount').innerHTML = (resa[0]/100);
		$('yearlyEurAmount').innerHTML = (resa[1]/100);
	}
}
function deleteKeywordFromReport(queryId) {
	var rqdeleteKeyword = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=deleteKeyword&queryId='+queryId,
		onComplete: updateKeywordsList }
	);
	function updateKeywordsList(requete) {
		var dblRes = requete.responseText.split('~');
		$('listReportKeywords').innerHTML = dblRes[0];
		// amounts
		var resa = dblRes[1].split('|');
		$('monthlyPtsAmount').innerHTML = resa[0];
		$('yearlyPtsAmount').innerHTML = resa[1];
		$('monthlyEurAmount').innerHTML = (resa[0]/100);
		$('yearlyEurAmount').innerHTML = (resa[1]/100);
	}
}
function modifyKeywordInReport(queryId) {
	var rqModifyKeyword = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=modifyKeyword&queryId='+queryId,
		onComplete: updateEditDiv }
	);
	function updateEditDiv(requete) {
		$('divEdit').innerHTML = requete.responseText;
		Effect.Appear('divEdit');
	}
}
function updateKeywordInReport(queryId,keyword,depth) {
	var rqUpdateKeyword = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=updateKeyword&queryId='+queryId+'&keyword='+keyword+'&depth='+depth,
		onComplete: updateKeywordsList }
	);
	function updateKeywordsList(requete) {
		$('divEdit').style.display = 'none';
		var endColorTR;
		// table
		var res = requete.responseText.split('|');
		$('listReportKeywords').innerHTML = res[1];
		if(document.getElementById('queryId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
		else { endColorTR = '#eeeeee'; }
		new Effect.Highlight('queryId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
	}
}



// urls ajax functions
function addURLInReport(url,isCompetitor) {
	var rqaddURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=addURL&url='+url+'&isCompetitor='+isCompetitor,
		onComplete: updateURLsList }
	);
	function updateURLsList(requete) {
		var endColorTR;
		var res = requete.responseText.split('|');
		$('listReportURLs').innerHTML = res[1];
		document.getElementById('inputURL').value = '';
		if(document.getElementById('urlId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
		else { endColorTR = '#eeeeee'; }
		new Effect.Highlight('urlId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
	}
}
function deleteURLFromReport(urlId) {
	var rqdeleteURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=deleteURL&urlId='+urlId,
		onComplete: updateURLsList }
	);
	function updateURLsList(requete) {
		$('listReportURLs').innerHTML = requete.responseText;
	}
}
function modifyURLInReport(urlId) {
	var rqModifyURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=modifyURL&urlId='+urlId,
		onComplete: updateEditDiv }
	);
	function updateEditDiv(requete) {
		$('divEdit').innerHTML = requete.responseText;
		Effect.Appear('divEdit');
	}
}
function updateURLInReport(urlId,url,isCompetitor) {
	var rqUpdateURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=updateURL&urlId='+urlId+'&url='+url+'&isCompetitor='+isCompetitor,
		onComplete: updateURLsList }
	);
	function updateURLsList(requete) {
		$('divEdit').style.display = 'none';
		var endColorTR;
		// table
		var res = requete.responseText.split('|');
		$('listReportURLs').innerHTML = res[1];
		if(document.getElementById('urlId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
		else { endColorTR = '#eeeeee'; }
		new Effect.Highlight('urlId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
	}
}

function updateReportOptions(reportId,countryReport,languageReport,depthReport,reportTitle,reportFooter,reportFont,reportFontSize) {
	var rqUpdateURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=updateReportOptions&reportId='+reportId+'&countryReport='+countryReport+'&languageReport='+languageReport+'&depthReport='+depthReport+'&reportTitle='+reportTitle+'&reportFooter='+reportFooter+'&reportFont='+reportFont+'&reportFontSize='+reportFontSize,
		onComplete: updateOptions }
	);
	function updateOptions(requete) {
		$('divMessage').innerHTML = requete.responseText;
		Effect.Appear('divMessage');
	}
}
function addRecipientInReport(fullName,email) {
	var rqaddRecipient = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=addRecipient&fullName='+fullName+'&email='+email,
		onComplete: updateRecipientsList }
	);
	function updateRecipientsList(requete) {
		var endColorTR;
		var res = requete.responseText.split('|');
		if(res[0] == -1) {
			$('divError').innerHTML = res[1];
			new Effect.Highlight('divError', { startcolor: '#ffff99',endcolor: '#ffffff' });
		}
		else {
			$('divError').innerHTML = '';
			$('listReportRecipients').innerHTML = res[1];
			if(document.getElementById('urlId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
			else { endColorTR = '#eeeeee'; }
			new Effect.Highlight('recipientId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
		}
	}
}
function deleteRecipientFromReport(recipientId) {
	var rqdeleteRecipient = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=deleteRecipient&recipientId='+recipientId,
		onComplete: updateRecipientsList }
	);
	function updateRecipientsList(requete) {
		$('listReportRecipients').innerHTML = requete.responseText;
	}
}
function modifyRecipientInReport(recipientId) {
	var rqModifyRecipient = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=modifyRecipient&recipientId='+recipientId,
		onComplete: updateEditDiv }
	);
	function updateEditDiv(requete) {
		$('divEdit').innerHTML = requete.responseText;
		Effect.Appear('divEdit');
	}
}
function updateReportRecipient(recipientId,fullName,email) {
	var rqaddRecipient = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=updateRecipient&recipientId='+recipientId+'&fullName='+fullName+'&email='+email,
		onComplete: updateRecipientsList }
	);
	function updateRecipientsList(requete) {
		var endColorTR;
		var res = requete.responseText.split('|');
		if(res[0] == -1) {
			$('divError').innerHTML = res[1];
			new Effect.Highlight('divError', { startcolor: '#ffff99',endcolor: '#ffffff' });
		}
		else {
			$('divEdit').style.display = 'none';
			$('divError').innerHTML = '';
			$('listReportRecipients').innerHTML = res[1];
			if(document.getElementById('recipientId_'+res[0]).className == 'pair') { endColorTR = '#ffffff'; }
			else { endColorTR = '#eeeeee'; }
			new Effect.Highlight('recipientId_'+res[0], { startcolor: '#ffff99',endcolor: endColorTR });
		}
	}
}

function postClientRemark(clientId,remark) {
	var rqUpdateURL = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=postClientRemark&clientId='+clientId+'&remark='+remark,
		onComplete: updateRemarkDiv }
	);
	function updateRemarkDiv(requete) {
		$('divClientRemark').innerHTML = requete.responseText;
	}
}

function postClientSupportQuery(clientId,subject,message) {
	var rqpostMessage = new Ajax.Request(
		'/ajaxActions.php', {
		method: 'get',
		parameters: 'a=postClientSupportQuery&clientId='+clientId+'&subject='+subject+'&message='+message,
		onComplete: updateFormDiv }
	);
	function updateFormDiv(requete) {
		var res = requete.responseText.split('|||');
		$('divContactForm').innerHTML = res[0];
		$('divContactTable').innerHTML = res[1];
	}
}

// others
function isNumeric(sText) {
	var ValidChars = "0123456789.,";
	var IsNumber=true;
	var Char;

	for (i=0; i<sText.length && IsNumber==true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}

	return IsNumber;
}
function PayPalAmountToPoints(amount) {
	if(!isNumeric(amount)) {
		$('rechargeWithPayPalAmount').value = amount.substring(0,(amount.length-1));
	}
	else if(amount < 0.27) {
		$('pointsComparedToPayPalAmount').value = '';
	}
	else {
		$('pointsComparedToPayPalAmount').value = Math.round((amount - (amount*3.4/100) - 0.25)*100);
	}
}
function PayPalAmountToEuros(amount) {
	if(!isNumeric(amount)) {
		$('pointsComparedToPayPalAmount').value = amount.substring(0,(amount.length-1));
	}
	else {
		$('rechargeWithPayPalAmount').value = Math.round((amount/100+0.25+amount/100*3.4/100)*100)/100;
	}
}
function hideDiv(id) {
	$(id).style.display = 'none';
	return false;
}
function showHideDiv(show) {
	var i;
	for(i=1; i<8 ; i++) {
		document.getElementById('i'+i).style.display = 'none';
		document.getElementById('l'+i).style.background = '#FFFFFF';
		document.getElementById('l'+i).style.color = '#000000';
	}
	$('i'+show).style.display = 'block';
	$('l'+show).style.background = '#009F95';
	$('l'+show).style.color = '#FFFFFF';
	return false;
}

