
function SQValidateElement(pName, pType, pManditory, pLen, pRegular, pErrorDesc)
{
	this.Name = pName;
	this.Type = pType;
	this.Manditory = pManditory;
	this.Len = pLen;
	this.Regular = pRegular;
	this.ErrorDesc = pErrorDesc;
}

function SQSetValue(pForm, pName, pValue)
{
	var o, e, i
    
  // o = pForm[pName];
	 o = pForm.elements[pName];
 //   o = pForm.item(pName)
    if ( o.type == null ) {
		for (i=0; i<o.length; i++) 
			o.item(i).checked = (o.item(i).value == pValue )
    }
    else {
	    o.value = pValue
    }
}

function SQGetValue(pForm, pName)
{
	var o, e, i
    
//   o = pForm.item(pName)
	//o = document.all[pName]
//    o = pForm[pName]
		o = pForm.elements[pName];
    if ( o.type == null ) {
		for (i=0; i<o.length; i++) 
			if ( o.item(i).checked )
				return o.item(i).value
		return ""
    }
    else
		return o.value
}

function SQValidateForm(pForm) {
	var Field, FieldIndex, Value, NewValue, o
	var ErrorFieldName="", ErrorDescription=""
	
	for (FieldIndex = 0; FieldIndex<SQValidateElements.length; FieldIndex++) {
		Field		= SQValidateElements[FieldIndex]
		Value		= SQGetValue(pForm, Field.Name)
		NewValue	= Value.replace(/(^\s+)|(\s+$)/g, "")
		
		var ThisErrorDescription = ""
		
// Manditory
		switch ( Field.Type ) {
			case 1 : // Single
				break;
				
			case 2 : // Multiple
				break;
				
			case 3 : // Float
				NewValue = NewValue.replace(/[^0-9.]/g, "")

			case 4 : // TextFixed
				if (NewValue.length > Field.Len && Field.Len>0)
					ThisErrorDescription = "Field is too long, must be less than " + Field.Len.tostring + " characters long."
				break;
				
			case 5 : // TextUnlimited
				break;
				
			case 7 : // Integer
				NewValue = NewValue.replace(/[^0-9]/g, "")
				break;
				
			default:
				window.alert("Bad field type for field " + Field.Name)
				break;
		}
		if ( NewValue=="" && Field.Manditory )
		{
			if ( Field.ErrorDesc=="")
				ThisErrorDescription = "Must be completed"
			else
				ThisErrorDescription = Field.ErrorDesc
		}
		else if (ErrorDescription.length==0 && Field.Regular!=null && ThisErrorDescription.length==0)
			if ( !Field.Regular.test(NewValue)) 
				ThisErrorDescription = (Field.ErrorDesc.length==0, "Wrong Format", Field.ErrorDesc)

		if (NewValue!=Value)
			SQSetValue(pForm, Field.Name, NewValue)

		if ( ThisErrorDescription.length>0 && ErrorFieldName.length==0) {
			ErrorFieldName	 = Field.Name
			ErrorDescription = ThisErrorDescription
		}
	}
	//if ( ThisErrorDescription.length>0) {
	if ( ErrorDescription.length>0) {
		window.alert ( ErrorDescription)
//		o = pForm[ErrorFieldName] //.focus()
		//o = document.all[ErrorFieldName]
		o = pForm.elements[ErrorFieldName];
	    if ( o.type == null ) 
			o = o.item(0)
		o.focus()
		return false
	}
	return true
}
