// Copyright 2005 - Corona Technologies Ltd.

function FormSubmit(divid,formid) {
	if (ValidateForm(formid)) {
  	frm = document.getElementById(formid)
  	frm.submit();
  	document.getElementById(divid).innerHTML = '<p style="height:100px">Procesing...</p>';
	} else {
	 return false;
	}
}

function FormComplete(divid,message) {
	document.getElementById(divid).innerHTML = '<p style="height:100px">' + message + '</p>';
}

// Validate form for submission
function ValidateForm(formid) {
  formobj = document.getElementById(formid)
	for (i=0;i<formobj.elements.length;i++) {
   obj = formobj.elements[i]
	
   // Identify Validation required - check for disabled
   if (obj.getAttribute("datafld")) {
  
   disabledlayer = false
   obj1 = obj
  
   // Find if obj is on a DIV
   while (obj1.tagName != 'BODY') {
  	if (obj1.tagName == 'DIV') {
  	 if (obj1.style.display == 'none') {
  	  disabledlayer = true
  	  break;
  	 }
  	}
    obj1 = obj1.parentNode
   }
  
   if ((obj.getAttribute("datafld") != '') && (!disabledlayer)) {
   valset = obj.getAttribute("datafld").split("|")
  
   // Trim input
   obj.value = TrimSpaces(obj.value)
  
   obj_value = obj.value
   obj_name = valset[1]
   if (valset[0] == 'Y') {
    obj_mandatory = true
   } else {
    obj_mandatory = false
   }
   obj_type = valset[2]
   obj_params = valset.slice(3)

   // Mandatory
   if (obj_mandatory) {
    if ((obj_value == '') && ((obj.type == 'text') || (obj.type == 'textarea'))) return SetFocus('Please enter a ' + obj_name, obj)
    if ((!obj.checked) && (obj.type == 'checkbox')) return SetFocus(obj_name,obj)
  
  	// Check selection has ben made on selection boxes
    if (obj.type.substr(0,6) == 'select') {
  	 if (obj.options.selectedIndex==0) {
  		return SetFocus('Please select ' + obj_name, obj);;
  		}
    }
   }
  
   // Validation Type
   if (obj_type) {
  	 switch (obj_type.toUpperCase()) {
     // Alphanumeric 0-9 a-z A-Z Space Hyphen / \
  	 case 'ALPHA':
     			var Pat1 = /^[a-zA-Z -]*$/;
     			if (!Pat1.test(obj_value)) { return SetFocus('Please enter valid details' ,obj); }
  	 			break;
  
  	 case 'ALPHANUMERIC':
     			var Pat1 = /^[-a-zA-Z0-9 \\\/.,]*$/;
     			if (!Pat1.test(obj_value)) { return SetFocus('Please enter valid details',obj); }
  	 			break;
  
  	 // Alphanumeric with Extended characters
  	 case 'ANEXTENDED':
     			var Pat1 = /^[-a-zA-Z0-9 \\\/.,\x27!;@:"£\$%\^&\*()]*$/;
     			if (!Pat1.test(obj_value)) { return SetFocus('Please enter valid details',obj); }
  	 			break;
  
     // Numeric 0-9 NUMERIC|Min|Max
  	 case 'NUMERIC':
     			var Pat1 = /^[0-9]*$/;
     			if (!Pat1.test(obj_value)) { return SetFocus('Please enter a valid number',obj); }
  	 			// Perform Range Checking
  				if (obj_params != '') {
  				 obj_value = parseFloat(obj_value)
  				 // Minimum
  				 if (obj_params[0]) {
      			 if (obj_value < obj_params[0]) { return SetFocus('Please enter a valid number',obj) }
  				 }
  				 // Maximum
  				 if (obj_params[1]) {
      			 if (obj_value > obj_params[1]) { return SetFocus('Please enter a valid number',obj); }
  				 }
  				}
  	 			break;
  
  	 // Email Address xxx@xxx.xxx
  	 case 'EMAIL':
					obj_value = obj_value.toLowerCase();
  				obj.value = obj_value;
  				Pat1 = /^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/;
          if ((obj_value != '') && (!Pat1.test(obj_value))) {
  						return SetFocus( 'Please enter a valid email address \n\ne.g. name@place.com', obj);
          }
  	 			break;
  
  	 // Address Fields
  	 case 'ADDRESS':
     			var Pat1 = /^[-a-zA-Z0-9 ,\.\x27]*$/;
  				apos = String.fromCharCode(39)
  				if (!Pat1.test(obj_value)) { return SetFocus('Please enter a valid address.', obj); }
  	 			break;
  
  	 // Name Fields
  	 case 'NAME':
     			var Pat1 = /^[-a-zA-Z ,\.\x27]*$/;
     			apos = String.fromCharCode(39)
  				if (!Pat1.test(obj_value)) { return SetFocus('Please enter a valid name.' , obj); }
  	 			break;
  
  	 // UK Postcode XX1 1XX
  	 case 'POSTCODE':
					obj_value = obj_value.toUpperCase();
          obj.value = obj_value;
					Pat1 = /^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/;
					if (!Pat1.test(obj_value)) {
  						return SetFocus('Please enter a valid post code.',obj);
           }
  	 			break;
  
  	 // Phone number
  	 case 'PHONE':
          obj.value = RemoveAllSpaces(obj.value)
  				obj_value = obj.value
  				Pat1 = /^[0-9 ]*$/;
          if ((!Pat1.test(obj_value)) && (obj_value != '')) {
  						return SetFocus('Please enter valid phone details.', obj);
           }
  	 			break;
  
  	 // Check to see if two values of 2 fields match
  	 case 'MATCH':
  			  obj_1 = document.getElementsByName(obj_params[0])[0]
  			  obj_2 = document.getElementsByName(obj_params[1])[0]
          if (obj_1.value != obj_2.value) return SetFocus(obj_name,obj_1,obj_2);
  	 			break;
  
  	 case 'EITHER_BOTH':
  	      obj_1 = document.getElementsByName(obj_params[0])[0]
  			  obj_2 = document.getElementsByName(obj_params[1])[0]
					if( obj_1.value == '' && obj_2.value == '' ) return SetFocus('Please enter either a ' + obj_name, obj_1, obj_2);false
  			  break;
  
      case 'RADIO_SELECTED':
            num = obj_params.length;
            selected = false;
            for (z=0;z<num;z++)
            {
               if( getElem(obj_params[z]).checked )
               {
                  selected = true;
               }
            }
            if( selected == false ) return SetFocus(obj_name, getElem(obj_params[num-1]));
  			 break;
  
  	 case 'DATE':
  			  day = getElemName(obj_params[0])[0].value
  			  month = getElemName(obj_params[1])[0].value
  				year = getElemName(obj_params[2])[0].value
  				datein = day + '/' + month +'/' + year
  				var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
  			 	var matchArray = datein.match(datePat);
           if (matchArray == null) {
  					return SetFocus(obj_name,obj);false
           } else {
            if ((month==4 || month==6 || month==9 || month==11) && day==31) {
  						return SetFocus(obj_name,obj);false
            }
            if (month == 2) {
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day>29 || (day==29 && !isleap)) {
  								return SetFocus(obj_name,obj);false
                }
            }
           }
  			  break;
  
    	 } // switch
  		} // if - mandatory
  	} // if - Validation Required
  	} // if alt tag exists

  // Change button style
		obj.style.borderColor = '';
		obj.style.borderWidth = '';
		obj.style.borderStyle = '';
  } // for 
  return true;
}

function ClearField(obj) {
if (obj.value != '') {
 obj.value = '';
 obj.select()
 obj.onfocus = null;
 }
}

// Set Focus to field and show message
function SetFocus(alertmess,obj1,obj2) {
obj1.style.borderColor = '#f00';
obj1.style.borderWidth = '1px';
obj1.style.borderStyle = 'solid';
if (obj2 != null) {
  obj2.style.borderColor = '#f00';
  obj2.style.borderWidth = '1px';
  obj2.style.borderStyle = 'solid';
}
alert(alertmess)
obj1.focus()
return false;
}

// Remove all spaces in value
function RemoveAllSpaces(objval) {
	RESpace = /\s/ig;
  objval = objval.replace(RESpace, '');
  return objval
}

// Trim spaces from start and end of string
function TrimSpaces(objval) {
  // Remove Space at start
  RESpace = /^\s*/i;
  objval = objval.replace(RESpace, '');
  // Remove Space at end
  RESpace = /\s*$/i;
  objval = objval.replace(RESpace, '');
  return objval;
}

// -->