function Utilities()
{
	this.loggedIn_ = false;
	this.memName_ = '';
	
	this.clickedField=function(id,initVal)
	{
		if(document.getElementById(id).value==initVal)
		{
			document.getElementById(id).value='';	
		}
		else
		{
			return;
		}
	}
	
	this.clearList=function()
	{
		document.getElementById('listContent').innerHTML='';
	}
	
	
	
	this.logMe = function(msg)
	{
	 	var logWin=window.open("","Log Window");
	  	if(logWin==null)
		{
			alert ("You may need to disable your popup blocker");	
		}
		  logWin.document.write(msg);
		   logWin.document.writeln("<br>");
	}
	
	this.listNavigator = function()
	{
		for(var i in navigator)
		{
			GLog.write(i.toString()+">>>>>"+navigator[i]);	
		}
	}
	
	this.isIE = function()
	{
		if(navigator.appName.indexOf("Microsoft")!=-1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	this.isOldIE = function()
	{
		if(navigator.appVersion.indexOf("MSIE 7")!=-1 || navigator.appVersion.indexOf("MSIE 6")!=-1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	this.getMapSides=function(map)
	{
		var btmLeft=map.fromLatLngToDivPixel(map.getBounds().getSouthWest());
	//	logMe("Left"+btmLeft);
		var topRight=map.fromLatLngToDivPixel(map.getBounds().getNorthEast());
		var sides=new Array();
		sides['l']=btmLeft.x;
		sides['r']=topRight.x;
		sides['t']=topRight.y;
		sides['b']=btmLeft.y;
		return sides;
	}

   
    this.removeAlt=function()
	{
		var alt =document.getElementById('alt_popup');
		if(alt!=null)
		{
			alt.parentNode.removeChild(alt);	
		}
			
	}
	
	this.alt=function(text,x,y)
	{	
		if(document.getElementById('alt_popup'))
		{
			this.removeAlt();
		}

		var alt=document.createElement('div');
			alt.id="alt_popup";
			alt.style.display="block";
			alt.style.position="relative";
			alt.style.height="15px";
			alt.style.width="145px";
			//alt.style.backgroundImage="url(../../images/interface/bg/altBg.png)";alt.style.backgroundRepeat="repeat-x";
			alt.style.backgroundColor="#EBEBEB";
			alt.style.border="solid 1px #969696";
			alt.style.fontWeight="bold";
			alt.style.padding="3px 3px 4px 3px";
			alt.style.left	=	x+"px";
			alt.style.top	=	y+"px";
			alt.appendChild( document.createTextNode(text) );
			document.getElementById('map').appendChild(alt);
	}
	
	
	this.swapNodes=function(oldNode,newNode)
	{
		var parent = oldNode.parentNode;
		parent.removeChild(oldNode);
		parent.appendChild(newNode);
	}
	
	this.offSetView=function(point)//adjusts map position so popup doesn't underlap top of map
	{
			var adjPnt=new GPoint(point.x, point.y-30);
			var adjLatLng= map.fromDivPixelToLatLng(adjPnt);
			map.panTo(adjLatLng);
	}
	
	this.baseName=function(filePath)
	{
		var baseName=filePath.substr(filePath.lastIndexOf('/')+1);
		return baseName;
		
	}
	
	this.getTypeFromTitle=function(title)
	{
		return title.toLowerCase();
	}
	
	this.getTypeFromTag=function(id)
	{
		var type = id.substr(id.length-2);
		return type;
	}
	

    this.getTrueIdFromTag=function(id)
	{
		var type = id.substring(0,id.length-2);
		return type;
	}
	
	this.getTagFromType=function(type)
	{
		switch(type)
		{
			case 'vintage':
					return 'vi';
					break;
			
			case 'accommodation':
					return 'ac';
					break;
					
			case 'dining':
					return 'di';
					break;
					
			case 'touring':
					return 'to';
					break;
		   
		   case 'spa':
					return 'sp';
					break;
		   
		   case 'retail':
					return 're';
					break;
					
		  case 'winery':
					return 'wi';			
				 break;
		}
	}
	
	this.getDirForType=function(type)
	{
		switch(type)
		{
				case 'winery':	
				
					var dir ='wineries';
					
					break;
				
				case 'accommodation':	
				
					var dir ='accommodation';
					
					break;
				
				case 'dining':
					
					var dir ='dining';
					
					break;
				
				case 'touring':	
				
					var dir ='touring';
					
					break;
					
			   case 'shopping':
			   		
					var dir = 'shopping';
					break;
					
			   case 'park':
			   		
					var dir = 'parks';
					break;
					
			   case 'selfdrive':
			   		
					var dir = 'selfdrive';
					break;		
		}
		return dir;
	}
	
	this.findIndexSimple=function(findWhat,inArray)
	{
		 for(var i=0 ; i < inArray.length ; i++)
		 {
			 if(inArray[i]==null || typeof inArray[i]=="undefined")
			 {
				return 0;	
			 }
			 
			 if(inArray[i]==findWhat)
			 {
				return i; 
			 }
		 } 
	}
	
	this.findIndex=function(findBy,findWhat,inArray)
	{
		 for(var i=0 ; i < inArray.length ; i++)
		 {
			 if(inArray[i]==null || typeof inArray[i]=="undefined")
			 {
				return 0;	
			 }
			
			 if(inArray[i][findBy]==findWhat)
			 {
				return i; 
			 }
			
		 } 
	}
	
	this.findArrayMatch=function(findByArray,findWhatArray,inArray)
	{
		 for(var i=0 ; i < inArray.length ; i++)
		 {
			 if(inArray[i]==null || typeof inArray[i]=="undefined")
			 {
				return 0;	
			 }
			 var matchCount = 0 ;
			 
			 //For item inArray[i] if each of the fields in findByArray for this item equal to corresponding value in findWhat array match increments
			 for(var j=0; j<findByArray; j++)
			 {
				if( inArray[i][ findByArray[j] ]== findWhatArray[j])
				{
					matchCount++;	
				}
			 }
			 
			 //If matches equal to number of fields in findByArray then this is the correct item
			 if(matchCount==findByArray.length-1)
			 {
				return i; 
			 }
		 } 
	}
	
	this.sortAsc = function(array,sortBy,sortBy2,sortBy3)
	{
	
		function compare(arrayItem1,arrayItem2)
		{
			var x 	= arrayItem1[sortBy];
			var xa	= arrayItem1[sortBy2];
			var xb	= arrayItem1[sortBy3];
			
			var y 	= arrayItem2[sortBy];
			var ya	= arrayItem2[sortBy2];
			var yb	= arrayItem1[sortBy3];
			
			if(x < y )
			{
				return -1;	
			}
			else if(x > y)
			{
				return 1;	
			}
			else
			{
				
				if(xa < ya )
				{
					return -1;	
				}
				else if(xa > ya)
				{
					return  1;	
				}
				else
				{
					
					if( parseInt(xb) < parseInt(yb) )
					{
						return -1;	
					}
					else if(xb > yb)
					{
						return 1;	
					}
					else
					{
						return 0;
					}
				}
			}
		}
		
		array.sort(compare);
		
		return array;
	}
	
	this.sortDesc = function(array,sortBy,sortBy2,sortBy3)
	{
	
		function compare(arrayItem1,arrayItem2)
		{
			var x 	= arrayItem1[sortBy];
			var xa	= arrayItem1[sortBy2];
			var xb	= arrayItem1[sortBy3];
			
			var y 	= arrayItem2[sortBy];
			var ya	= arrayItem2[sortBy2];
			var yb	= arrayItem1[sortBy3];
			
			if(x < y )
			{
				return 1;	
			}
			else if(x > y)
			{
				return -1;	
			}
			else
			{
				
				if(xa < ya )
				{
					return -1;	
				}
				else if(xa > ya)
				{
					return  1;	
				}
				else
				{
					
					if( parseInt(xb) < parseInt(yb) )
					{
						return -1;	
					}
					else if(xb > yb)
					{
						return 1;	
					}
					else
					{
						return 0;
					}
				}
			}
		}
		
		array.sort(compare);
		
		return array;
	}

	this.remFromArray 	=	function (index,array)
	{
		array.splice(index,1);
	}
	
	this.swapArrayItems	=	function(index1,index2,array)
	{
		var item1	=	array	[ index1];
		var item2	=	array	[ index2 ];
		
		array[index1]=item2;
		array[index2]=item1;
	}
	
	this.listArray=function(array)
	{
		for(var i in array)
		{
			this.logMe(i+" > "+array[i]);	
		}
	}
	
	this.listArray2=function(array)
	{
		for(var i=0;i<array.length;i++)
		{
			
			for(var j in array[i])
			{
				this.logMe(j +" = "+array[i][j]);
			}
			
		}
	}
	
	this.titleCase=function(str)
	{
		var strBits=str.split(" ");
	
		for(i=0;i<strBits.length;i++)
		{	
			strBits[i]=strBits[i].substr(0,1).toUpperCase()+strBits[i].substr(1).toLowerCase();
		}
		
		var strNew=strBits.join(" ");
		
		return strNew;
	}
	
	this.isEmptyList = function()
	{
		var list=document.getElementById('listContent');
		if(list.children.length==0)
		{
			return true;		
		}
		else
		{
			return false;		
		}
	}
	
	
	
	
	this.getLogoDir=function(name)
	{
	  var logo_dir=null;
			
		var firstChar=name.substr(0,1);
			
			firstChar=firstChar.toUpperCase();
			
			switch(firstChar)
			{
				case 0:
				case 1:
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
				case 7:
				case 8:
				case 9:
					logo_dir="0-9";
					break;
						
				case 'A':
				case 'B':
						logo_dir="A-B";
						break;
						
				case 'C':
				case 'D':
						logo_dir="C-D";
						break;
						
				case 'E':
				case 'F':
				case 'G':
				case 'H':
						logo_dir="E-H";
						break;
					
				case 'I':
				case 'J':
				case 'K':
				case 'L':
				case 'M':
						logo_dir="I-M";
						break;
				
					
				case 'N':
				case 'O':
				case 'P':
				case 'Q':
				case 'R':
						logo_dir="N-R";
						break;
				
				case 'S':
				case 'T':
				case 'U':
				case 'V':
						logo_dir="S-V";
						break;
						
				case 'W':
				case 'X':
				case 'Y':
				case 'Z':
						logo_dir="W-Z";
						break;
			}
		
		return logo_dir;
	}//end get logo dir

	this.addVarHolder=function()
	{	
		if(!document.getElementById("vars_holder"))
		{
			var vars = document.createElement('script');
			vars.id   ='vars_holder';  
			vars.type ='text/javascript';
			document.body.appendChild(vars);	
		}
	}
	
	this.remVarHolder=function()
	{	
		document.getElementById('vars_holder').parentNode.removeChild(document.getElementById('vars_holder'));
	}

	this.getFilesInDir=function(dir)
	{
			if(window.XMLHttpRequest)
			{
				var xhr = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) 
			{
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
				 
				var postData="";
				postData+="dir="+encodeURI(dir);
				
				xhr.onreadystatechange=function()
												{											
													if(this.readyState==4)
													{	
														var resp=this.responseText;
														
														utils.stopPBCycle();
														
															if(document.getElementById('vars_holder'))
															{
																	utils.remVarHolder();
																	utils.addVarHolder();
																document.getElementById('vars_holder').text=resp;
																//utils.logMe(format.nl2br(document.getElementById('vars_holder').text));
															
															}
															else
															{
																utils.addVarHolder();
																document.getElementById('vars_holder').text=resp;
															}
													}
													else
													{
														/*var error=xhr.responseStatus+"||"+xhr.responseText;
														var text=document.createTextNode(xhr.responseText);
														logWindow.appendChild(text);*/
													}
													return;											
												}
		
				try
				{
					 xhr.open("POST","php/processes/get_files_process.php",true);
					 xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					 xhr.send(postData);
				}
				catch(error)
				{
				//	
		//			var text=document.createTextNode("ERROR: "+error.name+"|"+error.description+"|"+error.lineNumber);
		//			logWindow.appendChild(text);
		//			var br=document.createElement("br");
		//			logWindow.appendChild(br);
				}
					
				return files_in_dir;		
	}


	this.initLoginNotes=function()
	{
		var username	=	document.getElementById('username').value;
		var password	=	document.getElementById('password').value;
		var getData="?username="+encodeURI(username)+"&password="+encodeURI(password);
			if(window.XMLHttpRequest)
			{
				var xhr = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) 
			{
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
				 
			
				
				xhr.onreadystatechange=function()
												{											
													if(this.readyState==4)
													{	
														var resp=this.responseText;
														//utils.logMe(resp);
														if(document.getElementById('vars_holder'))
														{
															utils.remVarHolder();
															utils.addVarHolder();	
															document.getElementById('vars_holder').text=resp;
														}
														else
														{
															utils.addVarHolder();
															document.getElementById('vars_holder').text=resp;
														}
														window.setTimeout(function()
																				   {
																						if(loggedIn)
																						{
																						 	utils.swapLoginFields();
																							notes.init(notes.lab_id_,notes.initVintage_,notes.context_);   
																						}
																						else
																						{
																							alert(loginError);
																						}
																				   },400);
														
													}
													else
													{
														/*var error=xhr.responseStatus+"||"+xhr.responseText;
														var text=document.createTextNode(xhr.responseText);
														logWindow.appendChild(text);*/
													}
												}
		
				try
				{
					 xhr.open("GET","php/processes/login_process_ajax.php"+getData,true);
					 xhr.send(null);
				}
				catch(error)
				{
				//	
		//			var text=document.createTextNode("ERROR: "+error.name+"|"+error.description+"|"+error.lineNumber);
		//			logWindow.appendChild(text);
		//			var br=document.createElement("br");
		//			logWindow.appendChild(br);
				}
	}


	this.initLoginWines=function()
	{
		var username	=	document.getElementById('username').value;
		var password	=	document.getElementById('password').value;
		var getData="?username="+encodeURI(username)+"&password="+encodeURI(password);
			if(window.XMLHttpRequest)
			{
				var xhr = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) 
			{
				var xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
				 
			
				
				xhr.onreadystatechange=function()
												{											
													if(this.readyState==4)
													{	
														var resp=this.responseText;
														//utils.logMe(resp);
														if(document.getElementById('vars_holder'))
														{
															utils.remVarHolder();
															utils.addVarHolder();	
															document.getElementById('vars_holder').text=resp;
														}
														else
														{
															utils.addVarHolder();
															document.getElementById('vars_holder').text=resp;
														}
														window.setTimeout(function()
																				   {
																						if(loggedIn)
																						{
																							utils.swapLoginFields();
																						 	cellar.init();   
																						}
																						else
																						{
																							alert(loginError);
																						}
																				   },400);
														
													}
													else
													{
														/*var error=xhr.responseStatus+"||"+xhr.responseText;
														var text=document.createTextNode(xhr.responseText);
														logWindow.appendChild(text);*/
													}
												}
		
				try
				{
					 xhr.open("GET","php/processes/login_process_ajax.php"+getData,true);
					 xhr.send(null);
				}
				catch(error)
				{
				//	
		//			var text=document.createTextNode("ERROR: "+error.name+"|"+error.description+"|"+error.lineNumber);
		//			logWindow.appendChild(text);
		//			var br=document.createElement("br");
		//			logWindow.appendChild(br);
				}
	}


	this.clearMap=function()
	{
		if(map!=null)
		{
			map.clearOverlays(); 
			map=null;
		}	
	}

	this.isEven=function(num)
	{
			if(num%2==0)
			{
					return true;
			}
			else
			{	
					return false;
			}
	}


	this.swapLoginFields=function()
	{
		var logFlds=	document.getElementById('loginFields');	
		logFlds.innerHTML='';
	
		var a=document.createElement('a');
			a.href="javascript:void()";
			a.target="_self";
			
		var img		=	document.createElement('img');
			img.src	=	'images/interface/buttons/shop_btn.png';
			img.style.border="none";
			a.appendChild(img);
			logFlds.appendChild(a);
		
		var a=document.createElement('a');
			a.href="javascript:void()";
			a.target="_self";
		var img		=	document.createElement('img');
			img.src	=	'images/interface/buttons/contact_btn.png';
			img.style.border="none";
			a.appendChild(img);
			logFlds.appendChild(a);	
			
		var a=document.createElement('a');
			a.className='noUnderLn';
			a.href="logout.php";
			a.target="_self";
	
		
		var img		=	document.createElement('img');
			img.setAttribute("id","logout_btn");
			img.setAttribute("name","logout_btn");
			img.src	=	btns['logout_btn'].src;
			img.style.border="none";
			img.onmouseover=function()
									{
										//this.src=btns['logout_btnO'].src;
										buttons.rollBtn(1,this);
									}
									
			img.onmouseout=function()
									{
										//this.src=btns['logout_btn'].src;
										buttons.rollBtn(0,this);
									}	
											
			a.appendChild(img);
			logFlds.appendChild(a);	
				
			logFlds.appendChild(format.addBr());
			
		var div		=	document.createElement('div');
			div.setAttribute("id","welcomeMsg");
			div.appendChild( 	document.createTextNode( "Welcome back "+utils.titleCase(memName) ));
			logFlds.appendChild(div);
		var div		=	document.createElement('div');
			div.setAttribute("id","memExpWarn");
			div.appendChild( 	document.createTextNode( "You have "+daysRemain+" day(s) of membership remaining" ) );
			logFlds.appendChild(div);
	
			if(!this.isIE())
			{
				
			}
					
			
			
	}
	
	
	this.triggerEvent=function(elemId,eventType)
	{
		var target=document.getElementById(elemId);
		if(this.isIE())
		{
			var evt=document.createEventObject();
			target.fireEvent("on"+eventType,evt);	
		}
		else
		{
			var evt=document.createEvent('MouseEvents');
			 evt.initMouseEvent(eventType, true, true, window,0, 0, 0, 0, 0, false, false, false, false, 0, target);
			 
			 var canceled = !target.dispatchEvent(evt);
			  if(canceled) {
				// A handler called preventDefault
				alert("canceled");
			  } else {
				// None of the handlers called preventDefault
				alert("not canceled");
			  }

		}
	}
	
	this.gotoPage =function (page)
	{
		
		if(page=='taste_tipper.php' && cellar.currentList_!=null)
		{
			page+="?currentList="+cellar.currentList_;
		}
		document.location.href=page;	
	}
	
	this.cancelBubble = function(evt)
	{
		var evt = (window.event)?window.event:evt;
		evt.cancelBubble=true;
	}
}//end class Utilities



