Array.prototype.find = function(searchStr) {  
	var returnArray = false;  
	for (i=0; i<this.length; i++) {    
		if (typeof(searchStr) == 'function') {      
			if (searchStr.test(this[i])) {        
				if (!returnArray) { returnArray = [] }        
				returnArray.push(i);      
			}    
		} else 	{      
		
		if (this[i]===searchStr) {        
			if (!returnArray) { returnArray = [] }        
				returnArray.push(i);      
			}    
		}  
	}  
	return returnArray;
}

function openSearchablePopup(type,source,popuptype,myWidth,myHeight,path,form,returnField,value_fieldname,value_fieldtype,caption_fieldname,caption_fieldtype,sendval,val)
{		
	
	a=eval("document.forms[form]."+returnField);

	fieldval=a.value;
	
	if (sendval && !val)
	{
		val=fieldval;
	}
	else
	{
		val='';
	}	
	if (!myWidth) myWidth=451;
	if (!myWidth) myWidth=451;
	if (!caption_fieldname) caption_fieldname='';
	if (!caption_fieldtype) caption_fieldtype='';
	additionalURLScript="";
	multi=0;
	if (popuptype == 'record_multi')
	{
		popuptype="record";
		multi=1;
	}
	
	dependency_field=eval("document.forms[form]."+value_fieldname+"_dependency_field");
	dependency_group=eval("document.forms[form]."+value_fieldname+"_dependency_group");
	
	if (dependency_field && dependency_group) 
	{
		temp_dependency_field=eval("document.forms[form]."+dependency_field.value);
		
		if (temp_dependency_field)
		{
			dependency_field_value=temp_dependency_field.value;
		}
		else
		{
			dependency_field_value="";
		}
		additionalURLScript="&dg="+dependency_group.value+"&df="+dependency_field.value+"&dv="+dependency_field_value;
	}
	
	depend_on_refreshedfld=eval("document.forms[form]."+value_fieldname+"_depend_on_refreshedfld");
	
	if (depend_on_refreshedfld)
	{
		temp_depend_on_refreshedfld=eval("document.forms[form]."+depend_on_refreshedfld.value);
		
		if (temp_depend_on_refreshedfld)
		{
			depend_on_refreshedfld_value=temp_depend_on_refreshedfld.value;
		}
		else
		{
			depend_on_refreshedfld_value="";
		}
		
		if (additionalURLScript=='')
		{
			additionalURLScript="&drf="+depend_on_refreshedfld.value+"&drv="+depend_on_refreshedfld_value;
		}
		else
		{
			additionalURLScript=additionalURLScript+"&drf="+depend_on_refreshedfld.value+"&drv="+depend_on_refreshedfld_value;
		}
	}
	
	var myLeft = (screen.width-myWidth)/2;
	var myTop = (screen.height-myHeight)/2;
	window.open(path+'?type='+type+'&source='+source+'&popuptype='+popuptype+'&target_form='+form+'&returnField='+returnField+'&value_fieldname='+value_fieldname+'&caption_fieldname='+caption_fieldname+'&value_fieldtype='+value_fieldtype+'&caption_fieldtype='+caption_fieldtype+'&multi='+multi+'&fv='+fieldval+'&search_text='+val+additionalURLScript,'SearchPopup','menubar=0,status=1,toolbar=0,scrollbars=1,resizable=1,width='+myWidth+',height='+myHeight+',left='+myLeft+',top='+myTop);	
}

function calcDate(daydropdown,monthdropdown,yeardropdown,txtdate,errormsg){
   	var daydropdown_field=document.getElementById(daydropdown);
	var monthdropdown_field=document.getElementById(monthdropdown);
	var yeardropdown_field=document.getElementById(yeardropdown);
	var txtdate_field=document.getElementById(txtdate);
	
	//if all 3 dropdowns aren't empty
	if (daydropdown_field.selectedIndex != 0 && monthdropdown_field.selectedIndex != 0 && yeardropdown_field.selectedIndex != 0){
		/*checks for a valid date*/
		if (!(validateDate(daydropdown_field,monthdropdown_field,yeardropdown_field))){
			txtdate_field.value = "";
			alert(errormsg);
			daydropdown_field.selectedIndex=0;
		}
		else{
			//if all 3 dropdowns aren't "- -" then populate txtdate textbox
			txtdate_field.value = daydropdown_field.value + "/" + monthdropdown_field.value + "/" + yeardropdown_field.value;
		}
	}
	else{
		txtdate_field.value = "";
	}
}		

function validateDate(dayElement, monthElement, yearElement){

   var dayValue=parseInt(dayElement.options[dayElement.selectedIndex].value);
   var monthValue=parseInt(monthElement.options[monthElement.selectedIndex].value);
   var yearValue=parseInt(yearElement.options[yearElement.selectedIndex].value);
	
   if (dayValue != 0 && monthValue != 0 && yearValue != 0)
   {	
   		//check correct number of day for given month/year
		if(monthValue==2){
			//February
			if (yearValue == Math.round(yearValue / 4) * 4){
				//leap year
				if (dayValue > 29){
					return false;
				}
			}
			else{ //non-leap year
				if (dayValue > 28){
					return false;
				}
			}
		}
		else if(monthValue==4){//April
			if (dayValue > 30){
				return false;
			}	
		}
		else if(monthValue==6){//June
			if (dayValue > 30){
				return false;
			}	
		}
		else if(monthValue==9){//September
			if (dayValue > 30){
				return false;
			}	
		}
		else if(monthValue==11){//November
			if (dayValue > 30)
				return false;
			}
		else {
			//date is valid
			return true;
		}
		
		return true;
	}
}	

function getActualValue(val,field_name,field_id){
	startIndex = val.lastIndexOf('(');
	endIndex = val.lastIndexOf(')');
	if (startIndex != -1 && endIndex != -1)
	{
		var actualvalue = val.substring(startIndex+1,endIndex);
		var displayvalue = val.substring(0,startIndex-1);
		displayvalue=encodeURI(displayvalue);
		displayvalue = displayvalue.replace(/&/gi,'%26');
		params= '?actualvalue='+actualvalue+'&displayvalue='+displayvalue+'&field_name='+field_name+'&field_id='+field_id;
		easyAjax('/console/shared/utilities/ec_edit_record/ajax/check_autosuggest.cfm'+params,'ajaxtestForm');
	}
	else if (val != '') 
	{
		document.getElementById(field_name).value = ""
		document.getElementById(field_name+'_display').value = ""
		document.getElementById(field_name).focus();
	}
	else if (val == '')
	{
		document.getElementById(field_name).value = ""
		document.getElementById(field_name+'_display').value = ""
		document.getElementById(field_name).focus();
	}
}

function popUp(URL) {
	id = "galleryPopupWindow"
	iMyWidth = (window.screen.width/2) - (450 + 10);
	iMyHeight = (window.screen.height/2) - (300 + 50);

	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=900,height=600,left = "+iMyWidth+",top = "+iMyHeight+",screenX=" + iMyWidth + ",screenY=" + iMyHeight + "');");
}


function loadEasyDoc(type,directory,document,val,console_folder){
	window.open ('/'+console_folder+'/shared/utilities/ec_edit_record/document_loader.cfm?page=downloaddocument&filename='+document+'&folder='+directory,'mywindow','status=1,toolbar=0,width=10,height=10'); 
}

