
var siteName = 'hr'; //change to appropriate site

 Ext.onReady(function() {
    Ext.QuickTips.init();
	featured = 2;   
    if(Ext.get("rotate_article") != null){
	    var mgr = new Ext.Updater("rotate_article");
	    mgr.showLoadIndicator=false; 
		mgr.startAutoRefresh(15,rotateSpotlight);
	}
	
	function rotateSpotlight(){
		showSpotlight(featured,'reviewsSpotlight_'+featured);
		featured = featured + 1;
		if(featured > 4)
			featured = 1;
		
	}	

	// This function will show the comments for a passed article Id.
	//This can be attached to any div with an event listner  example  Ext.get('showcommentbutton-div').on('click', submitForm);
    if (Ext.get('showreview-div') != null) {
		Ext.get('showreview-div').on('click', showReviewComments);
	} 
	var movieStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			 url: '/'+siteName+'/userreview/getMovieListing.do',timeout:'60000' , method:'POST'
		}),
		id:'movieStoreId',
		reader: new Ext.data.JsonReader({
			root: 'records',
			fields: [
				'movieName','movieId'
			]
		})
	});
	
	var locationStore = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			 url: '/'+siteName+'/userreview/getLocationListing.do',timeout:'60000'
		}),
		reader: new Ext.data.JsonReader({
			root: 'records',
			fields: [
				'zipCode','city','state','locationDetail'
			]
		})
	});
	
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);


   movieStore.on('loadexception', function(o, response, e) {
  //	Ext.MessageBox.alert('Message', 'loadexception occured');
  });
   
   if (Ext.get('movieselect-div') !=null){
   
		var combo = new Ext.form.ComboBox({
	        store: movieStore,
			displayField: 'movieName',
			valueField:'movieId',
			hiddenName:'updateModel.commentEntityId', 
	        typeAhead: true,
	        forceSelection: true,
	        triggerAction: 'all',
	        emptyText:'Select a Film...',
	        selectOnFocus:true,
			enableKeyEvents:true,
	        renderTo: 'movieselect-div',
			minChars:'2',
	     	mode: 'remote',
			name: 'movieId',
			id:'movieCombo'
			
	    });
		
		combo.setSize({width:200,height:20})
	}
	   if (Ext.get('locationselect-div') !=null){
		var locationCombo = new Ext.form.ComboBox({
        store: locationStore,
		displayField: 'locationDetail',
		valueField:'zipCode',
		hiddenName:'updateModel.zipCode', 
        typeAhead: true,
        forceSelection: false,
        triggerAction: 'all',
        emptyText:'Type a city,state or a zipcode...',
        selectOnFocus:true,
		enableKeyEvents:true,
        renderTo: 'locationselect-div',
		minChars:'3',
     	mode: 'remote',
		name: 'locationCombo',
		id:'locationComboId',
		editable: true, 
		hideTrigger: false,
		regex:new RegExp("(^[a-zA-Z ]*[,][ a-zA-Z]*$)|(^[0-9]{5})"),
		regexText:'Please re enter the 5 digit zip code, or a city and state seperated with comma. Example: Albany\, NY or 12201',
		triggerClass: 'noTriggerArrow'
    });
	  locationCombo.setSize({width:180,height:20})
	}
	
	
	if (Ext.get('zipcode-div') !=null){
		var locationCombo = new Ext.form.TextField({
        name :'updateModel.zipCode', 
        emptyText:'Type a zipcode...',
        id:'locationComboId',
		regex:new RegExp("(^[0-9]{5}$)"),
		regexText:'Invalid zipCode,Please re enter the 5 digit zip code',
        renderTo: 'zipcode-div'
    });
	  locationCombo.setSize({width:150,height:20})
	}
	
	
		locationStore.on('beforeload', function(){
		
		    Ext.apply(locationStore.baseParams, {
			location:locationCombo.getValue()
			});
	    });

// if it is a view page dispable the movie selection combo		
	if ( (sPage == 'viewreview.jsp') || (sPage =='updateUserReviews-4viewpage.do' )){
		combo.setDisabled(true);
	}
	
	
   if ( (Ext.get('moveIdHidId')) != null) {
	   movieStore.on('beforeload', function(){
			  Ext.apply(movieStore.baseParams, {
			    movieId:Ext.get('moveIdHidId').dom.innerHTML
			  });
	    });
  }	
  
 //retain the selected location on  showing error form
 	locationStore.on('beforeload', function(){
			    if ((document.getElementById("locationComboValueId") != null) && (document.getElementById("locationComboValueId").value != "")) {
				   Ext.apply(locationStore.baseParams, {
						query:document.getElementById("locationComboValueId").value
					});
			  }
    });
	
	if ((document.getElementById("locationComboValueId") != null) && (document.getElementById("locationComboValueId").value != "")){
		locationStore.load(); 
	}

 	locationStore.on('load', function(){
		if ( (document.getElementById("locationComboValueId") != null) && (document.getElementById("locationComboValueId").value != "")){
		locationCombo.setValue(document.getElementById("locationComboValueId").value);
		}
	 });
	 

		
     if (  sPage == 'index1.jsp') {
	   movieStore.on('beforeload', function(){
			  Ext.apply(movieStore.baseParams, {
			    topMovie:'yes'
			  });
	    });
		
		combo.setEditable(false);
  }	
  
   if ( (Ext.get('moveIdHidId')) != null) {
		movieStore.on('load', function(o, response, e) {
		combo.setValue(Ext.get('moveIdHidId').dom.innerHTML);
		});
   }
  
  movieStore.load(); 

	 
	function showReviewComments() {
	var objRegExp  = /(^\d{5}$)/;
	var location =locationCombo.getRawValue();
	var city ='';
	var state='';
	var zipCode='';
	var listreview = document.getElementById("listreview-div");
	
// If the location zipcode is 0000, that means user selected a city and state from the autocomplete. in that case fire ajax request with city,state to retrive all the 
//comments for that city,state combo
/*	if (locationCombo.getValue() == '00000'){
		location = Ext.getCmp('locationComboId').lastSelectionText;
	}
	*/
	if(!locationCombo.validate()){
		listreview.innerHTML = location + " is not a valid location. Please re enter the 5 digit zip code, or a city and state seperated with comma "
		return;
	}
	var commaPos =location.indexOf(',');
	if ( commaPos== -1){
	  zipCode = location;
	} else {
	  city =  location.substring(0, commaPos).trim();
	  state =  location.substring(commaPos+1,location.length).trim();
	} 
//todo: add regular expression for city,state pattern	
	
	
	if ( ( city =='' ) && (state=='') && (zipCode=='')){
		listreview.innerHTML = location + " is not a valid location. Please re enter the 5 digit zip code, or a city and state seperated with comma "
		return;
	}
	
	
	if (zipCode != ''){
	    if (!objRegExp.test(zipCode)){
			listreview.innerHTML = zipCode + " is not a valid location. Please re enter the 5 digit zip code, or a city and state seperated with comma "
			return;
		}
	}
	

	listreview.innerHTML ="<img src=\"/hr/inc/images/ajax-loader.gif\">";
//	Ext.Msg.wait('Loading Reviews. Please Wait..');
		var recordsPerPage=5;
		var store = new Ext.data.Store({
				proxy: new Ext.data.HttpProxy({
					 url: '/'+siteName+'/userreview/displayUserReviews.do',timeout:'60000'
				}),
				reader: new Ext.data.JsonReader({
					root: 'records',
					fields: [
						'title','description','userName','postedDate', 'zipCode','projectName','commentId','commentEntityId'
					]
				})
			});
			if ( (zipCode != '') && (zipCode != '00000')) {
			   Ext.apply(store.baseParams, {
				zipCode:zipCode,
			    recordsPerPage:recordsPerPage
			   });
		   } else {
			   Ext.apply(store.baseParams, {
				city:city,
				state:state,
			    recordsPerPage:recordsPerPage
			   });
		   }
			store.load();
			
			store.on('loadexception', function(o, response, e) {
			 listreview = document.getElementById("listreview-div");
			 listreview.innerHTML = " ";
//			 Ext.MessageBox.hide();
 	     	 listreview.innerHTML =""
             
			 if (document.getElementById("userzipcode") == null){
				Ext.MessageBox.alert('Message', 'No user reviews found for the entered Location :' + location);
			 }
		  });
			
			 	store.on('load', function(store, records, options) {
//				 Ext.MessageBox.hide();
         		var listreview = document.getElementById("listreview-div");
				listreview.innerHTML =""
//				Ext.MessageBox.alert('Message', 'load completed');
					var commentListing = "" ;
					commentListing += "<ul>";
					commentCount=0;
					store.each(function(record)	{
						  commentCount += 1;
						  
						   commentListing += "<li><div class=\"commentText\"><span class=\"userProjectReview\">" + record.get('projectName') +"</span><br/><a href=\"/hr/userreview/viewreview.jsp?reviewId="+record.get('commentId') +  "\"> " + record.get('title') +"</a></div>";
						   commentListing += "<div class=\"commentText\">"+ record.get('userName') +"</div></li>";
					});
					
					if (commentCount >0){
					commentListing = commentListing + "</br>" + " <a href=\"/hr/userreview/alluserreviews.jsp?zipCode="+zipCode+"&city="+city+"&state="+state+"\">More Local Reviews&raquo;</a>";
					}
					 
					listreview.innerHTML = commentListing;
		});
	};
   
  if ( document.getElementById("userzipcode")!= null) {
    if(  sPage == 'index1.jsp' ) {
	locationCombo.setValue(document.getElementById("userzipcode").value);
	showReviewComments();
	locationCombo.setValue('');
	} else if ( (locationCombo != null) && (document.getElementById("userzipcode") != null)){
	locationCombo.setValue(document.getElementById("userzipcode").value);
	}
 }
 	if ( sPage == 'selectproject.jsp'){
		combo.initialConfig.hideTrigger = true;
	}
    
	});



