
function validatePreviewFormData()
{	
	previewStoryClose();
	evtPreview ="SubmitStory";
	return validateFormData(evtPreview);	
}
var acceptTems=false;
function validateFormData(evtPreview)
{	
	if(evtPreview =="Preview")
	{
		acceptTems=true;
	}
	if(evtPreview =="SubmitStory")
	{
		acceptTems=false;
	}
 	var firstName=(document.getElementById("firstName")).value;
	var lastName=(document.getElementById("lastName")).value;	
	var emailadd1=(document.getElementById("emailadd1")).value;
	var emailadd2=(document.getElementById("emailadd2")).value;	
	var city=(document.getElementById("city")).value;
	var state=(document.getElementById("state")).value;
	var zip=(document.getElementById("postcode")).value;	
	var petName=(document.getElementById("petName")).value;
	var petStoryTitle=(document.getElementById("petStoryTitle")).value;
	var petStory=(document.getElementById("petStory")).value;

	var adoptionyear=(document.getElementById("adoptionyear")).value;
	var adoptionmonth=(document.getElementById("adoptionmonth")).value;


	/*
		var petPhoto=(document.getElementById("uploadImage")).value;	
		alert(document.getElementById("uploadImage"))	;
		alert(petPhoto)	;
	*/
	
	resetErrorLabel();
	resetBGColor();
	
	
	var errorFound = false;
	var focusElement="";
	var scrollLocation=0;
	var result = true;

	
	if(isEmptyString(firstName)){
		//alertMessage = alertMessage + "\n" + "Please enter your first name.";
		setBGColor('firstName');
		//setValue('e1',"Please enter your first name.");
		result = false;
		if(!errorFound){errorFound = true;focusElement="firstName";}

	}
	
	
	if(isEmptyString(lastName)){
		/*alertMessage = alertMessage + "\n" + "Please enter your last name.";*/
		setBGColor('lastName');
		//setValue('e2',"Please enter your last name.");		
		result = false;
		if(!errorFound){errorFound = true; focusElement="lastName";}
	}
	
	
	if(isEmptyString(emailadd1)){		
		//setValue('e3',"Please enter email address.");		
		setBGColor('emailadd1');
		result = false;
		if(!errorFound){errorFound = true;focusElement="emailadd1";}
	}else{
		if(eMailIDcheck(emailadd1))
		{
			setBGColor('emailadd1');
			//setValue('e3',"Please enter valid email address.");			
			result = false;
			if(!errorFound){errorFound = true;focusElement="emailadd1";}
		}
	}
	
	
	if(isEmptyString(emailadd2)){		
		//setValue('e4',"Please confirm your email address.");		
		result = false;
		setBGColor('emailadd2');
		if(!errorFound){errorFound = true;focusElement="emailadd2";}
	}
	else
	{
			if(emailadd1 != emailadd2)
			{
				//setValue('e4',"Provided emails are not matching.");
				setBGColor('emailadd2');
				result = false;
				if(!errorFound){errorFound = true;focusElement="emailadd2";}
			}
	}
		
	
	if(isEmptyString(city)){		
		//setValue('e5',"Please enter your city.");		
		setBGColor('city');
		result = false;
		if(!errorFound){errorFound = true;focusElement="city";}
	}


	if(isEmptyString(state)){		
		//setValue('e6',"Please enter your state.");
		setBGColor('state');
		result = false;
		if(!errorFound){errorFound = true;focusElement="state";}
	}

	if(isEmptyString(zip)){				
		//setValue('e7',"Please enter your zip code.");
		setBGColor('zip');
		result = false;
		if(!errorFound){errorFound = true;focusElement="zip";}
	}else{
		pat=/^[0-9]*$/;		
		if(!pat.test(zip)){
			//setValue('e7',"Please enter your zip code.");
			setBGColor('zip');
			result = false;
			if(!errorFound){errorFound = true;focusElement="zip";}
		}
	}
	
	if(isEmptyString(adoptionyear)){
		setBGColor('adoptionyear');
		result = false;
		if(!errorFound){errorFound = true;focusElement="adoptionyear";}

	}

	if(isEmptyString(adoptionmonth)){
		setBGColor('adoptionmonth');
		result = false;
		if(!errorFound){errorFound = true;focusElement="adoptionmonth";}

	}

	if(isEmptyString(petName)){		
		//setValue('e9',"Please enter pet namme.");
		setBGColor('petName');
		result = false;
		if(!errorFound){errorFound = true;focusElement="petName";}
	}

	if(isEmptyString(petStoryTitle)){				
		//setValue('e10',"Please enter story title.");
		setBGColor('petStoryTitle');
		result = false;
		if(!errorFound){errorFound = true;focusElement="petStoryTitle";}
	}
	
	
	if(isEmptyString(petStory)){				
		//setValue('e11',"Please enter story.");
		setBGColor('petStory');
		result = false;
		if(!errorFound){errorFound = true;focusElement="petStory";}
	}
	if(!result)
	{
		//alert(alertMessage);
		//alert(focusElement);
		//alert(scrollLocation);
		if(focusElement != "")
		{
			document.getElementById(focusElement).focus();
		}
		// show error pop up
		showErrorPopup();
		return false;
	}
	else if(false==acceptTems){
			
			if(evtPreview !="Preview")
			{
				if(true!=window.showModalDialog("/iams/en_US/jsp/angel2/Iams_Content_GuideLines.jsp","name", "dialogWidth:650px;dialogHeight:500px"))
				{
					//showErrorPopup();
					return false;
				}
				else{
					acceptTems= true;
				}
			}
			
		}

	replaceTextAreaData();

	
 	return true;
	 
}

function showErrorPopup(){
	/*
	var d = document.getElementById("errorDiv");
	d.style.display = "block";
	*/
	var dc = document.getElementById("Error_Disp");
	dc.style.display = "block";
}
function hideErrorPopup(){
	/*
	var d = document.getElementById("errorDiv");
	d.style.display = "none";
	*/
	var dc = document.getElementById("Error_Disp");
	dc.style.display = "none";
}



function setValue(filed , value){ 
 	var errorDiv = document.getElementById(filed);
 	errorDiv.innerHTML = value;	
 }

function resetErrorLabel(){	
	//alert("IN resetErrorLabel");
	/*setValue("e1","");
	setValue("e2","");
	setValue("e3","");
	setValue("e4","");
	setValue("e5","");
	setValue("e6","");
	setValue("e7","");
	setValue("e8","");
	setValue("e9","");
	setValue("e10","");
	setValue("e11","");	*/

	//alert("OUT resetErrorLabel");
} 

function resetBGColor(){
	/*
	var firstName=(document.getElementById("firstName"));
	firstName.style.backgroundColor = '#ffffff';
	var lastName=(document.getElementById("lastName"));	
	lastName.style.backgroundColor = '#ffffff';
	var emailadd1=(document.getElementById("emailadd1"));
	emailadd1.style.backgroundColor = '#ffffff';
	var emailadd2=(document.getElementById("emailadd2"));	
	emailadd2.style.backgroundColor = '#ffffff';
	var city=(document.getElementById("city"));
	city.style.backgroundColor = '#ffffff';
	var state=(document.getElementById("state"));
	state.style.backgroundColor = '#ffffff';
	var zip=(document.getElementById("zip"));	
	zip.style.backgroundColor = '#ffffff';
	var petName=(document.getElementById("petName"));
	petName.style.backgroundColor = '#ffffff';
	var petStoryTitle=(document.getElementById("petStoryTitle"));
	petStoryTitle.style.backgroundColor = '#ffffff';
	var petStory=(document.getElementById("petStory"));
	petStory.style.backgroundColor = '#ffffff';	
	*/
}

  function setBGColor(controlName){
  	/*
  	var control = document.getElementById(controlName); 
  	if(control){
  		control.style.backgroundColor = '#fff1d2';
  	}
  	*/
  }


function previewStory(){
	var evtPreview ='Preview';
	if(validateFormData(evtPreview))
	{
		
		var d = document.getElementById("Preview_Story");
		d.style.display = "block";
		var d = document.getElementById("ShareStory");
		d.style.display = "none";
		var previewTextArea = document.getElementById("previewTextArea"); 
		var textAreaData= document.getElementById("petStory").value;
		previewTextArea.value = textAreaData;

		document.getElementById("previewTextArea").focus();
        window.location="#top";
		//acceptTems= true;
	}
	/*else if(acceptTems==false && true==window.showModalDialog("/iams/en_US/jsp/angel2/Iams_Content_GuideLines.jsp","name", "dialogWidth:650px;dialogHeight:500px")){
		var d = document.getElementById("Preview_Story");
		d.style.display = "block";
		var d = document.getElementById("ShareStory");
		d.style.display = "none";
		var previewTextArea = document.getElementById("previewTextArea"); 
		var textAreaData= document.getElementById("petStory").value;
		previewTextArea.value = textAreaData;
		acceptTems= true;
	}
	else{
		showErrorPopup();
		return false;
	}*/
}

function previewStoryClose(){	
	var d = document.getElementById("Preview_Story");
	d.style.display = "none";
	var d = document.getElementById("ShareStory");
	d.style.display = "block";

	/*document.getElementById("tellUsDiv").style.display= "none";*/
}
function storyFormCancel(){	
	var tellUsDiv = parent.window.document.getElementById('tellUsDiv');
	if(tellUsDiv){
		tellUsDiv.style.display= "none";
	}
}
function storyPreviewCancel(){
	previewStoryClose();
	var tellUsDiv = parent.window.document.getElementById('tellUsDiv');
	if(tellUsDiv){
		tellUsDiv.style.display= "none";
	}
}


function replaceTextAreaData()
{
		//alert("IN replaceTextAreaData");
		var objTxtArea;		
		var convertedValue =AddBR(document.getElementById("petStory").value);
		document.getElementById("petStory").value=convertedValue;
		//alert("OUT replaceTextAreaData");
		return true; //replace it by true
}


function AddBR(value) 
{
		//alert("IN replaceBR");
		var result = "";
		var length = value.length;
		for(var index=0;index<length;index++)
		{
			var c = value.charAt(index);
			var asciiValue = c.charCodeAt(0);
			
			if(asciiValue==10 )
			{
				result = result + "<BR/>"
			}
			else
			{
				if(asciiValue!=13 )
				{	
					result = result+ c
				}
			}
		}	
		//alert(result);
		//alert("OUT replaceBR");
		return result;
 }



function replaceBROnload(){   
 // alert("IN replaceBROnload");
  var objTxtArea;
  objTxtArea=document.getElementById("petStory");
  //alert(objTxtArea);
  if(objTxtArea && objTxtArea.value != "")
  {
 	 var convertedValue =replaceBR(objTxtArea.value);
 	 //alert(convertedValue);
	 objTxtArea.value=convertedValue;
  }  
	//alert("OUT replaceBROnload");  
 }

  function replaceBR(value) 
  {
  var add = '\n';
  var newValue = value.replace(/<BR\/>/g, add);
  return newValue;  
  }
  

