/*

##		 ##			 ##			 ############  	    ##			######     ## #        ##         #########       ##
## #   # ##  	   ##  ##			  ##		  ##  ## 		  ##	   ## 	#      ##		     ##	        ##  ##
##	# #	 ##		  ##    ##	          ##         ##	   ##		  ##	   ##     #    ##		     ##        ##    ##
##   #   ##      ## #### ##	          ##        ## #### ##		  ##       ##       #  ##       ##   ##		  ## ###  ##
##		 ##     ##        ##          ##       ##		 ##		  ##       ##        # ##     #	     ##		 ##	       ##
##		 ##	   ##		   ##		  ## 	  ##          ##    #######	   ##		  ###	   ## ###	    ##          ##

*/ 

//	+-----------------------------------+
//	|	Initialize ajax					|
//	+-----------------------------------+

function newAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

//	+-----------------------------------+
//	|	Change class of any Element		|
//	+-----------------------------------+

function change_class(buttonid, classname)
{
	document.getElementById(buttonid).setAttribute("class", classname);
}

//	+-------------------------------------------------------------------------------+
//	|	Remove space from front and end												|
//	|	Remove more than one space from string as well as check for empty string	|
//	+-------------------------------------------------------------------------------+

function trim(str)
{
    if(!str || typeof str != 'string')
        return false;

    //return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
	var strArr = new Array();
	strArr = str.split("  ");
	for(var i = 0; i < strArr.length ; i++)
	{
		str=str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
	}
	return str;
}


//	+-----------------------------------------------+
//	| check whether radio button Checked or not		|
//	+-----------------------------------------------+
function is_checked(radiobutton)
{
	var radiobutton_len=radiobutton.length;
	for(i=0;i<radiobutton_len;i++)
	{
		if(radiobutton[i].checked)
		{
			return radiobutton[i].value;
			//return true;
		}
	}
	return false;
}

//	+-------------------------------------------------------+
//	| check only for city list select option or textbox		|
//	+-------------------------------------------------------+
function is_valid_city_selection(selectoption)
{
	if(selectoption.value<0 || selectoption.value=='' || !isNaN(selectoption.value) || selectoption.value.length<3)
		return false;
	else
		return selectoption.value;
}

//	+-----------------------------------------------+
//	| check whether select option Selected or not	|
//	+-----------------------------------------------+
function is_valid_selection(selectoption)
{
	if(selectoption.value<0 || selectoption.value=='')
		return false;
	else
		return selectoption.value;
}

//	+-----------------------------------+
//	| focus on the current textfield	|
//	+-----------------------------------+
function textfieldfocus(fieldvalue,fieldid,fieldname)
{
	if(fieldvalue!=false || fieldvalue=='')
	{
		if(fieldvalue===false)
			fieldvalue='';
		
		if(fieldid!='')
		{
			document.getElementById(fieldid).value=fieldvalue;
			document.getElementById(fieldid).focus();
		}
		else if(fieldname!='')
		{
			fieldname.value=fieldvalue;
			fieldname.focus();
		}
	}
}

//	+-----------------------------------+
//	| focus on the current selectfield	|
//	+-----------------------------------+
function selectfocus(fieldname,removeflag)
{
	if(removeflag==0)
	{
		fieldname.style.backgroundColor='#E4ECF5';
		fieldname.style.border='1px solid #7EC1FF';
		fieldname.focus();
	}
	else if(removeflag==1)
	{
		fieldname.style.backgroundColor='';
		fieldname.style.border='';
	}
}

//	+-----------------------------------------------------------+
//	| remove focus on the rest selectfield from array of field	|
//	+-----------------------------------------------------------+
function selectonefocus4marr(fieldnamearr,indexval)
{
	//alert(fieldnamearr.length+'=='+indexval);
	for (i=0; i<fieldnamearr.length; i++)
    {
		if(indexval==i)
		{
			fieldnamearr[i].style.backgroundColor='#E4ECF5';
			fieldnamearr[i].style.border='1px solid #7EC1FF';
			fieldnamearr[i].focus();
		}
		else
		{
			fieldnamearr[i].style.backgroundColor='';
			fieldnamearr[i].style.border='';
		}
	}
}

//	+-----------------------------------------------------------------------+
//	| check for currency field	[it will check for the +ve decimal numbers]	|
//	+-----------------------------------------------------------------------+
function validcurrency(fieldvalue)
{
	if(fieldvalue<1)
		return false;
	
	var pattern=/^[0-9]*(\.)?[0-9]+$/;
	return pattern.test(fieldvalue);
}

//	+-------------------------------------------------------------------------------+
//	| check for Year field	[it will check for yyyy format in 18th-20th century]	|
//	+-------------------------------------------------------------------------------+
function isValidYear(fieldvalue) 
{
	var pattern =  /(18|19|20)[0-9]{2}$/ ;
	return pattern.test(fieldvalue);
}

//	+---------------------------------------------------------------+
//	| check for date field	[it will check for yyyy-mm-dd format]	|
//	+---------------------------------------------------------------+
function isValidDate(fieldvalue) 
{
	//var pattern = /(?:19|20\d{2})\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])/;
	var pattern = /(?:19[0-9][0-9]|20[0-9][0-9])\-(?:0[1-9]|1[0-2])\-(?:0[1-9]|[12][0-9]|3[01])/;
	return pattern.test(fieldvalue);
}

//	+---------------------------------------------------------------+
//	| check for file field	[it will check for file format]			|
//	+---------------------------------------------------------------+
function isvalidfileformat(filename)
{
	var filelength=filename.length;
	var dotpos = filename.lastIndexOf(".")
	ext = filename.substr(dotpos+1);
	ext=ext.toLowerCase();
	//alert(ext);
	if(ext=='jpg' || ext=='jpeg' || ext=='gif' || ext=='png')
		return true;
	else
		return false;
}

//	+-----------------------------------+
//	| Display Google Map by address		|
//	+-----------------------------------+
function DisplayMapInDetail(addr)
{
	var gMap;
	var gMarker;
	var gGeocoder = new GClientGeocoder();
	var gAddress = addr;
	var map = document.getElementById("DetailIdforMap");

	var baseIcon = new GIcon();
	baseIcon.image = "../images/map/MapMarker-Blue.png";
	baseIcon.shadow = "../images/map/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);
	markerOptions = { icon:baseIcon };

	if (GBrowserIsCompatible()) 
	{
		gMap = new GMap(map);
		gMap.addControl(new GSmallMapControl());

		gGeocoder.getLatLng(
			gAddress,
			function(point) 
			{
				if (point == null)
				{
					map.style.display = 'none';
					return;
				}
				try
				{
					//var gMapInfo = document.getElementById('gMapInfo');
					var opts = {maxHeight: 10};
					gMap.setCenter(point, 13);
					gMap.addOverlay(new GMarker(point, markerOptions));							
				}
				catch(e){}
			}
		);		
	}
}
/*	ONLY FOR GOOGLE MAP	*/

//	+-----------------------------------+
//	| Delete confirmation by name or id	|
//	+-----------------------------------+
function DeleteConfirm(FormNameVal,IdStatus)
{
	//alert(FormNameVal);
	if(confirm("Do you really want to delete ?"))
	{
		if(IdStatus=='')
			document.FormNameVal.submit();
		else
			document.getElementById(FormNameVal).submit();
	}
}

//	+-----------------------------------------------------------------------------------+
//	|	display textbox in place of select option if list does not contain user value	|
//	+-----------------------------------------------------------------------------------+
function opentextboxinreplaceofselect(id,textname,classname,size,type,textid, textvalue)
{
	document.getElementById(id).innerHTML='';
	document.getElementById(id).innerHTML='<input id="'+textid+'" type="'+type+'" class="'+classname+'" name="'+textname+'" size="'+size+'" value="'+textvalue+'">';
}

