﻿var TESTIMONIALS = new Array();
TESTIMONIALS[0] = new Array('23','Fátima Martins','fatimasusana13@hotmail.com','','','Recebi e gostei imenso! Obrigada pela vossa celeridade e profissionalismo. Bons negócios!','{}');


function prepareTemplate()
{	
	// get testimonial content
	var content_a = PROXT_TEMPLATE.match(/\[TESTIMONIAL_CONTENT\](.*)\[\/TESTIMONIAL_CONTENT\]/);
	if ( ! content_a[1] ){
		alert('Invalid testimonial template provided');
		return false;
	}
	template = PROXT_TEMPLATE.replace(/\[TESTIMONIAL_CONTENT\](.*)\[\/TESTIMONIAL_CONTENT\]/,'[_TEMPLATE_CONTENT_HERE_]');
	var content = content_a[1];
	
	// get content from the url
	var url_a = PROXT_TEMPLATE.match(/\[IF_URL\](.*)\[\/IF_URL\]/);
	var url = url_a[1];
	content = content.replace(/\[IF_URL\].*\[\/IF_URL\]/,"[_URL_HERE_]");
	
	// get photo from the content
	var photo_a = PROXT_TEMPLATE.match(/\[IF_PHOTO\](.*)\[\/IF_PHOTO\]/);
	if ( photo_a ){
		var photo = photo_a[1];
	}else { var photo = ''; }
	content = content.replace(/\[IF_PHOTO\](.*)\[\/IF_PHOTO\]/,"[_PHOTO_HERE_]");
	
	return new Array( template, content, url, photo );
}

function customFieldIds()
{
	custom_fields = new Array();
		return custom_fields;
}

function replaceCustomFields(template,extrainfo)
{
	if ( extrainfo == '' ){ return template; }
	var custom_fields = customFieldIds();
	for(var i=0;i<custom_fields.length;i++){
		var patt = '[CUSTOM_'+custom_fields[i]+']';
		if ( extrainfo[ custom_fields[i] ] ){ var rep = extrainfo[ custom_fields[i] ]; }
		else { var rep = ''; }
		var template = template.replace( patt, rep);
	}
	return template;
}
function displayTestimonials(patterns)
{
	var max_chars = 0;
	var template = '';var gtemplate = '';
	for(i=0;i<TESTIMONIALS.length;i++)
	{
		var id = TESTIMONIALS[i][0];
		var fullname = stripslashes(TESTIMONIALS[i][1]);
		fullname = unescape(fullname.replace(/\+/g,'%20'));
		var email = TESTIMONIALS[i][2];
		var url = TESTIMONIALS[i][3];
		var photo = TESTIMONIALS[i][4];
		var testimonial = stripslashes(TESTIMONIALS[i][5]);
		testimonial = unescape( testimonial.replace(/\+/g,'%20') );
		if ( max_chars ){
			testimonial = testimonial.substr(0,max_chars);
			testimonial = testimonial + '...';
		}
		var extra = TESTIMONIALS[i][6];
		template = patterns[1];
		// URL
		if ( url && patterns[1] ){
			var url_line = patterns[2].replace(/\[URL\]/,url);
			template = template.replace(/\[_URL_HERE_\]/,url_line);
		}
		else { template = template.replace(/\[_URL_HERE_\]/,""); }
		// PHOTO
		if ( photo && patterns[3] ){
			var link = '<IMG src="/testimonials//plugins/proxtestimonial/photos/'+
				             photo+'" width="120" height="121" class="img">';
			var photo_line = patterns[3].replace(/\[PHOTO_IMG_LINK\]/,link);
			template = template.replace(/\[_PHOTO_HERE_\]/,photo_line);
		}
		else { template = template.replace(/\[_PHOTO_HERE_\]/,''); }
		//CONTENT REPLACE
		template = template.replace(/\[FULL_NAME\]/,fullname);
		template = template.replace(/\[EMAIL\]/,email);
		template = template.replace(/\[TESTIMONIAL\]/,testimonial);
		template = template.replace(/\[CUR_NUM\]/g,i);
		template = replaceCustomFields( template,extra );
		gtemplate = gtemplate + template + '<P style="margin:7px;">&nbsp;</P>';	
	}
	document.write( patterns[0].replace(/\[_TEMPLATE_CONTENT_HERE_\]/,gtemplate) );
}
function stripslashes(str)
{
	str = str.replace(/\\'/,"'");
	str = str.replace(/\\"/,'"');
	return str;
}

var patterns = prepareTemplate();
displayTestimonials(patterns);