/* CTForm.js */

function CTTableFormItem (type, name, value, width){
 this.created=false;
 this.type=type.toLowerCase();
 this.width=width;
 this.bgColor="";
 this.colSpan=1;
 this.rowSpan=1;
 this.handlers="";
 this.align="left";
 this.valign="middle";
 this.name=name;
 this.imgSrc='';
 this.imgWidth=0;
 this.imgHeight=0;
 this.imgBorder=0;
 this.caption="";
 this.itemBgColor="";
 this.FontColor="#000000";
 this.FontFamily="Verdana";
 this.FontStyle="plain";
 this.FontWeight="bold";
 this.FontSize="8";
 this.size="";
 this.maxLength="";
 this.border="";
 this.value=(this.type=='expand'?'':value);
 this.status="";
 this.optionText=new Array();
 this.optionValue=new Array();
 this.optionSelect=new Array();
 this.setHandlers=CTTableItemSetHandlers;
 this.setLayout=CTTableItemSetLayout;
 this.setImage=CTTableItemSetImage;
 this.setFont=CTTableItemSetFont;
 this.setStatus=CTTableFormItemSetStatus;
 this.setStyle=CTTableFormItemSetStyle;
 this.setImage=CTTableFormItemSetImage;
 this.addOption=CTTableFormItemAddOption;
 this.addOption2=CTTableFormItemAddOption2;
 this.removeOption=CTTableFormItemRemoveOption;
 this.getObject=CTTableFormItemGetObject;
 this.addObject=CTTableFormItemAddObject;
 this.clear=CTTableFormItemClear;
 this.create=CTTableFormItemCreate;
 this.createStr=CTTableFormItemCreateStr;
 this.ClearValue=CTTableFormItemClearValue;
 this.SetValue=CTTableFormItemSetValue;
}

function CTTableFormItemGetObject ()
{
  if(isMinNS4) {
   return (document[this.name]);
  }
  else if(isMinIE4) {
   return (eval('document.all.'+this.name));
  }
  else return (NULL);
}

function CTTableFormItemClear () {
var cb;

 if (this.type == 'select' || this.type == 'selectmultiple')
 {
  this.optionText.length=0;
  this.optionValue.length=0;
  this.optionSelect.length=0;
  if (this.created) {
    cb = this.getObject ();
    cb.options.length=0;  
  }
 }
}

function CTTableFormItemAddOption(text, value){

var opt;
var cb;

 this.optionText[this.optionText.length]=text;
 this.optionValue[this.optionValue.length]=value;
 this.optionSelect[this.optionSelect.length]=0;
 if (this.created) {
   opt = new Option (text, value, 0, 0);
   cb = this.getObject ();
   cb.options[cb.options.length]=opt;  
 }
}

function CTTableFormItemAddOption2(text, value, selected){

var opt;
var cb;

 this.optionText[this.optionText.length]=text;
 this.optionValue[this.optionValue.length]=value;
 this.optionSelect[this.optionSelect.length]=selected;
 if (this.created) {
   opt = new Option (text, value, 0, selected);
   cb = this.getObject ();
   cb.options[cb.options.length]=opt;  
 }
}

function CTTableFormItemRemoveOption(){

var opt;
var cb;
var ind;
var cont;
var text;
var value;

 if (this.created) {
   cb = this.getObject ();
   if (cb.options.length>0) {
	ind=cb.options.selectedIndex;
	for (cont=ind+1;cont<cb.options.length;cont++) {
		text=cb.options[cont].text;
		value=cb.options[cont].value;
		opt = new Option (text, value, 0, 0);
		cb.options[cont-1]=opt;  
	}
   cb.options.length=cb.options.length-1;
   }
 }
}

function CTTableFormItemSetStatus(status){
 if (!this.created) {
   this.status=status;
 }
}

function CTTableFormItemSetStyle(size, maxLength, border, itemBgColor){
 if (!this.created) {
   this.size=size;
   this.maxLength=maxLength;
   this.border=border;
   this.itemBgColor=itemBgColor;
 }
}

function CTTableFormItemSetImage (imgsrc, width, height, caption, border){
 if(!this.created){
  this.imgSrc=imgsrc;
  this.imgWidth=width;
  this.imgHeight=height;
  this.caption=caption;
  this.imgBorder=border;
 }
}

function CTTableFormItemClearValue (){

 if(this.type != "expand")
   return;

 this.value='';
}


function CTTableFormItemSetValue (text){

 if(this.type = "textarea")  {
	ta = this.getObject ();
	ta.value=text;  
 }
 if(this.type = "readonly") {
	ta = this.getObject ();
	ta.value=text;  
 }

}



function CTTableFormItemAddObject (obj) {
 if(this.type != "expand")
   return;

 var str=obj.createStr();
 this.value+=str;

 if (this.created)
 {
   if(isMinNS4) {
     document[this.name].innerHTML=value;
   } 
   else if(isMinIE4) {
     eval('document.all.'+this.name).innerHTML=this.value;
   }
 }
}

function CTTableFormItemCreate (curtable){
 var str;

 if (this.created) return ('');

 str='<td'+(this.width>0? ' width='+adjust(this.width):'')+' align='+this.align+' valign='+this.valign+(this.bgColor==""?'':' bgcolor='+this.bgColor)+' colspan='+this.colSpan+' rowspan='+this.rowSpan+'>';
 str+=this.createStr(curtable);
 str+='</td>\n';
 this.created=true;
 return (str);
}

function CTTableFormItemCreateStr (curtable){
 var str;
 var tp=this.type.toLowerCase ()
 switch (tp) {
  case 'text':
  case 'password':
  case 'button':
  case 'file':
  case 'submit':
  case 'hidden':
  case 'reset':
   
   str='<INPUT TYPE='+this.type+' NAME="'+this.name+'"'+(this.value!=""? ' VALUE="'+this.value+'"':'');
  
   if (tp == 'text' || tp == 'password') {
    str+=(this.size!=""? ' SIZE='+this.size:'')+(this.maxLength!=""? ' MAXLENGTH='+this.maxLength:'');
   }
   str+=(this.handlers != ""? ' '+this.handlers:'');

  
   str+=' STYLE="color:'+this.FontColor+';font-family:'+this.FontFamily+';font-size:'+adjust(this.FontSize)+'pt;font-style:'+this.FontStyle+';font-weight:'+this.FontWeight+';';

   str+=(this.itemBgColor!=""?' background-color:'+this.itemBgColor+';':'')+(this.border!=""?' border='+this.border+';':'');   
   str+='">';
   break;
  case 'image':
   str='<INPUT TYPE='+this.type+(this.imgSrc!=""? ' SRC="'+this.imgSrc+'"':'');
   str+=(this.imgWidth != 0? ' WIDTH='+adjust(this.imgWidth):'');
   str+=(this.imgHeight != 0? ' HEIGHT='+adjust(this.imgHeight):'');
   str+=(this.imgBorder != 0? ' BORDER='+adjust(this.imgBorder):'');
   str+=(this.caption != ''? ' ALT="'+this.caption+'"':'');
   str+=(this.handlers != ""? ' '+this.handlers:'');
   str+='>';
   break;

  case 'radio':
  case 'checkbox':
   str='<INPUT TYPE='+this.type+' NAME='+this.name+(this.value!=""? ' VALUE="'+this.value+'"':'');
   str+=(this.status != ""? ' '+this.status:'');
   str+=(this.handlers != ""? ' '+this.handlers:'');
   str+='>'
   break;
  case 'select':
   str='<SELECT NAME='+this.name+(this.size!=""? ' SIZE='+this.size:'');
   str+=(this.handlers != ""? ' '+this.handlers:'');
   str+=' STYLE="color:'+this.FontColor+';font-family:'+this.FontFamily+';font-size:'+adjust(this.FontSize)+'pt;font-style:'+this.FontStyle+';font-weight:'+this.FontWeight+';';
   str+=(this.itemBgColor!=""?' background-color:'+this.itemBgColor+';':'');   
   str+='">';
   for (i=0; i<this.optionText.length; i++) {
    str+='<OPTION VALUE='+this.optionValue[i]+(this.optionSelect[i]==1?' SELECTED>':'>')+this.optionText[i]+'</OPTION>';
   }
   str+='</SELECT>';
   break;

  case 'selectmultiple':
   str='<SELECT NAME='+this.name+(this.size!=""? ' SIZE='+this.size:'');
   str+=(this.handlers != ""? ' '+this.handlers:'');
   str+=' Multiple '
   str+=' STYLE="color:'+this.FontColor+';font-family:'+this.FontFamily+';font-size:'+adjust(this.FontSize)+'pt;font-style:'+this.FontStyle+';font-weight:'+this.FontWeight+';';
   str+=(this.itemBgColor!=""?' background-color:'+this.itemBgColor+';':'');   
   str+='">';
   for (i=0; i<this.optionText.length; i++) {
    str+='<OPTION VALUE='+this.optionValue[i]+(this.optionSelect[i]==1?' SELECTED>':'>')+this.optionText[i]+'</OPTION>';
   }
   str+='</SELECT>';
   break;

  case 'textarea':
   str='<TEXTAREA NAME="'+this.name+'"'+(this.size!=""? ' ROWS='+this.size:'')+(this.maxLength!=""? ' COLS='+this.maxLength:'' );
   str+=(this.handlers != ""? ' '+this.handlers:'');
   str+=' STYLE="color:'+this.FontColor+';font-family:'+this.FontFamily+';font-size:'+adjust(this.FontSize)+'pt;font-style:'+this.FontStyle+';font-weight:'+this.FontWeight+';';
   str+=(this.itemBgColor!=""?' background-color:'+this.itemBgColor+';':'');   
   str+='">';
   str+='</TEXTAREA>';
   if(isMinNS4) afterCmds+='document.'+this.name+'.value='+this.value+';\n';
   if(isMinIE4) afterCmds+='document.all.'+this.name+'.value="'+this.value+'";\n';

  break;
  case 'expand':
   str='<SPAN ID="'+this.name+'">';
   str+=(this.value != ''? this.value:'');
   str+="</SPAN>";
   break;
  case 'readonly':
   str='<input name="'+this.name+'" readonly style="border:none" value="'+this.value+'">';
   break;
  case 'label':
   str='<input type=text name="'+this.name+'" readonly'+(this.value!=""? ' VALUE="'+this.value+'"':'');
   str+=(this.size!=""? ' SIZE='+this.size:'')+(this.maxLength!=""? ' MAXLENGTH='+this.maxLength:'');
   str+=' STYLE="border=3;color:'+this.FontColor+';font-family:'+this.FontFamily+';font-size:'+adjust(this.FontSize)+'pt;font-style:'+this.FontStyle+';font-weight:'+this.FontWeight+';';
   str+=(this.itemBgColor!=""?' background-color:'+this.itemBgColor+';':'');   
   str+='">';
   break;
 }
 return (str);
}


function checkCaracter(){
 for (i=0;i<document.forms[0].elements.length-1;i++){
  if ((num=document.forms[0].elements[i].value.indexOf('"')>=0) || 
 (num=document.forms[0].elements[i].value.indexOf("'")>=0)) {
    alert ("Nenhum campo não deve conter aspas ou ' !!!" ) ; 
    return false
  }
 }
}
