	/*
	  Form Validation v1.1
	  Written by Sharky, quasimodo
	  Last Updated  21-Dec-2001  by  quasimodo
	*/
	
	
	function check() {
		if ( chktext(0,3) && chkbox() && chktext(4,4) && chkstate() && chktext(5,5) && chkemailtel() && chkbox2() && chktext(6,8) ) {
			return true;
		} else return false;
	}

        function chkstate() {
            country = document.forms['inputform'].working_country.value;
            if (country == '131' || country == '222') {
                if (document.forms['inputform'].state.value =='*') {
                    message("selected a State");
                    return false;
                }
                else {
                    return true;
                }
            }
            else {
                    return true;
            }
        }

	function info_chk() {
		if ( chktext(0,3) && chkbox() && chktext(4,4) && chkstate() && chktext(5,5) && chkemailtel() && chkbox2() ) {
			return true;
		} else return false;
	}

	function message(msg) {
		alert("You have not " + msg);
	}

	function chkbox() {
		items = document.forms['inputform'].org_type.length;
		items--;
		orgflag = 0;
		for ( i=0; i<=items; i++ ) {
			if (document.forms['inputform'].org_type[i].checked == true) {	orgflag = 1; } 
		}
		if ( orgflag == 0 ) {
			message("checked an organization type");
			return false;
		} else {
			return true;
		}
    }
	function chkbox2() {

		items = document.forms['inputform'].coverage.length;
		items--;
		checkflag = 0;
		for ( i=0; i<=items; i++ ) {
			if (document.forms['inputform'].coverage[i].checked == true) {	checkflag = 1; } 
		}
		if ( checkflag == 0 ) {
			message("checked a coverage area");
			return false;
		} else {
			return true;
		}
	}

	function chkemailtel() {
		if ( !txt("email")) {
			message("entered an e-mail address");
			return false;
		}
                else {
                    itm = eval("document.forms['inputform'].email.value");
                    if ( !emailCheck (itm) ) {
                        return false;
                    }
                }
		if ( !txt("tel") ) {
			message("entered a telephone number");
			return false;
		} 
		return true;
	}			


	function email_chk() {
		if ( !txt("Email")) {
			message("entered an e-mail address");
			return false;
		}
                else {
                    itm = eval("document.forms['inputform'].Email.value");
                    if ( !emailCheck (itm) ) {
                        return false;
                    }
                }
		return true;
	}


	function chktext(startnum,endnum) {
		textArray = new Array("a first name","a surname","a job title","a news organization","a postal address","a zip/post code","a username","a password","a confirmed password");
		nameArray = new Array("first_name","surname","job_title","news_organization","address1","post_code","username","password","confirm");
		textflag = 0;
		forloop: for ( i=startnum; i<=endnum; i++ ) {
			if ( !txt(nameArray[i],textArray[i]) ) {
				msg = "entered " + textArray[i];
				message(msg);
				textflag = 1;
				break forloop;
			}
		}
		if ( textflag == 1 ) { return false; } else { return true; }
	}
	
	function txt(nm) {
		itm = eval("document.forms['inputform']." + nm + ".value");
                itm = trimWhitespace(itm);
		if ( itm == "" || itm == null ) { return false; } else { return true; }
	}

        function trimWhitespace(inputString) {
          // Removes leading and trailing spaces from the passed string. Also
          // removes consecutive spaces and replaces it with one space.
          
	  var retValue = inputString;
	  var tmpValue = "";
	  
          // Check for spaces at the beginning of the string
          var ch = retValue.substring(0, 1);
          while (ch == " ") { 
             retValue = retValue.substring(1, retValue.length);
             ch = retValue.substring(0, 1);
          }
          
          // Check for spaces at the end of the string
          ch = retValue.substring(retValue.length-1, retValue.length);
          while (ch == " ") { 
             retValue = retValue.substring(0, retValue.length-1);
             ch = retValue.substring(retValue.length-1, retValue.length);
          }
          
          // Note that there are two spaces in the string - look for multiple spaces within the string
          while (retValue.indexOf("  ") != -1) { 
             retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
          }
                   
	  // Strip other white-space characters
	  tmpValue = retValue;
	  retValue = "";
	  len = tmpValue.length;
	  
	  for (var i=0; i < len; i++) 
	  {
	    if(tmpValue.charAt(i) >= ' ')
	    {
	      retValue = retValue + tmpValue.charAt(i);	    
	    }
	  }    

          // Return the trimmed string
          return retValue; 
          
       } // Ends the "trim" function


	function validateProfileName(formName, textValue){
	  var isOK = true;
	  
	  isOK = checkForWhiteSpaceOnly(formName, textValue);
	  
          if(!isOK){
            alert('You must enter at least one non-blank character.');
            return isOK;
          }
	  
	  // Any more checks down here...

	  return isOK;
	
	}

        function checkForWhiteSpaceOnly(formName, textName){
          var isOK = true;
          itm = eval("document.forms['" + formName + "']." + textName + ".value");
          if ( itm == "" || itm == null ) { 
            isOK = false; 
          } 
          
          if(isOK){
            ctr = 0;            
            len = itm.length;
            for (var i=0; i < len; i++) {
              if(itm.charAt(i) > ' '){
                ctr ++;
              }
            }
            
            if(ctr == 0){
              isOK = false;
            }
          }
          return isOK;
        }
	
	
        function checkNullSpaces(nm){
          var isOK = true;
          itm = trimWhitespace(eval("document.forms['inputform']." + nm + ".value"));
          if ( itm == "" || itm == null ) { 
            isOK = false; 
          } 
          
          if(isOK){
            ctr = 0;            
            len = itm.length;
            for (var i=0; i < len; i++) {
              if(itm.charAt(i) > ' '){
                ctr ++;
              }
            }
            
            if(ctr == 0){
              isOK = false;
            }
          }
          return isOK;
        }
	
	
        function checkAndDisplayError(nm){             
          var isOK = checkNullSpaces(nm);         
          if(!isOK){
            alert('You must enter at least one non-blank character');
          }
          return isOK
        }

	function changetype(formname) {
		values = document.forms['inputform'].elements['working_country'].options[formname].value;
		var idx = formname;
		document.forms['inputform'].country_address.selectedIndex = idx;
		document.forms['inputform'].working_country.selectedIndex = idx;
		document.forms['inputform'].state.length = 0;
		document.forms['inputform'].state.length = 1;
		document.forms['inputform'].state.options[0].value = '*';
		if ((values == "222" || values == "131")) {
                    document.forms['inputform'].state.options[0].text = '--- Select ---';
                } else {
                    document.forms['inputform'].state.options[0].text = '--- Not Required ---';
                }
		document.forms['inputform'].state.options[0].selected = true;
		if (values == "131") {
			for (i=0; i<db1.length; i++) {
                                var arrayOfStrings = db1[i].split("|");
				document.forms['inputform'].state.options[i+1] = new Option(arrayOfStrings[1],arrayOfStrings[0],"","");
			}
		}
		if (values == "222") {
			for (i=0; i<db2.length; i++) {
                                var arrayOfStrings2 = db2[i].split("|");
				document.forms['inputform'].state.options[i+1] = new Option(arrayOfStrings2[1],arrayOfStrings2[0],"","");
			}
		}
	}

	
	function chk_year() {
		si_fy = document.forms['inputform'].elements['yearStart'].selectedIndex;
		values_fy = document.forms['inputform'].yearStart.options[si_fy].value;
		si_ty = document.forms['inputform'].yearEnd.selectedIndex;
		values_ty = document.forms['inputform'].yearEnd.options[si_ty].value;
		if (values_fy > values_ty) {
			alert("You cannot set the from date greater than the to date.");
			document.forms['inputform'].yearEnd.selectedIndex = document.forms['inputform'].elements['yearStart'].selectedIndex;
		}
	}

	function chkpasswd() {
		if ( document.forms['inputform'].password.value == document.forms['inputform'].confirm.value ) {
			return true;
		} else { 
			alert("Passwords do not match - Please try again");
			document.forms['inputform'].password.value = "";
			document.forms['inputform'].confirm.value = "";
			return false;
		}
	}
	
	
        // --UK-- is the date valid?
        function IsDateValid(year, month, day)
        {
          correct = false;
          bi = (year%400==0 || (year%4==0 && year%100!=0));
          if ( ( month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) && day >= 1 && day <= 31)
            { correct = true;}
            else
            {
                  if ( ( month == 4 || month == 6 || month == 9 || month == 11 ) && day >= 1 && day <= 30)
                  { correct = true;}
                  else
                  {
                        if (bi)
                        { correct = ( day >= 1 && day <= 29 && month == 2);}
                        else
                        { correct = ( day >= 1 && day <= 28 && month == 2);}
                  }
                }
          return correct;  
        }

        // --UK-- is the date valid?
        function DatesAreValid(syear, smonth, sday, eyear, emonth, eday)
        {
          correct = false;
		  // first of all check for invalid leap years etc.
          if ( IsDateValid(syear, smonth, sday) && IsDateValid(eyear,emonth,eday) ) {
			if ( parseInt(syear) <= parseInt(eyear) )
			{
				if ( parseInt(syear) < parseInt(eyear) ) {
					correct = true; // simply return as we are before the end year!
				}
				else { // the syear must equal the eyear so check the rest
					if ( parseInt(smonth) < parseInt(emonth) ) {
						correct = true; // simply return as we are before the end month!
					}
					else { // the smonth must equal the emonth so check the days
						if ( parseInt(smonth) == parseInt(emonth) )
						{
							if ( parseInt(sday) <= parseInt(eday) ) {
								correct = true;
							}
							else {
								correct = false;
							}
						}
						else {
							correct = false;
							// Invalid month!
						}
					}
			    } // do we have a valid set of years!
			}
			else {
				// invalid year as syear is before eyear
				correct = false;
			}
		  }
          return correct;  
        }


	function chkDates() {
		si_fy = document.forms['inputform'].elements['yearStart'].selectedIndex;
		values_fy = document.forms['inputform'].yearStart.options[si_fy].value;
		si_fm = document.forms['inputform'].elements['monthStart'].selectedIndex;
		values_fm = document.forms['inputform'].monthStart.options[si_fm].value;
		si_fd = document.forms['inputform'].elements['dayStart'].selectedIndex;
		values_fd = parseInt(document.forms['inputform'].dayStart.options[si_fd].value);

        if ( IsDateValid( values_fy, values_fm, values_fd) == false ) {
			alert("You have selected an Invalid Date for the 'From' Date");
        }
        else {
            si_ty = document.forms['inputform'].elements['yearEnd'].selectedIndex;
            values_ty = document.forms['inputform'].yearEnd.options[si_ty].value;
            si_tm = document.forms['inputform'].elements['monthEnd'].selectedIndex;
            values_tm = document.forms['inputform'].monthEnd.options[si_tm].value;
            si_td = document.forms['inputform'].elements['dayEnd'].selectedIndex;
            values_td = parseInt(document.forms['inputform'].dayEnd.options[si_td].value);

            if ( IsDateValid( values_ty, values_tm, values_td) == false ) {
                alert("You have selected an Invalid Date for the 'To' Date");
            }
            else {
                if ( DatesAreValid(values_fy, values_fm, values_fd, values_ty, values_tm, values_td) == false ) {
                    alert("You have entered a 'From' date set after the 'To' date");
                }
            }
        }
	}


	function chkDate() {
		si_fy = document.forms['inputform'].elements['year'].selectedIndex;
		values_fy = document.forms['inputform'].year.options[si_fy].value;
		si_fm = document.forms['inputform'].elements['month'].selectedIndex;
		values_fm = document.forms['inputform'].month.options[si_fm].value;
		si_fd = document.forms['inputform'].elements['day'].selectedIndex;
		values_fd = parseInt(document.forms['inputform'].day.options[si_fd].value);

        if ( IsDateValid( values_fy, values_fm, values_fd) == false ) {
			alert("You have selected an Invalid Date");
        }
	} 
