var interactive123cf_loaded=1;

String.prototype.endsWith = function(str)
{return (this.match(str+"$")==str)}

function replaceAll(txt, repl, with_this) {
  return txt.replace(new RegExp(repl, 'g'),with_this);
}

function RefreshFrameHeight(jumptotop) {
	if (document.getElementById('thisisjsform')!=null)
	if (parent.socket != null) {
		//var mymsg=(document.body.clientHeight || document.body.offsetHeight || document.body.scrollHeight) + "||"+ jumptotop;
		var mymsg=Math.max(document.body.clientHeight,document.body.offsetHeight,document.body.scrollHeight) + "||"+ jumptotop;
		parent.socket.postMessage(mymsg);		
		}
	}
	
function disableForm(formid,list_cid) {	
		if (document.all || document.getElementById) {
			var theform=document.getElementById(formid);
			for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
				if (true) {
					formElement.disabled = true;
				}
			}
			if(list_cid!="")
					{var array_list=list_cid.split("||");						
						//enable this elements:
						for(var i=0;i<=array_list.length;i++)
						if(document.getElementById(array_list[i]))
							{
							document.getElementById(array_list[i]).disabled = false;//email
							}
					}
		}
	}
	
	
function setFocusOnFirstTextField(optionalFid, optionalCid){
 optionalFid = (typeof optionalFid == "undefined")?'0':optionalFid; 
 optionalCid = (typeof optionalCid == "undefined")?'0':optionalCid; 
 if ((optionalFid!=0)&&(optionalCid!=0)) {
	elem=document.getElementById('id123-control'+optionalCid);
	if (typeof elem=="undefined") elem=document.getElementById('id123-control'+optionalCid+'_0');
	if (typeof elem!="undefined") {
		elem.focus();
		return;
		}	
	}
	
 for(z=0;z<document.forms.length;z++){
  var form = document.forms[z];
  var elements = form.elements;
  for (var i=0;i<elements.length;i++){
    var element = elements[i];
    if((element.type == 'text' || element.type == 'textarea' || element.type == 'checkbox' || element.type == 'radio') &&
      !element.readOnly &&
      !element.disabled){
	  if (element.value=='')
		element.focus();
	  return;	  
    }
  }  
 }
}	

function InputRules(inputfieldid) {    
	var thenewhiddenvalues=document.getElementById('hiddenfields').value;	
	if( typeof( formrules ) == 'undefined' ) { return 0; }
	var rulescnt=formrules.length;

	for (var j=0;j<rulescnt;j++) {		
		//FCO - verificam daca avem treaba in acest rule cu controlul pe care s-a dat click ..sa nu procesam degeaba
		if (inputfieldid!=undefined)  //daca idul fieldului nu face parte cumva din idul stang din rule=> acest rule nu e pentru noi
				if (formrules[j][0].indexOf(inputfieldid)==-1) continue;

		//~fco
		var leftm='id123-control'+formrules[j][0];	
	    var action=''; var notaction='none';
	    if (formrules[j][3]=='1') { action='none'; notaction=''; }
	    var rightm=formrules[j][2].toLowerCase();
		//alert('leftm'+formrules[j][0]);
		// verificare validitate regula
		isadvancedfield=0;
		if (document.getElementById(leftm)==null) 
			{//daca nu gaseste asa, poate e un field advanced ce are - in loc de _
			var pos_=formrules[j][0].indexOf('_');
			if (pos_>0)
				{
				var firstgroup=formrules[j][0].substr(0,pos_);
				var secondgroup=formrules[j][0].substr(pos_+1,formrules[j][0].length-pos_);
				leftm='id123-control'+firstgroup+'-'+secondgroup;
				if (document.getElementById(leftm)==null) continue; 
				}
				else 
				{
				 //daca nu gaseste, poate e un field advanced de tipul time sau date, ce nu are _, dar pe form componentele sunt cu _
				//alert('spec');
				if (document.getElementById(leftm+'-1')==null) 						
							 continue;
					else isadvancedfield=1;
	
				}
			}
		if (isadvancedfield==0) 
			var leftmval=document.getElementById(leftm).value.toLowerCase();
		else
			{//e adv field care trebuie concatenat
			leftmval='';
			for(i=1;i<4;i++) //nu are mai mult de 4
				if (document.getElementById(leftm+'-'+i)!=null) 
						leftmval+=document.getElementById(leftm+'-'+i).value.toLowerCase();
				else break;
			//alert(leftmval);
			}
		//alert(leftmval);
		var condtrue=0;
	    switch (formrules[j][1]) {
	    case "1":
		    if (leftm.indexOf('_')>0) {
			    if (document.getElementById(leftm).checked==true) condtrue=1;
			    }			  
			else 
			if (leftmval==rightm) condtrue=1; 		                 
			break;
	    case "2":
		    if (leftm.indexOf('_')>0) {
			    if (document.getElementById(leftm).checked!=true) condtrue=1;		
				}
			else	
	        if (leftmval!=rightm) condtrue=1; 		                 
	        break;		
	    case "3":
	        pos=leftmval.indexOf(rightm);
	        if (pos>=0) condtrue=1;
	        break;		
	    case "4":
	        pos=leftmval.indexOf(rightm);
	        if (!(pos>=0)) condtrue=1;
	        break;	
	    case "5":
	        pos=leftmval.indexOf(rightm);
	        if (pos==0) condtrue=1;
	        break;	
	    case "6":	    
	        if (leftmval.endsWith(rightm)) condtrue=1;
	        break;			
	    }				
		
		if (formrules[j][5]=="3") // notification rule
			{
			if (condtrue==1) {
				if (document.getElementById('special_autoresponder') != null)
					document.getElementById('special_autoresponder').value=formrules[j][4];
			
				if (document.getElementById('multiple_autoresponder') !=null) {
					var multipleautoresponder=document.getElementById('multiple_autoresponder').value;
					var current_autoresponder='-'+formrules[j][4]+'-';
					// add to Multiple Autoresponder list, only if not already there
					if (multipleautoresponder.indexOf(current_autoresponder)== -1) {
						multipleautoresponder+=current_autoresponder;
						document.getElementById('multiple_autoresponder').value=multipleautoresponder;						
						}						
					}
				}
			else {			    
				// delete from Multiple Autoresponder list
				if (document.getElementById('multiple_autoresponder') != null) {					
					var multipleautoresponder=document.getElementById('multiple_autoresponder').value;					
					document.getElementById('multiple_autoresponder').value=replaceAll(multipleautoresponder, '-'+formrules[j][4].toString()+'-', '');
					}				
				}
			continue;
			}
			
		if(formrules[j][4]!="id123-button-send")
			{
			var controlid="id123-control"+formrules[j][4];
			var controlidsplit=controlid.split("_"); var controlidunic=controlidsplit[0];
				
			var titleid="id123-title"+formrules[j][4];
			var titleidsplit=titleid.split("_"); titleid=titleidsplit[0];
			}
		else   // pt submit	
			{
			 var controlid=formrules[j][4];
			 var controlidsplit=formrules[j][4];
			 var titleid=formrules[j][4];
			}
		
		// verificare validitate regula
		//if (document.getElementById(controlid)==null) continue;
		
		// obsolete: var titleid="id123-title"+formrules[j][4];
		// obsolete: var titleidsplit=titleid.split("_"); titleid=titleidsplit[0];
		// now hide or show the parent TR(s)		
	    if (condtrue==1) {	   
		    // show or hide control
			var tr=document.getElementById(controlid);
			if (tr!=null) tr.parentNode.parentNode.style.display=action;
			// show or hide label
			var tr=document.getElementById(titleid);
			if (tr!=null) tr.parentNode.parentNode.style.display=action;	   								
			// show or hide separator			
			var trsep=document.getElementById('separator-'+controlidunic);
			if (trsep!=null) trsep.style.display=action;	   												
												
			thenewhiddenvalues=thenewhiddenvalues.replace(new RegExp(controlidunic+';',"g"),'') ;
			if (action=='none') thenewhiddenvalues=thenewhiddenvalues+controlidunic+';';				
	        }
	    else {	            		    
		    // show or hide control	
            var tr=document.getElementById(controlid);			
			if (tr!=null) tr.parentNode.parentNode.style.display=notaction;			
			// show or hide label
			var tr=document.getElementById(titleid);
			if (tr!=null) tr.parentNode.parentNode.style.display=notaction; 	
			// show or hide separator			
			var trsep=document.getElementById('separator-'+controlidunic);
			if (trsep!=null) trsep.style.display=notaction;				
			
			thenewhiddenvalues=thenewhiddenvalues.replace(new RegExp(controlidunic+';',"g"),'') ;
			if (notaction=='none') thenewhiddenvalues=thenewhiddenvalues+controlidunic+';';							
	        }
		document.getElementById('hiddenfields').value=thenewhiddenvalues;				
	    }	
	RefreshFrameHeight(0);
	}
	
function InputSetDefaultValue(elemid, defval, action, inputcolor_default, inputcolor_gray)	
	{
	elem=document.getElementById(elemid);
	if (elem==null) return;
	
	if ((action=='focus')&&(elem.value==defval)) {
		elem.value='';		
		}
	if ((action=='blur')&&(elem.value=='')) {
		elem.value=defval;		
		}
	if (elem.value==defval)
		elem.style.color=inputcolor_gray;		
	else elem.style.color=inputcolor_default;		
	}
