register("SYM.form");
register("SYM.form.field");
/*******************************************  present ****************************************************
**
**	CHECK IF THE FIELD IS PRESENT ANYWHERE 
**	IF NO FORM SPECIFIED TRY ALL, AND DOCUMENT.ALL
**
*******************************************************************************************************************/
SYM.form.field.present= function ( szFieldName , szFormName )
{
	return true;
}
SYM.form.defaultform = null;
SYM.form.defaultloose = false;
/*******************************************  validEmail  ****************************************************
**
**	CHECK IF THE EMAIL IS A VALID ONE
**
*******************************************************************************************************************/
SYM.form.validEmail = function(szemail)
{ 
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return szemail.match(re);
}
/*******************************************  serialize  ****************************************************
**
**	SERIALIZE A FORM TO USE IN AJAX POST OR YOU WANT TO STORE INSIDE A 
**	COOKIE. IF NO FORM I USED THEN USE [0]
**
*******************************************************************************************************************/
SYM.form.serialize = function ( szform )
{
	var szvalues = "";
	if (szform == null || szform == "" ){szform = 0}
	var elements = document.forms[szform].getElementsByTagName("INPUT");
	for (var i = 0  ; i < elements.length ; i++)
	{
		field = elements[i];	
		if (field.name != null && field.name.substring(0,1) != "_" && field.name.substring(0,1) != "%" )
		{
			if (field.type != "file" && field.type != "button" && field.type != "image"  && field.name != "")
			{
				if ( szvalues !=  ""){szvalues +="&"}			
				if (field.type == "radio" || field.type == "checkbox")
				{ 				
					var szvalue =  SYM.form.getItemValue( field.name , false , szform ) ;
					if( szvalue.length != 0)
					{
						 if (szvalue.substring(szvalue.length-2,szvalue.length) == "; ")
							szvalue = szvalue.substring(0,szvalue.length-2);
					}
 
					if (field.type == "checkbox")
					{
						values = szvalue.split("; ");
						for ( var ii = 0 ; ii < values.length ; ii++)
						{
							szvalues += (ii != 0?"&":"") +  field.name + "=" + encodeURIComponent (values[ii]);
						}
					}else
					{
						szvalues += field.name + "=" + encodeURIComponent (szvalue);
					}
					while (true) // move to next field
					{	
						tmpfield = elements[i];
						if (tmpfield.name != field.name ){ i--;break; }
						i++;
						if (i >= elements.length ) {break;}
					}	
				}else
				{
					szvalues += field.name + "=" + encodeURIComponent ( SYM.form.getItemValue( field , false , szform ) );
				}
			}
		}
	}

	var elements = document.forms[szform].getElementsByTagName("SELECT");
	for (i =0  ; i < elements.length; i++)
	{
		field = elements[i];
		if ( szvalues !=  ""){szvalues +="&"}
		var szvalue = SYM.form.getItemValue( field , false , szform );
		if (field.multiple  && szvalue.length != 0)
		{			
			 if (szvalue.substring(szvalue.length-2,szvalue.length) == "; ")
			szvalue = szvalue.substring(0,szvalue.length-2);
			values = szvalue.split("; ");
			for ( var ii = 0 ; ii < values.length ; ii++)
			{
				szvalues += (ii != 0?"&":"") +  field.name + "=" + encodeURIComponent (values[ii]);
			}
		}else
		{
			szvalues += field.name + "=" + encodeURIComponent ( SYM.form.getItemValue( field , false , szform ) );
		}
	}
 
	var elements = document.forms[szform].getElementsByTagName("TEXTAREA");
	for (i =0  ; i < elements.length; i++)
	{
		field = elements[i];		
		if ( szvalues !=  ""){szvalues +="&"}
		szvalues += field.name + "=" + encodeURIComponent( SYM.form.getItemValue( field , false , szform ) );
	}
	return szvalues; 
} 
/******************************************************************************************************************
**
**	LOADS A URL WITH JSON ITEMS
**	THEN APPLIES THE ENTRIES TO THE FORM SPECIFIED 
**
*******************************************************************************************************************/
SYM.form.loadForm = function()
{return true;}
/******************************************************************************************************************
**
**	UPDATES ONE FIELD FROM ONE FORM TO ANOTHER	
**
*******************************************************************************************************************/
SYM.form.updateField = function(  )
{}
SYM.form.appendItemValue =  function( active_field  , szValue , form  , loose , override  )
{
	var value = SYM.form.getItemValue( active_field  , szValue , form  , true , override  )
	
	if (value != null)
	{
		SYM.form.setItemValue( active_field  , value + szValue , form  , true, override  );
	}
}
/*****************************************  setItemValue *******************************************
**
**	SETS THE FIELD VALUE FROM A FIELD, IF NO FORM IS SPECIFIED THEN USE THE [0]
**	IF LOOSE THEN TRY TO SEE IF NO FORM, TEST DOCUMENT.ALL AND ALL 
**	FORMS PRESENT ON THE PAGE.
**	active_field (text of field object)
**	if override then it is the client calling ignore the symfieldtype attribute
**
*******************************************************************************************************************/
SYM.form.setItemValue = function( active_field  , szValue , form  , loose , override  )
{
	if (loose == null) {loose = SYM.form.defaultloose;} // use default value
	var sss = active_field;
	if (SYM.util.text.isString(active_field))
	{
		active_field= SYM.form.getField( active_field, loose , form );
	}
	if ( active_field == null)
	{return "";}
	
	if ( (active_field.length == null &&  active_field.getAttribute("symfieldtype") != null) && override == null)
	{  			
				try{			
			var obj = eval("SYM.client.ui.field." + active_field.getAttribute("symfieldtype") );			
			obj.set( active_field.name  ,   szValue );	
		}catch(asdg){}
		return;		
	}
	var szType = "";
	try
	{	
		if ( active_field.length != null )
		{
			if ( active_field.type != null )
			{
				szType = active_field.type.toString();	
			}else
			{
				szType = active_field[0].type.toString();			
			}
		}else
		{
			szType = active_field.type.toString();	
		}
	}catch(ee)
	{
		szType = active_field.type.toString();	
	}
	if( szType == "text" || szType == "textarea" || szType == "hidden" || szType == "password")
	{
		active_field.value = szValue;		
	}else if( szType == "select-one" )
	{
		for (  var select_counter = 0 ; select_counter < active_field.length ; select_counter++  )
		{
			if ( active_field[ select_counter ].value != "" )
			{
				if ( active_field[ select_counter ].value == szValue || active_field[ select_counter ].text == szValue) {
					active_field.selectedIndex = select_counter;
				}
			}else
			{
				if ( active_field[ select_counter ].text == szValue ) {
					active_field.selectedIndex = select_counter	;
				}
			}
		}
	}else if( szType == "select-multiple" )
	{
		for (  var multi_counter = 0 ; multi_counter < active_field.length ; multi_counter++  )
		{
			active_field[ multi_counter ].selected = false;			
		}
		var szMultiValues = szValue.split("; ");
		for ( multi_counter = 0 ; multi_counter < active_field.length ; multi_counter++  )
		{
			for ( var multi_value = 0 ; multi_value < szMultiValues.length ; multi_value++)
			{
				if ( active_field[ multi_counter ].value != "" )
				{
					if ( active_field[ multi_counter ].value == szMultiValues[ multi_value ] ) {
						active_field[ multi_counter ].selected = true;
					}		
				}else
				{
					if ( active_field[ multi_counter ].text == szMultiValues[ multi_value ] ) {
						active_field[ multi_counter ].selected = true;
					}
				}
			}
		}
	}else if( szType == "radio")
	{
		for (  var radio_counter = 0 ; radio_counter < active_field.length ; radio_counter++  )
		{	
			if ( active_field[ radio_counter ].value == szValue )
			{
				active_field[ radio_counter ].checked = true;
			}
		}	
	}else if( szType == "checkbox")
	{
		var subValues = szValue.split("; ");
		if ( active_field.length != null )
		{
			for (  var check_counter = 0 ; check_counter < active_field.length ; check_counter++  )
			{
				for (  var sub_check_counter = 0 ; sub_check_counter < subValues.length ; sub_check_counter++  )
				{
					if ( active_field[ check_counter ].value == subValues[ sub_check_counter ]  )
					{		
						active_field[ check_counter ].checked = true;
					}
				}
			}
		}else
		{
			for (  var sub_check_counter = 0 ; sub_check_counter < subValues.length ; sub_check_counter++  )
			{
				if ( active_field.value == subValues[ sub_check_counter ]  )
				{		
					active_field.checked = true;
				}
			}
		}
	}
}
/*****************************************  getFieldValue  *******************************************
**
**	GETS THE FIELD VALUE FROM A FIELD, IF NO FORM IS SPECIFIED THEN USE THE [0]
**	IF LOOSE THEN TRY TO SEE IF NO FORM, TEST DOCUMENT.ALL
**
*******************************************************************************************************************/
SYM.form.getItemValue = function( active_field, loose , form  , override)
{
	if (SYM.util.text.isString(active_field))
	{
		active_field= SYM.form.getField( active_field , loose , form );
	}
	if ( active_field == null)
	{return "";}
	// CHECK IF THIS IS A SPECIAL FIELD, THEN SEND TO ITS OWNER.
	if ( ((active_field.length == null && active_field.getAttribute("symfieldtype") != null) || active_field.length != null && active_field[0].getAttribute("symfieldtype") != null ) && override == null )
	{  	
			try{
				var obj = null;
				if (active_field.length == null) {
					obj = eval("SYM.client.ui.field." + active_field.getAttribute("symfieldtype"));
					valuein = obj.get( active_field.name );	
				}else
				{
					obj = eval("SYM.client.ui.field." + active_field[0].getAttribute("symfieldtype"));
					valuein = obj.get( active_field[0].name );	
				}				
				return valuein;
			}catch(asdg){}
			return "";	
		
	}
	var szType = "";
	try
	{	
		if ( active_field.length != null )
		{
			if ( active_field.type != null )
			{
				szType = active_field.type.toString();	
			}else
			{
				szType = active_field[0].type.toString();			
			}
		}else
		{
			szType = active_field.type.toString();	
		}
	}catch(ee)
	{
		szType = active_field.type.toString();	
	}
	if( szType == "text" || szType == "textarea" || szType == "hidden" || szType == "password" || szType == "file")
	{
		return active_field.value		
	}else if( szType == "select-one" )
	{
		if ( active_field.selectedIndex != -1 )
		{
			if ( active_field[active_field.selectedIndex].value != "" )
			{
				return active_field[active_field.selectedIndex].value;
			}else
			{
				return active_field[active_field.selectedIndex].text;
			}
		}else				
		{
			return "";
		}
	}else if( szType == "select-multiple" )
	{
		var szValue = "";
		for ( mv = 0 ; mv < active_field.length ; mv++ )
		{
			if ( active_field[ mv ].selected )
			{
				if ( active_field[ mv ].value != "" )
				{
					szValue += active_field[ mv ].value + "; "
				}else
				{
					szValue += active_field[ mv ].text + "; "
				}
			}
		}	
		return szValue;
	}else if( szType == "radio" )
	{	
		var szValue = "";
		for (rad = 0 ; rad < active_field.length ; rad++)
		{
			if ( active_field[ rad ].checked ) 
			{
				if ( active_field[ rad ].value != "" )
				{
					szValue = active_field[ rad ].value;
				}else
				{
					szValue = active_field[ rad ].text;
				}
			}
		}	
		return szValue;
	}else if( szType == "checkbox" )
	{
		var szValue = "";	
		if (  active_field.length == null )
		{
			if ( active_field.checked ) 
			{
				if ( active_field.value != "" )
				{
					szValue += active_field.value + "; "
				}else
				{
					szValue += active_field.text + "; "
				}
			}
		}else
		{
			for (check = 0 ; check < active_field.length ; check++)
			{
				if ( active_field[ check ].checked ) 
				{
					if ( active_field[ check ].value != "" )
					{
						szValue += active_field[ check ].value + "; "
					}else
					{
						szValue += active_field[ check ].text + "; "
					}
				}
			}
		}
		return szValue;
	}
	
}
/******************************************************************************************************************
**
**	GETS A FIELD FROM A FORM, IF NO FORM SPECIFIED THEN USE [0]
**	IF LOOSE THEN TRY TO SEE IF NO FORM, TEST DOCUMENT.ALL AND ALL 
**	FORMS PRESENT ON THE PAGE.
**
*******************************************************************************************************************/
SYM.form.getField = function (  szfieldname , loose , szform )
{
	if ( szform == null && SYM.form.defaultform != null ) {szform = SYM.form.defaultform; }
	var frm = null;
	if ( szform == null )
	{
		frm = document.forms[0];
	}else
	{
		frm = document.forms[szform];
	}	
	var field = null;
	if (frm == null || (loose!=null && loose))
	{
		field  = SYM.util.dom.getObject(szfieldname);
		if (field == null)
		{
			// now check if on another form.
		}
	}else if(frm != null)
	{
		field  = eval("frm['"+ szfieldname+ "']" )
		if (field == null && loose)
		{field  = SYM.util.dom.getObject(szfieldname);}
	}	
	
	return field;
}

