/*

String functions start

*/

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}


function removeNL(s) {
  /*
  ** Remove NewLine, CarriageReturn and Tab characters from a String
  **   s  string to be processed
  ** returns new string
  */
  r = "";
  for (i=0; i < s.length; i++) {
    if (s.charAt(i) != '\n' &&
        s.charAt(i) != '\r' &&
        s.charAt(i) != '\t') {
      r += s.charAt(i);
      }
    }
  return r;
  }



/*

string function End
*/
function CheckBoxList_Required(src, args)
{
     
     var ctl = document.getElementById(src.getAttribute("ctl"));
     var cnt = parseInt(src.getAttribute("cnt"))
     var inps = ctl.getElementsByTagName("INPUT");
     var inp, i, cnt1;
     
     
     cnt1=0;
     for (i = 0; i < inps.length; i++)
     {
        var inp = inps[i];
        if (inp.getAttribute("TYPE").toUpperCase() == "CHECKBOX")
        {
            if (inp.checked == true)  cnt1++;
            
        }
     }
     
        if (cnt == cnt1)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
        
}

function RadioButtonList_Required(src, args)
{
     
     var ctl = document.getElementById(src.getAttribute("ctl"));
     var inps = ctl.getElementsByTagName("INPUT");
     var inp, i, cnt1;
     
     cnt1=0;
     for (i = 0; i < inps.length; i++)
     {
        var inp = inps[i];
        if (inp.getAttribute("TYPE").toUpperCase() == "RADIO")
        {
            if (inp.checked == true)  cnt1++;
            
        }
     }
     
        if (cnt1 > 0)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
        
}

function DateControl_Required(src, args)
{
    var dt='';
    dt = src.getAttribute('ControlToValidate');
    if ((dt != null) || (dt == '' ) || (dt == undefined))
    {
        dt = src.getAttribute("dt");
    }
    //in case date id is not supplied, means it shud not compare date values
        if ((dt != null) || (dt == '' ))
        {
            var dt = DTC_GetValue(dt);
            if (dt == null) dt = '';
            if (dt == '') 
            {
                args.IsValid = false;
                return;
            }
        }
}

 function SimpleDate_Required(clt, args)
{
    var dy, mn, yr;
    dy = document.getElementById(clt.getAttribute('dy'));
    mn = document.getElementById(clt.getAttribute('mn'));
    yr = document.getElementById(clt.getAttribute('yr'));
    if ((dy.selectedIndex == 0) || (mn.selectedIndex == 0) || (yr.selectedIndex == 0))
    {
        args.IsValid = false;
    }
    
}


//Script for default Action of Enter Button
function GetControlsValue(ctl)
	{	
	
		if (ctl.nodeName == "SELECT")
		{
			return ctl.value;
		}
		else
		if (ctl.nodeName == "INPUT")
		{
			if (ctl.type == "checkbox")
				return ctl.checked;
			else
				return ctl.value;
		}
		else
		if (ctl.nodeName == "TEXTAREA")
		{
			return ctl.innerText;
		}
		
	}
	
	function myparseFloat(val)
	{
		
		if (val==null) return 0;
		if (val=='') return 0;
		return parseFloat(val);
		
	}
function testEnterKey(btn) 
{    
	if (event.keyCode == 13) 
	{        
		event.cancelBubble = true;
		event.returnValue = false;
		btn.click(); 
	}
}

var PopUpWin='';
function PopUp(url,title,width,height)
{
	if (!PopUpWin.closed && PopUpWin.location)
	{
		PopUpWin.location.href = url;
	} else {					
		var left=(screen.width-width)/2;
		var top=(screen.height-height)/2;
		//PopUpWin = window.open(url,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height+',left='+left+',top='+top);
		PopUpWin = window.open(url,null,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height+',left='+left+',top='+top);
	}
	
	if (window.focus) {PopUpWin.focus()}
	return false;
}


/* for multiAddress profile and person edit*/

	
	function LoadStates(lst)
	{
		LoadStatesOFCountry(lst,-1)
	}
	
	function LoadStatesOFCountry(selCountry,curIndex)
	{
		var selState, i,n, ids, txts, opt, txtState, selStateID;
		selState=document.getElementById(selCountry.getAttribute("stateList"));
		txtState=document.getElementById(selCountry.getAttribute("stateText"));
		
		if  (selState == null) return;
		if  (txtState == null)  return;
		
		//if  (selState != null) selState.innerHTML ="";
		//if  (txtState != null)  txtState.value="";
		selState.innerHTML ="";
		txtState.value="";
		
		opt= selCountry.options[selCountry.selectedIndex];
			
		if (curIndex >=0)
		{
			
			selStateID = document.getElementById(selState.getAttribute("other")).value.split("~")[curIndex];
			
			//alert(curIndex + '--' + selStateID );
			/*if (isNaN(parseInt(selStateID))) 
			{
				selState.style.display="none";
				txtState.style.display="block";	
				return;
			}*/
		}
		
		if ((opt.getAttribute("stateText") == undefined) || (opt.getAttribute("stateId") == undefined))
		{
			selState.style.display="none";
			txtState.style.display="block";	
			//txtState.value="";
			//address_onchange(txtState);
			
			//setListValue(selState,"",0);
			return;
		}
		
		if ((opt.getAttribute("stateText") == "") || (opt.getAttribute("stateId") == ""))
		{
			selState.style.display="none";
			txtState.style.display="block";	
			//txtState.value="";
			//address_onchange(txtState);
			
			//setListValue(selState,"",0);
			return;
		}
		
		txtState.style.display="none";
		selState.style.display="block";	

		
		txts= opt.getAttribute("stateText").split("~");
		ids= opt.getAttribute("stateId").split("~");
		//var res= Address.getStatesList(selCountry.value);
		//fillListByNameID(res,selState,selStateID);
		
		
		n= txts.length;
		if (n> ids.length)
		{
			n= ids.length;
		}
		curIndex=0
		
			
		for (i=0;i<n;i++)
		{		
			
			opt= document.createElement("OPTION");
			
			opt.text = txts[i];
			opt.value = ids[i];
			if (selStateID == ids[i]) 
			{
				curIndex= i;
				//opt.selected = true;
				//selState.selectedIndex=i;
			}
			
			//some browsers does not suppport options.add such as safari
			if (selState.options.add) 
			{
				selState.options.add(opt)
			}
			else
			{
				selState.options[selState.options.length] = opt
			}
			
			if (selStateID == ids[i])  opt.selected = true;
			//selState.insertBefore(opt);
			
		}
		//alert(curIndex);
			//setTimeout('try1();',50);
			
		//while (selState.readyState!="complete" && selState.readyState!="loaded" )
		//{
		//	
		//}
		//selState.selectedIndex = curIndex;
		//alert(selState.selectedIndex);
		
		
	}
	
	function fillListByNameID(res, lst, selected)
	{
		lst.innerHTML ="";
		if  (res.value.length== 0) 
		{
			return false;
		}
		for (i=0; i<res.value.length;i++)
			{
				var opt = document.createElement('OPTION');
				opt.text = res.value[i].name;
				opt.value = res.value[i].id;
				opt.setAttribute("des",res.value[i].details);
				lst.options.add(opt);
				if (selected == res.value[i].id)  opt.selected = true;
			}
		return true;
	}	
	


	function try1()
	{
		//alert("timeout");
	}
	
	function Address_SetDisable(src, val)
	{
	     var ctls_src= src.split("~");
	     for (i=0;i<ctls_src.length;i++)
		{
		    if (i < ctls_src.length)
		    {
		        ctl = document.getElementById(ctls_src[i]);
		    }
		    if (ctl != null) 
			{
			    ctl.disabled = val;
			}
		}
	}
	
	//src and dst are ctls of respective contols:
	//ctls is a csv of controls client ids
	function Address_Copy(src,dst)
	{
	    var ctl, ctl_src;
	    var ctls_dst= dst.split("~");
	    var ctls_src= src.split("~");
		
		for (i=0;i<ctls_dst.length;i++)
		{
		    if (i < ctls_dst.length)
		    {
		        ctl = document.getElementById(ctls_dst[i]);
		    }
            if (i < ctls_src.length)
		    {
		        ctl_src = document.getElementById(ctls_src[i]);
		    }
		        
			if ((ctl != null) && (ctl_src != null))
			{
		        val="";
		        val = ctl_src.value;
			    if (ctl.nodeName == "INPUT")
			    {
				    setTextValue(ctl,val);
			    }
			    else if (ctl.nodeName == "SELECT")
			    {
				    setListValue(ctl,val,0);
			    }
			}
		}
		
	}
	function setAddress(tab)
	{
		var ctls, i, ptd;
		ctls = tab.getElementsByTagName("INPUT");
	    	
		for (i=0; i<ctls.length; i++)
		{
			if (ctls[i].checked == true)
			{
				setAddress1(ctls[i],(i + 1), tab)
			}
		}
	}
	
	function setAddress1(chk,ind, td)
	{
		var  ctls, ctl, val, other, index, hdn;
		
		
		index= ind;
		
		ctls= td.getAttribute("ctls").split("~");
		
		if (chk.checked != true ) return;
		
		for (i=0;i<ctls.length;i++)
		{
			ctl = document.getElementById(ctls[i]);
			
			if (ctl != null)
			{
				
				hdn= document.getElementById(ctl.getAttribute("other"));
				other = hdn.value.split("~");
				val="";
				if (other.length > index) 
				{
					val= other[index];
				}
				//alert(other + '(' + index + ')' + '/' + val);
				
				if (ctl.nodeName == "INPUT")
				{
					//ctl.value= val;
					setTextValue(ctl,val);
				}
				else if (ctl.nodeName == "SELECT")
				{
					/*if (ctl.options.length > 0) 
					{ 
						ctl.value= val;
						alert(val);
					}*/
					setListValue(ctl,val,index);
				
				}
			}
		}
		
	}
	
	function setTextValue(ctl, val)
	{
		//alert(ctl.id + '=' + ctl.value + '/' + val)
		ctl.value = val.toString();
		
		//alert(ctl.value + '/' + val)
	}
	
	function setListValue(ctl,val, curIndex)
	{
		var i,j, ctl1, val1;
		val1 =val;
		ctl.selectedIndex = 0;
		
		if (isNaN(parseInt(val1)))
		{ 
			
			if (ctl.getAttribute("ctext") == undefined) return;
			ctl1 = document.getElementById(ctl.getAttribute("ctext"));
			ctl1.value = val;
			
			
		}
		else
		{
					//<str is not a number> 
			
			for (j=0; j<ctl.options.length; j++)
			{
				if (ctl.options[j].value == val)
				{
					ctl.options[j].selected=true;
					//ctl.selectedIndex = j;
					break;
					//j= ctl.options.length;
				}
			}
			
			if (ctl.id.indexOf("Country")>=0 ) 
			{
				
				LoadStatesOFCountry(ctl,curIndex);
				
				//alert("cc");
			}
		
			
		}
	}
	
	function getListValue(ctl)
	{
		var i,j, ctl1, ret;
		if (ctl.options.length == 0 )
		{
			
			if (ctl.ctext == undefined) return;
			ctl1 = document.getElementById(ctl.ctext);
			
			return ctl1.value;
		} 
		else
		{
			if (ctl.selectedIndex >=0) 	
			{
				ret= ctl.options[ctl.selectedIndex].value;
				return ret;
			}
			return "";
		}
		
	}
	
	function getIndexFromName(name, id)
	{
		var str, str1,i;
		str= id;
		if (name == undefined) return;
		str1 = name.split(":");
		//str = str.replace("_","");
		
		for (i=0;i<str1.length;i++)
		{
			str = str.replace(str1[i] + '_',"");
		} 
		i= (parseInt(str) + 1);
		return  i;
	}
	
	function getSelectedRadio(tbl)
	{
		var ctls, i;
		ctls= tbl.getElementsByTagName("INPUT");
		for (i=0; i < ctls.length; i++)
		{
			if (ctls[i].type =="radio")
			{
				if (ctls[i].checked== true) return ctls[i];
			}
			
		}
	}
	
	function address_onchange(ctl)
	{
		var hdn, val, chk, curIndex;
		//ctl = window.event.srcElement;
		hdn = document.getElementById(ctl.getAttribute("other"));
		val = hdn.value.split("~");
		if (ctl.atype != 'none')
			{
				chk = getSelectedRadio(document.getElementById(ctl.getAttribute("atype")));
				curIndex = getIndexFromName(chk.name, chk.id);
			}
		else
			{
				curIndex=1;
			}
		if (ctl.nodeName == "INPUT")
		{
			val[curIndex] = ctl.value;
		}
		else
		{
			val[curIndex] = getListValue(ctl);
			if (ctl.id.indexOf("Country")>=0 ) 
			{
			    var txtState;
			    txtState=document.getElementById(ctl.getAttribute("stateText"));
			    address_onchange(txtState);
		
			}
		}
		hdn.value= val.join("~");
		
		//alert(hdn.value);	
	}
	

	
	function CRM_ParentCompany_New(radid)
	{
		var rad, trOn, trOff, isNew, vlds;
		rad= document.getElementById(radid);
		trOn= document.getElementById(rad.getAttribute("onRow"));
		trOff= document.getElementById(rad.getAttribute("offRow"));
		isNew = (rad.getAttribute("isNew")=="1");
		vlds = rad.getAttribute("vlds");
		if (rad.checked==true)
		{
			trOn.style.display = "block";
			trOff.style.display = "none";
			CRM_ParentCompany_SetValidators(vlds,isNew);
		}
		else
		{
			trOff.style.display = "block";
			trOn.style.display = "none";
			CRM_ParentCompany_SetValidators(vlds,(!isNew));
		}
		/*inp= window.event.srcElement;
		CRM_ParentCompany_New2(inp.id, true);*/
	}
	
	function CRM_ParentCompany_SetValidators(csv, enable)
	{
		var vlds, i, vld;
		if (csv ==null) return;
		
		vlds= csv.split(",");
		
		for (i=0;i<=vlds.length;i++)
		{
			vld= document.getElementById(vlds[i]);
			
			if (vld != null)
			{
				//vld.enabled= enable;
				ValidatorEnable(vld,enable);
				
			}
			
		}
	}
	
	
	
	    function CheckBox_Clicked()
			{
				var hdn 
				hdn= document.getElementById(window.event.srcElement.hdn);
				hdn.value = hdn.value.replace(window.event.srcElement.getAttribute('code') + ',' ,'');
				if (window.event.srcElement.checked)
				{
					hdn.value = hdn.value + window.event.srcElement.getAttribute('code') + ',' ;
				}
			}
	
	    function TextBox_Filled(ctlid)
			{
				var hdn, ctl
				ctl = document.getElementById(ctlid); 
				hdn= document.getElementById(ctl.getAttribute('hdn'));
				hdn.value = hdn.value.replace(ctl.getAttribute('code') + ',' ,'');
				if (ctl.value == 'undefined')  ctl.value = '';
				if (ctl.value != '')
				{
					hdn.value = hdn.value + ctl.getAttribute('code') + ',' ;
				}

			}
	
			
			function ToggleDisplay(imgid)
			{
				var tr1 , img, wdt, hdn;
				
				//img = window.event.srcElement;
				img =document.getElementById(imgid);
				
				/*
				if (img.nodeName == "A")
				{
					img= img.parentElement.parentElement.cells(1).children(0);
				}*/
				
				tr1 = document.getElementById(img.getAttribute("dst"));
				hdn= document.getElementById(tr1.getAttribute("state"));
				//tr1= img.parentElement.parentElement.nextSibling;
				
				wdt= tr1.clientWidth;
				
				if (hdn.value=="1" )
				{
					hdn.value= "0";
					tr1.style.display="none";
				}
				else
				{
					hdn.value= "1";
					tr1.style.display="block";
				}			
			}	
		
		
		function GetImageGUID(PortalURL, cbf)
		{
			if (PortalURL == undefined) PortalURL ='';
			////var imgID = window.showModalDialog("Standard/Images/ImageSelector.aspx" + PortalURL, "ImageSelectorInterior.aspx" + PortalURL ,"dialogHeight: 515px; dialogWidth: 730px; help: No; resizable: No; status: No;");
			//var imgID = window.showModalDialog(PortalURL, "ImageSelectorInterior.aspx" + PortalURL ,"dialogHeight: 515px; dialogWidth: 730px; help: No; resizable: No; status: No;");
			var imgID = RadWindow_Show(PortalURL, "515px", "730px", "Image Manager",null,cbf,null)
			return imgID;
		}	
		function GetImageGUIDURL(imgID)
		{
			if (imgID != undefined)
			{
				return "/Images/RenderImage.aspx?ID="+imgID;
			}
			else
			{
				return "";
			}
		}
		function GetIconGUIDURL(imgID)
		{
			if (imgID != undefined)
			{
				return "/Images/ShowThumbNail.aspx?ID="+imgID;
			}
			else
			{
				return "";
			}
		}
		
		
	function BitWiseSubtract(val1, val2)
	{
		return val1 - (val1 & val2);
	}
	function BitWiseAdd(val1, val2)
	{
		return val1 | val2;
	}
	
	
	
	
//For rad window ***********************************************

var myradWindow;
		function RadWindow_Show(url,height,width, caption, argument, cbf, param)
		{
				myradWindow= RadWindow_Show1(url,height,width, caption, argument, cbf, param)
				//wnd1= RadWindow_Show1("",600,600, "try", null, aa, null);
			
		}
		
		//with 5.5.0
		function RadWindow_Show2(url,height,width, caption, argument, cbf, param)
		{
			var o2h ;
			o2h= new RadWindowInfo()
			o2h.Url=url; 
			o2h.Width=width; 
			o2h.Height=height; 
			o2h.Caption=caption; 
			o2h.IsVisible= true; 
			o2h.Argument=argument; 
			o2h.CallbackFunc= cbf; 
			o2h.OnLoadFunc=null; 
			o2h.Param=param; 
			o2h.Resizable= true; 
			o2h.Movable= true; 
			o2h.UseClassicDialogs=true; 
			o2h.BlankIFrameLocation=null; 
			
			var wnd; 
			
			o2h.l2h= false; 
			if ( false != o2h.l2h&&!o2h.UseClassicDialogs)
			{
				if (!this.i2h){ this.i2h=new Array(); }
				wnd=this.i2h[RADCOMMAND_SET_IMAGE_PROPERTIES]; 
				if (wnd&&wnd.ShowWindow){wnd.ShowWindow( true); }
				else {
						o2h.CloseHide= true; 
						wnd=GetRadWindowManager().ShowModalWindow(o2h); 
						wnd.OnClientClosing=I2h; this.i2h[RADCOMMAND_SET_IMAGE_PROPERTIES]=wnd; 
					}
			}
			else {wnd=GetRadWindowManager().ShowModalWindow(o2h); }
			
			return wnd; 
		} 
		//with 6.6.1 (latest)
		function RadWindow_Show1(url,height,width, caption, argument, cbf, param)
		{
		    //var Idx=new RadWindowInfo(); Idx.Url=url; Idx.Width=width; Idx.Height=height; Idx.Caption=(Odx?Odx: ""); Idx.IsVisible= true; Idx.Argument=i23; Idx.CallbackFunc= function (i2l,I2l){if (I2l.ldx && I2l.ldx.select){I2l.ldx.select(); }I2l.Oc4=null; I2l.CanAddCustomColors=null; I2l.StripAbsoluteImagesPaths=null; if (I2l.l2i){I2l.l2i(i2l,I2l); }if (null!=i2l)I2l.l10.l9p(RadEditorNamespace.o9m,null); I2l.l10.SetFocus(); } ; Idx.OnLoadFunc=null; Idx.Param=odx; Idx.Resizable= true; Idx.Movable= true; Idx.UseClassicDialogs=i3o.UseClassicDialogs; Idx.BlankIFrameLocation=i3o.BlankIFrameLocation; wnd=GetEditorRadWindowManager().ShowModalWindow(Idx); return wnd;
		    var Idx=new RadWindowInfo(); 
		    Idx.Url=url; 
		    Idx.Width=width; 
		    Idx.Height=height; 
		    Idx.Caption=caption;
		    Idx.IsVisible= true; 
		    Idx.Argument=argument; 
		    Idx.CallbackFunc= cbf;
		    Idx.OnLoadFunc=null; 
		    Idx.Param=param; 
		    Idx.Resizable= true; 
		    Idx.Movable= true; 
		    Idx.UseClassicDialogs=false; 
		    Idx.scrolling = true;
		    //Idx.BlankIFrameLocation=i3o.BlankIFrameLocation; 
		    wnd=GetEditorRadWindowManager().ShowModalWindow(Idx); 
		    wnd.Window.frameElement.scrolling="yes";
		    wnd.Window.scrolling="yes"
		    wnd.scrolling="yes";
		    
    	    return wnd;
		}
		
		function radWindow_Close()
		{
			var id;
			id='';
			CloseDlg(id);
            return;
		}
		
		function Common_GetRadWindow()
        {
            var oWindow = null;
            if (window.radWindow) oWindow = window.radWindow;
            else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
            return oWindow;
        } 
		function radWindow_Close1(id)
		{
			CloseDlg(id);
			//alert(id);
			//var oWindow = Common_GetRadWindow();    
		    //oWindow.Close(id);
			return;
		}
// rad window *********************************************************************


// for popup window
	
		function popup_GetReturnItem(val)
		{
			
			var vars= new Array(2);
			var vals, txts, all;
			
			//if (val == null) return;
			if (val ==null) val="1~<@>try~";
			all = val.split("<@>");
			if (all.length>=0) vals = all[0].split("~");
			if (all.length>=1) txts = all[1].split("~");
			
			
			//vars[0] = vals
			//vars[1] = txts
			
			vars[0] = vals
			vars[1] = txts
			
			
			return vars;
			
		}
	
		function popup_FillListByReturn(lst,vars)
		{
			var vals,txts,i,opt;
			
			
			vals = vars[0]
			txts = vars[1]
	
			lst.innerHTML ="";
			for (i=0;i<vals.length-1;i++)
			{
				
				opt= document.createElement("OPTION");
				if (txts.length>=i)  opt.text = txts[i];
				if (vals.length>=i)  opt.value = vals[i];
				
				lst.options.add(opt);
			}
			
		}
// popup window***************************************

//Start selectors
	function Filter_CustomFields_GetAnswers(ele)
	{
		
		var lst = document.getElementById(ele.getAttribute("listctl"));
		if (ele.value==null)  
		{	
			lst.innerHTML="";
			return;
		}
		if (ele.value == '0') 
		{	
			lst.innerHTML="";
			return;
		}
		var res = Filter_CustomField.GetAnswers(ele.getAttribute("pid"), ele.value);
		
		if (res.error !=null)
				{
					alert(res.error);
					return;
				}
		
		lst.disabled = !(fillListByNameID(res, lst, ""));
		
		
				
	}


function RegionSelector_GetStates(ele)
	{
		
		var lst = document.getElementById(ele.getAttribute("listctl"));
		if (ele.value==null)  
		{	
			lst.innerHTML="";
			return;
		}
		if (ele.value == '0') 
		{	
			lst.innerHTML="";
			return;
		}
		var res = RegionSelector.GetStates(ele.getAttribute("pid"), ele.value);
		
		if (res.error !=null)
				{
					alert(res.error);
					return;
				}
		
		lst.disabled = !(fillListByNameID(res, lst, ""));
		
		
				
	}
//Selectors


// shows/hides select lists - useful for overlapping dialog boxes or menus
function hideSelectLists(hide) {
    var visibility = "hidden"; 
    if (hide != true) {
        visibility = "visible";
    
    }
	for(var i = 0; i < document.forms.length; i++) {
		for(var e = 0; e < document.forms[i].length; e++){
			if(document.forms[i].elements[e].tagName == "SELECT") {
				document.forms[i].elements[e].style.visibility= visibility;
			}
		}
	}

}