// JavaScript Document
/*
Class Testimonials_new
2008 august 15

*/
Testimonials_new = function()
{
	this.site_url = null;
	this.tstm_id = null;
	this.error = null;
	this.processorUrl = null;
	this.imagesUrl = null;
	this.iconsUrl = null;
	this.photos = null;
	
	//erase left blank spaces in a string
	this.ltrim = function(s) 
	{ 
		return s.replace(/^\s+/, ""); 
	};
	
	//erase right blank spaces in a string
	this.rtrim = function(s) 
	{ 
		return s.replace(/\s+$/, ""); 
	};
	
	//erase left and right blank spaces in a string
	this.trim = function(s) 
	{ 
		return this.rtrim(this.ltrim(s)); 
	};	
	
	//validate email if it's correct return 0 else return 1
	this.validateEmail = function(email)
	{
		var regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
		// Presume that the email is invalid
		var valid = 0;
		// Validate the syntax
		
		if (email.search(regexp) == -1)
		{
			valid = 1;
		} 
		
		return valid;
	}

	this.printPhotos = function()
	{
		//alert(this.photos);
		var images = "";
		var cont = 0;
		var position = 0;
		var uploaded = "";
		
		for(var i = 0; i < this.photos.length; i++)
		{
			
			if(cont == 4)
			{
				/*images += "</div><div class='rowImages'>";*/
				images += "<br class = 'clearBoth' />";
			}
			

			if(this.photos[i]!="")
			{
				position = i+1;
				//alert(this.photos[i]);
				images += "<div class = 'contentColumnImage'><div class='columnImage'><div class='vcAreaMoreImages'><div class='vc'><center><img class = 'vcElement' src='"+this.imagesUrl+this.photos[i]['url']+"' "+this.photos[i]['size']+" /></center></div></div></div>";	
				//images += "<br /><div class = 'deleteTestimonialImage' ><a href='#' onClick='javascript:testimony_new.deleteImage("+position+")'>Delete</a></div></div>";
				images += "<a href='#' onClick='javascript:testimony_new.deleteImage("+position+")' class='removePicture'>Delete</a></div>";		
				cont++;
			}
		
		}
		/*images += "</div>";*/
		uploaded = "<label class='cpTitle4'>uploaded: "+this.photos.length+" of 7 pictures.</label>";
		
		YAHOO.util.Dom.get("uploaded").innerHTML= (uploaded);
		
		YAHOO.util.Dom.get("step2_images_div").innerHTML= (images);
	}
	
	this.getTstmPhotos_success = function(o)
	{
		//alert(o.responseText);
		if(o.responseText == 0)
		{
			this.step2_frm_div_module.hide();
		}
		else
		{
			this.step2_frm_div_module.show();			
		}
	}
	
	this.getTstmPhotos_failure = function()
	{
		alert("Error this.getTstmPhotos function");	
	}

	this.getTstmPhotos = function()
	{
		var callBack=
		{
			success: this.getTstmPhotos_success,
			failure: this.getTstmPhotos_failure,
			scope: this
		};		
		var postData = "action=getTstmPhotos&tstm_id="+this.tstm_id;
		/*YAHOO.util.Dom.get(this.step2_frm_div).innerHTML= ("<p>Loading ...<p>");*/
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);		
	}
	
	
	this.deleteImage_success = function(o)
	{
		//alert(o.responseText);
		
		if(o.responseText != "null")
		{
			this.photos  = YAHOO.lang.JSON.parse(o.responseText);		
			if(this.photos[0] == 'error')
			{
				alert("I can't delete your photo =(");	
			}
			else
			{
				
					this.printPhotos();
			}
		}
		else
		{
			//Blanquear el div de imagenes	
			YAHOO.util.Dom.get("uploaded").innerHTML = "";
			YAHOO.util.Dom.get("step2_images_div").innerHTML = "";
		}
		this.step2_frm.reset();
		this.getTstmPhotos(); //oculta o muestra el file form
		
	}
	this.deleteImage_failure = function()
	{
		alert("Error in: deleteImage _failure");
	}
	this.deleteImage = function(position)
	{
		if(confirm('Do you want to delete this picture?'))
		{
			//alert("Borrame : tstm_photo"+position+" del testimonio: "+this.tstm_id);	
			var callBack=
			{
				success: this.deleteImage_success,
				failure: this.deleteImage_failure,
				scope: this
			};		
			var postData = "action=deleteImage&tstm_id="+this.tstm_id+"&position="+position;
			/*YAHOO.util.Dom.get(this.storedAddressesDiv).innerHTML= ("<p>Loading ...<p>");*/
			YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
		}
	}
	
	/******************************************************
	START STEP3
	*******************************************************/
	this.getTestimonyText_success = function(o)
	{
		var text = YAHOO.lang.JSON.parse(o.responseText);
		var value = "";
		var title = "";
			
			//put the title testimony above the image
			title += "<div><h3 class='cpTitle2'>"+text[0]+"</h3></div>";		
			
			value += "<div><label class='cpTitle4'>Name:</label><br class='clearBoth' />"+text[1]+"</div><br class='clearBoth' />";
			value += "<div><label class='cpTitle4'>Location:</label><br class='clearBoth' />"+text[2]+"</div><br class='clearBoth' />";
			value += "<div><label class='cpTitle4'>Date:</label><br class='clearBoth' />"+text[3]+"</div><br class='clearBoth' />";
			value += "<div><label class='cpTitle4'>Flowers:</label><br class='clearBoth' />";
			//alert(text[4]+text[5]+text[6]+text[7]);
			if(text[4] != "null" && text[4] != null)
				value += text[4];
			if(text[5] != "null" && text[5] != null)
				value += ", "+text[5];
			if(text[6] != "null" && text[6] != null)
				value += ", "+text[6];
			if(text[7] != "null" && text[7] != null)
				value += ", "+text[7];

			value += "</div><br class='clearBoth' />";				
			
			value += "<div><label class='cpTitle4'>Testimony:</label><br class='clearBoth' />"+text[8]+"</div><br class='clearBoth' />";
		
		YAHOO.util.Dom.get("getTestimonyTitle").innerHTML= (title);
		
		YAHOO.util.Dom.get("getTestimonyText").innerHTML= (value);
		
		
	}
	
	this.getTestimonyText_failure = function()
	{
		
		alert("Error in getTestimonyText");
	}
	
	this.getTestimonyText = function()
	{
		var callBack=
		{
			success: this.getTestimonyText_success,
			failure: this.getTestimonyText_failure,
			scope: this
		};		
		var postData = "action=getTestimonyText&tstm_id="+this.tstm_id;
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
		
	}
	
	
	
	
	this.getTestimonyImages_success = function(o)
	{
		var text = YAHOO.lang.JSON.parse(o.responseText);
		
		
		var image = "";
		var images = "";
		
		YAHOO.util.Dom.get("getTestimonyImage").innerHTML= (image);
		YAHOO.util.Dom.get("getTestimonyImages").innerHTML= (images);
		
		if(o.responseText == 'null')
		{
			image += "<div class='vcAreaMainImage'><div class='vc'><center><a rel='lightbox' class='thumbnailMainImageLink lightbox vcElement' href='"+this.imagesUrl+"no_image_thumb.jpg'><img src='"+this.imagesUrl+"no_image_thumb.jpg' ></a></center></div></div><br class='clearBoth' />";	
		}
		else
		{
			//alert(text[0][url] + " - " +text[0][size]);
			image += "<div class='vcAreaMainImage'><div class='vc'><center><a rel='lightbox' class='thumbnailMainImageLink lightbox vcElement' href='"+this.imagesUrl+text[0]['url']+"'><img src='"+this.imagesUrl+text[0]['url']+"' "+text[0]['size']+" ></a></center></div></div><br class='clearBoth' />";	
			for(var i=1; i<text.length; i++)
			{
				//alert(encodeURI(text[i]['url']));
				if(i==3)
					images += '<div class="vcAreaMoreImages"><div class="vc"><center><a rel="lightbox" class="thumbnailLink lightbox vcElement" href="'+this.imagesUrl+text[i]["url"]+'"><img src="'+this.imagesUrl+text[i]["url"]+'" '+text[i]["size"]+' ></a></center></div></div><br class="clearBoth" />';
				else
					images += '<div class="vcAreaMoreImages"><div class="vc"><center><a rel="lightbox" class="thumbnailLink lightbox vcElement" href="'+this.imagesUrl+text[i]["url"]+'"><img src="'+this.imagesUrl+text[i]["url"]+'" '+text[i]["size"]+' ></a></center></div></div>';
			}
	
		}
		YAHOO.util.Dom.get("getTestimonyImage").innerHTML= (image);
		if(images != "")
			YAHOO.util.Dom.get("getTestimonyImages").innerHTML= (images);
		/*
		Note: 2008 august 18
		esta línea es necesaria para que funcione el efecto de lightbox en todas las imágenes incluidas las extra images
		*/
		$(".lightbox").lightbox();
			
	}
	
	this.getTestimonyImages_failure = function()
	{
		
		alert("Error in getTestimonyImages");
	}
	
	
	this.getTestimonyImages = function()
	{
		var callBack=
		{
			success: this.getTestimonyImages_success,
			failure: this.getTestimonyImages_failure,
			scope: this
		};		
		var postData = "action=getTestimonyImages&tstm_id="+this.tstm_id;
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
	}

	this.goBackStep2_btn_click = function()
	{
		this.step3_div.hide();
		this.step2_div.show();
	}
	
	this.updateToS2_success = function()
	{
		document.location.href=this.site_url+"/testimonials/Testimonials.php"
	}
	
	this.updateToS2_failure = function()
	{
		alert("Error updateToS2");		
	}
	this.submitTestimonial_btn_click = function()
	{
		
		var callBack=
		{
			success: this.updateToS2_success,
			failure: this.updateToS2_failure,
			scope: this
		};		
		var postData = "action=updateToS2&tstm_id="+this.tstm_id;
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);
		
	}

	/******************************************************
	END STEP3
	*******************************************************/

	
	/******************************************************
	START STEP2
	*******************************************************/
	this.uploadImages = function()
	{
			YAHOO.util.Connect.setForm(this.step2_frm, true);
			
			var uploadHandler = {
				upload: function(o) 
				{
					//alert(o.responseText);

					this.getTstmPhotos();//ver u ocultar step2_frm_div
					
					
					if(o.responseText != "null")
					{
					this.photos  = YAHOO.lang.JSON.parse(o.responseText);
					if(this.photos[0]=='error')
							alert(this.photos[1]);
						else
						{
							
								this.printPhotos();
						}
					}
					else
					{
						//Blanquear el div de imagenes	
						YAHOO.util.Dom.get("uploaded").innerHTML = "";
						YAHOO.util.Dom.get("step2_images_div").innerHTML= "";	
					}
					this.step2_frm.reset();
				},
				scope: this
			};
			var postData = "action=uploadImages&tstm_id="+this.tstm_id;
			YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, uploadHandler, postData);	
	}


	this.goBackStep1_btn_click = function(e, obj)
	{
		//document.location.href = this.site_url+"/testimonials/Testimonials_new.php?tstm_id="+this.tstm_id;
		this.step2_div.hide();
		this.step1_div.show();
		
	}

	this.uploadImages_btn_click = function(e, obj)
	{
		if(this.trim(this.step2_frm.tstm_photo.value)=="")
		{
			alert("Add at least one image");
		}
		else
		{
			this.uploadImages();	
		}
	}
	
	this.goToStep3_btn_click = function(e, obj)
	{
		this.step2_div.hide();
		this.getTestimonyText();
		this.getTestimonyImages();
		this.step3_div.show();

	}
	/******************************************************
	END STEP2
	*******************************************************/

	
	
	
	/******************************************
	START SECTION TO UPDATE TESTIMONY
	******************************************/	
	this.updateStep1Testimony_success = function(o)
	{
		//alert(o.responseText);
		//document.location.href = this.site_url+"/testimonials/Testimonials_upf.php?tstm_id="+o.responseText;
		this.tstm_id = o.responseText;
		this.step1_div.hide();
		this.getTstmPhotos();//oculta o muestra el formulario de subida de imágenes
		this.step2_div.show();

	}
	
	this.updateStep1Testimony_failure = function()
	{
		alert("Error");	
	}
	
	this.updateStep1Testimony = function()
	{
		//alert("updateStep1Testimony");		
		var callBack=
		{
			success: this.updateStep1Testimony_success,
			failure: this.updateStep1Testimony_failure,
			scope: this
		};		
		//var postData = "action=insertStep1Testimony";
		var postData = "action=updateStep1Testimony&tstm_id="+this.tstm_id+"&tstm_title="+this.step1_frm.tstm_title.value+"&tstm_name="+this.step1_frm.tstm_name.value+"&tstm_flwr1="+this.step1_frm.tstm_flwr1.value+"&tstm_flwr2="+this.step1_frm.tstm_flwr2.value+"&tstm_flwr3="+this.step1_frm.tstm_flwr3.value+"&tstm_flwr4="+this.step1_frm.tstm_flwr4.value+"&tstm_location="+this.step1_frm.tstm_location.value+"&tstm_season="+this.step1_frm.tstm_season.value+"&tstm_weddate="+this.step1_frm.tstm_weddate.value+"&tstm_email="+this.step1_frm.tstm_email.value+"&tstm_testimony="+this.step1_frm.tstm_testimony.value;
		/*YAHOO.util.Dom.get(this.storedAddressesDiv).innerHTML= ("<p>Loading ...<p>");*/
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
	}
	/******************************************
	END SECTION TO UPDATE TESTIMONY
	******************************************/	

	/******************************************
	START SECTION TO INSERT TESTIMONY
	******************************************/	
	this.insertStep1Testimony_success = function(o)
	{
		//alert(o.responseText);
		//document.location.href = this.site_url+"/testimonials/Testimonials_upf.php?tstm_id="+o.responseText;
		this.tstm_id = o.responseText;
		this.step1_div.hide();
		this.step2_div.show();
	}
	this.insertStep1Testimony_failure = function(o)
	{
		alert("Error in insertStep1Testimony function");
	}

	this.insertStep1Testimony = function()
	{
		//alert("insertStep1Testimony");
		var callBack=
		{
			success: this.insertStep1Testimony_success,
			failure: this.insertStep1Testimony_failure,
			scope: this
		};		
		var postData = "action=insertStep1Testimony&tstm_title="+this.step1_frm.tstm_title.value+"&tstm_name="+this.step1_frm.tstm_name.value+"&tstm_flwr1="+this.step1_frm.tstm_flwr1.value+"&tstm_flwr2="+this.step1_frm.tstm_flwr2.value+"&tstm_flwr3="+this.step1_frm.tstm_flwr3.value+"&tstm_flwr4="+this.step1_frm.tstm_flwr4.value+"&tstm_location="+this.step1_frm.tstm_location.value+"&tstm_season="+this.step1_frm.tstm_season.value+"&tstm_weddate="+this.step1_frm.tstm_weddate.value+"&tstm_email="+this.step1_frm.tstm_email.value+"&tstm_testimony="+this.step1_frm.tstm_testimony.value;
		/*YAHOO.util.Dom.get(this.step1_div).innerHTML= ("<p>Loading ...<p>");*/
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);		
		
	}
	/******************************************
	END SECTION TO INSERT TESTIMONY
	******************************************/	

	/******************************************
	START SECTION BUTTON GO TO STEP2
	******************************************/	
	this.goToStep2_btn_click = function(e, obj)
	{
		this.error = "";
		var initMessage = "Please enter the following information to continue :";
		
		//Validate all fields of newTestimony_frm 
		if(this.trim(this.step1_frm.tstm_title.value) == "")
			this.error += " Title,";

		if(this.trim(this.step1_frm.tstm_name.value) == "")
			this.error += " Name,";
		
		if(this.step1_frm.tstm_flwr1.value == 0 && this.step1_frm.tstm_flwr2.value == 0 && this.step1_frm.tstm_flwr3.value == 0 && this.step1_frm.tstm_flwr4.value == 0)
			this.error += " At Least One Flower,";

		if(this.trim(this.step1_frm.tstm_location.value) == "")
			this.error += " Location,";

		if(this.trim(this.step1_frm.tstm_weddate.value) == "")
			this.error += " Wedding Date,";

		if(this.trim(this.step1_frm.tstm_email.value) == "")
			this.error += " Email,";
		else
			{
				if(this.validateEmail(this.step1_frm.tstm_email.value) != 0)
					this.error += " Not Valid Email,";
			}
		
		if(this.trim(this.step1_frm.tstm_testimony.value) == "")
			this.error += " Your Testimony,";			
		
		if(this.error != "")
		{
			this.error = initMessage+this.error.substr(0,this.error.length-1)+".";
			alert(this.error);
		}
		else
		{
			if(this.tstm_id != "")
				this.updateStep1Testimony();
			else
				this.insertStep1Testimony();	
		}
	}
	/******************************************
	END SECTION BUTTON GO TO STEP2
	******************************************/	

	/******************************************
	START SECTION INIT
	******************************************/	
	this.init = function(site_url, tstm_id)
	{
		//General initializations
		this.site_url = site_url;		
		this.tstm_id = tstm_id;
		this.processorUrl = this.site_url+"/model/Testimonials_new_MDL.php";
		this.imagesUrl = this.site_url+"/site_files/FiftyFlowers/Image/Testimonials/";
		
		//Initializations for step1
		this.step1_div = new YAHOO.widget.Module("step1_div", { visible: true }); 
		this.step1_div.render(); 
		this.step1_frm = YAHOO.util.Dom.get("step1_frm");
		this.step1_frm.reset();

		//Initializations for step2
		this.step2_div = new YAHOO.widget.Module("step2_div", { visible: false }); 
		this.step2_div.render(); 
		this.step2_frm = YAHOO.util.Dom.get("step2_frm");
		this.step2_frm.reset();

		this.step2_frm_div = YAHOO.util.Dom.get("step2_frm_div");
		this.step2_frm_div_module = new YAHOO.widget.Module("step2_frm_div", { visible: true }); 
		this.step2_frm_div_module.render(); 

		//Initializations for step3
		this.step3_div = new YAHOO.widget.Module("step3_div", { visible: false }); 
		this.step3_div.render(); 
		
		//Listeners for step1
		YAHOO.util.Event.addListener("goToStep2_btn", "click", this.goToStep2_btn_click, this, true);
		
		//Listeners for step2		
		YAHOO.util.Event.addListener("goBackStep1_btn", "click", this.goBackStep1_btn_click, this, true);
		YAHOO.util.Event.addListener("uploadImages_btn", "click", this.uploadImages_btn_click, this, true);
		YAHOO.util.Event.addListener("goToStep3_btn", "click", this.goToStep3_btn_click, this, true);
		
		//Listeners for step3		
		YAHOO.util.Event.addListener("goBackStep2_btn", "click", this.goBackStep2_btn_click, this, true);
		YAHOO.util.Event.addListener("submitTestimonial_btn", "click", this.submitTestimonial_btn_click, this, true);
		
	}
	/******************************************
	END SECTION INIT
	******************************************/	

}
