/**
 * amsForm - Class for creating a website form.
 *
 * CHANGE LOG:
 * - 2005-12-16 - Soung: Added form validating functions
 * - 2005-11-22 - Radek Zak: Class was created
 *
 * @package amsForm
 * @version 1.0
 * @author Radek Zak <rzak@automs.com>
 * @copyright 2005 Automated Marketing Solutions Inc.
 * @require ams
 */


/**
 * This class is dependent on ams framework.  It may not work properly without it.
 */
if( typeof( ams ) == 'undefined' )
{
	alert( "Error, ams framework not included!!!" );
}


/**
 * Constants for amsForm class.
 *
 * @access public
 * @return void
 */
function amsFormConstant()
{
}
amsFormConstant.CLASS_NAME = 'amsForm';
amsFormConstant.LOAD_DELAY = 0;
amsFormConstant.FIELD_TYPE_RAW_TEXT = 1;
amsFormConstant.FIELD_TYPE_SELECT = 2;
amsFormConstant.FIELD_TYPE_BUTTON = 3;
amsFormConstant.FIELD_TYPE_TEXT = 4;
amsFormConstant.FIELD_TYPE_TEXTAREA = 5;
amsFormConstant.FIELD_TYPE_CHECK = 6;
amsFormConstant.FIELD_TYPE_RADIO = 7;
amsFormConstant.FIELD_TYPE_HIDDEN = 8;
amsFormConstant.FIELD_TYPE_SUBMIT = 9;
amsFormConstant.FIELD_TYPE_CUSTOM_FIELD = 10;



amsFormConstant.getForm = amsFormConstant_getForm;

/**
 * Links popup element with popup identifier.
 *
 * @param int fid	form identifier
 * @access public
 * @return object	Returns popup object on success, or void on failure;
 */
function amsFormConstant_getForm( fid )
{
	fid = parseInt( fid );
	if ( document.getElementById( 'amsForm_' + fid ))
	{
		return document.getElementById( 'amsForm_' + fid );
	}
	else
	{
		ams.debug( 'warning: popup id ("' + fid + '") does not exist' );
	}
}
ams.amsFormConstant = amsFormConstant;
ams.debug( '( "amsFormConstant" ) - loaded' );




/**
 * Field information for amsForm class.
 *
 * @access public
 * @return void
 */
function amsFormField()
{
	this.fieldID = '';
	this.fieldTypeID = '';
	this.fieldName = '';
	this.fieldValue = '';
	this.fieldLabel = '';
	this.fieldDisplay = '';
	this.fieldJustification = '';
	this.fieldOrientation = '';
	this.fieldRequired = '';
}
ams.amsFormField = amsFormField;
ams.debug( '( "amsFormField" ) - loaded' );





/**
 * Constructor for amsForm class.
 *
 * @param int fid	popup identifier
 * @param int wpgid	website identifier
 * @access public
 * @return void
 */
function amsForm( fid , wpgid , urlact)
{
	if( typeof( fid ) == 'undefined' ) fid = 0;
	this.formID = fid;
	this.PageID = wpgid; //for redirect 
	this.URLAction = urlact;
	this.formBody = '';
	this.formFields = new Array();
	this.obj = this;
	this.objName = '';
	
	this.loaded = false;
}

amsForm.prototype.init = amsForm_init;								// initialize popup window
amsForm.prototype.load = amsForm_load;								// load popup frame



/**
 * Initializes the class.
 *
 * @access public
 * @return void
 */
function amsForm_init()
{
	this.load();
}



/**
 * Creates the html content of the popup window.
 *
 * @access public
 * @return void
 */
 

function createTarget(t){
window.open('', t, 'width=1024,height=850');
return true;
}



function amsForm_load()
{
	if( ! this.loaded )
	{
		var tmpName = amsFormConstant.CLASS_NAME;
		var theFormSubmit = '';
		var theForm = '';
		var theValidatePart = '';
		/* 		 ADDED to validate form data  	*/
		/* 		START  	*/
    
       theValidatePart += 'checkForm(\'' + tmpName + '_' + this.formID + '\'';
		  
		      for( var ixx = 0; ixx < this.formFields.length; ixx++ )
      		{
      		
      		var TypeofField = '';
      		var MaxLengthofField = '';
          theField = this.formFields[ixx];
		      switch( parseInt( theField.fieldTypeID ) ) {
						case amsFormConstant.FIELD_TYPE_TEXT:
             if (theField.fieldName == 'FirstName' || 
                 theField.fieldName == 'LastName' || 
                 theField.fieldName == 'CompanyName' ||
                 theField.fieldName == 'Title' ||
                 theField.fieldName == 'City' ||
                 theField.fieldName == 'State' ||
                 theField.fieldName == 'Country' ||
                 theField.fieldName == 'Addr1' ||
                 theField.fieldName == 'Addr2' ||
                 theField.fieldName == 'PostalCode' 
                 ) 
              {
              MaxLengthofField = '2';                                   
              }
             
             else if ( theField.fieldName == 'Email1' || theField.fieldName == 'Email2' ) {
               TypeofField = 'mail';
               MaxLengthofField = '4';
              }
             else if  ( theField.fieldName == 'Birthday' || theField.fieldName == 'Anniversary' ) {
               TypeofField = 'datetype';
               MaxLengthofField = '6';
               }
            
             else if ( theField.fieldName == 'HomePhone' || 
                       theField.fieldName == 'HomeFax' ||
                       theField.fieldName == 'BusPhone' ||
                       theField.fieldName == 'BusFax' ||
                       theField.fieldName == 'Pager' ||
                       theField.fieldName == 'CellPhone'
                       ) {
               
               TypeofField = 'phone';
               MaxLengthofField = '10';
               
               }
             else if ( theField.fieldName == 'BusPhoneExt') {
               TypeofField = 'number';
               MaxLengthofField = '1';
               
               } 
             
       			var tempStr = theField.fieldLabel.replace( ":" ,"");
       			theValidatePart += ',\'' + tmpName + '_' + this.formID + '_' + theField.fieldName +':' + tempStr + ':' + TypeofField+ ':' + MaxLengthofField + ':' + '\'';   
        		break;
		  
		       }
		
	      }
	   theValidatePart += ')';

		 
		/*     
		END 
   document.write(theValidatePart);		
		onclick = "' + theValidatePart + '"
		action="/includes/formSubmit"
		
		   */

    theForm += '<table width=100% cellpadding=8 cellspacing=0 border=0><tr><td class="reportOrderFormContentTD">';
		theForm += '<form class="amsFormForm" id="' + tmpName + '_' + this.formID + '" name="' + tmpName + '_' + this.formID + '" method="post" action="/includes/formSubmit/"  target="formtarget"/>';
    theForm += '<input type="hidden" name="amsFormID" value="' + this.formID + '" />';
    theForm += '<input type="hidden" name="WebsitePageID" value="' + this.PageID + '" />';		
		
		theForm += '<table class="amsFormTable" width="100%" cellspacing="0" cellpadding="0" border="0">';
		theForm += '<col class="amsFormColumn1" />';
		theForm += '<col class="amsFormColumn2" />';
	
		
		for( var i = 0; i < this.formFields.length; i++ )
		{
		  
			theField = this.formFields[i];
			/*
			amsFormConstant.FIELD_TYPE_RAW_TEXT = 1;
			amsFormConstant.FIELD_TYPE_SELECT = 2;
			amsFormConstant.FIELD_TYPE_BUTTON = 3;
			amsFormConstant.FIELD_TYPE_TEXT = 4;
			amsFormConstant.FIELD_TYPE_TEXTAREA = 5;
			amsFormConstant.FIELD_TYPE_CHECK = 6;
			amsFormConstant.FIELD_TYPE_RADIO = 7;
			amsFormConstant.FIELD_TYPE_HIDDEN = 8;
			amsFormConstant.FIELD_TYPE_SUBMIT = 9;
			amsFormConstant.FIELD_TYPE_CUSTOM_FIELD = 10;
			*/
				    var asterik; 			
			 if (theField.fieldRequired == '1') {
        asterik = '<font color=red><b>*</b></font>';
				    }
			else {
			   asterik = '';
			  }	    
			
			switch( parseInt( theField.fieldTypeID ) ) {
				case amsFormConstant.FIELD_TYPE_RAW_TEXT:
					theForm += '<tr>';
					theForm += '<td valign="top" colspan="2">' + theField.fieldLabel + '</td>';
					theForm += '</tr>';
					break;
				case amsFormConstant.FIELD_TYPE_TEXT:
				 	theForm += '<tr>';
					theForm += '<td valign="top" width="30%">' + theField.fieldLabel + asterik + '</td>';
					theForm += '<td valign="top"><input class="amsFormInputText" type="text" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '" value="' + theField.fieldValue + '" fieldRequired="' + + theField.fieldRequired + '"/></td>';
					theForm += '</tr>';
					break;
  	  	case amsFormConstant.FIELD_TYPE_TEXTAREA:
					theForm += '<tr>';
					theForm += '<td valign="top">' + theField.fieldLabel + '</td>';
					theForm += '<td valign="top"><textarea class="amsFormTextarea" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '">' + theField.fieldValue + '</textarea></td>';
					theForm += '</tr>';
					break;
				case amsFormConstant.FIELD_TYPE_SUBMIT:
					theFormSubmit += '<tr class="amsFormSubmitRow">';
					theFormSubmit += '<td height="100%" class="reportOrderFormContentTD" valign="top" align="center" colspan="2"><center><input onclick = "' + theValidatePart + '" class="amsFormSubmit" type="button" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '" value="' + theField.fieldLabel + '" /></center></td>';
					theFormSubmit += '</tr>';
					break;
				case amsFormConstant.FIELD_TYPE_SELECT:
					theForm += '<tr>';
					theForm += '<td valign="top">' + theField.fieldLabel + asterik +'</td>';
					theForm += '<td valign="top"><select name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '">'; 
					
					for( var ccount = 0; ccount < theField.populateSelect_value.length ; ccount++ ) {
					  theForm += '<option value="'+ theField.populateSelect_value[ccount] +'">'+ theField.populateSelect_display[ccount] +'</option>';
					  }
										theForm += '</select></td></tr>';
					break;
					
				case amsFormConstant.FIELD_TYPE_CUSTOM_FIELD:
					theForm += '<tr>';
					theForm += '<td valign="top"  width="30%">' + theField.fieldLabel + asterik+'</td>';
					theForm += '<td valign="top">';
					theForm += '<input class="amsFormInputText" type="text" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '" value="' + theField.fieldValue + '" fieldRequired="' + theField.fieldRequired + '"/>';
					//theForm += '<input type="hidden" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '_label" value="' + theField.fieldLabel + '"/>';
					theForm += '</td>';
					theForm += '</tr>';
					break;
					
				default:
					theForm += '<tr>';
					theForm += '<td valign="top"  width="30%">' + theField.fieldLabel + asterik +'</td>';
					theForm += '<td valign="top"><input class="amsFormInputText" type="text" name="' + tmpName + '_' + this.formID + '_' + theField.fieldName + '" value="' + theField.fieldValue + '" /></td>';
					theForm += '</tr>';
					break;
			}
		}

		
		

   		theForm += '</table>'; 
   		theForm += '<br>';

		  if (typeof(extraHTML) != 'undefined'){
		  theForm +=  extraHTML;
		  }
   
      
    	theForm +='</td></tr>' + theFormSubmit;	  
   
    	theForm += '</table>';



		theForm += '</form>';
		/* This Form is used for redirecting. Nothing to do with any form submitting process*/
		theForm += '<form class="amsFormForm" name="ReDirectTo" method="post">';
		theForm += '<input type="hidden" name="URLREDIRECT" value="'+this.URLAction+'">';
		theForm += '</form>';
		
		ams.amsFormConstant.getForm( this.formID ).innerHTML = theForm;
		this.loaded = true;
	}
}



ams.amsForm = amsForm;
ams.debug( '( "amsForm" ) - loaded' );

