// JAVASCRIPT FILE FOR FORMATTING/VALIDATING WEB REGISTRATIONS
// Ben Hambidge - 15th May 2000

// list of country names to translate. must be in caps.

  countrychange = new Array();

  countrychange["DEUTSCHLAND"]="GERMANY"
  countrychange["U.S.A."]="USA"
  countrychange["U.S.A"]="USA"
  countrychange["U S A"]="USA"
  countrychange["U. S. A."]="USA"
  countrychange["SVERIGE"]="SWEDEN"
  countrychange["ESPANA"]="SPAIN"
  countrychange["ENGLAND"]=""
  countrychange["WALES"]=""
  countrychange["SCOTLAND"]=""
  countrychange["ITALIA"]="ITALY"
  countrychange["UNITED STATES"]="USA"
  countrychange["UNITED STATES OF AMERICA"]="USA"
  countrychange["NORGE"]="NORWAY"
  countrychange["DANMARK"]="DENMARK"
  countrychange["EIRE"]="IRELAND"
  countrychange["TURKIYE"]="TURKEY"
  countrychange["BELGIE"]="BELGIUM"
  countrychange["BELGE"]="BELGIUM"
  countrychange["LA SUISSE"]="SWITZERLAND"
  countrychange["SUISSE"]="SWITZERLAND"
  countrychange["SCHWEIZ"]="SWITZERLAND"
  countrychange["NEDERLAND"]="NETHERLANDS"
  countrychange["OSTERREICH"]="AUSTRIA"
  countrychange["MAGYAR"]="HUNGARY"
  countrychange["BRASIL"]="BRAZIL"
  countrychange["HOLLAND"]="NETHERLANDS"
  
// list of london boroughs. must be in caps.

londonboroughs = ["CAMDEN","WESTMINSTER","FULHAM","TOTTENHAM","HAMMERSMITH","CHELSEA","BOW","HACKNEY","CHINGFORD","HIGHGATE","BLACKHEATH","SHEEN","EAST SHEEN","WIMBLEDON","WANDSWORTH","CLAPHAM","PUTNEY","KNIGHTSBRIDGE","WEST KENSINGTON","PARK ROYAL","CHISWICK","COVENT GARDEN"]

namex = ["de", "de la", "la", "l'", "le", "du", "des", "van", "von", "der", "van der", "von der", "van den", "von den", "vder"]

// This function writes out the form information to a cookie on the users
// local machine. It loops through the form and stores as name/value pairs

function writecookie() {
	var cookie=""
	var today = new Date()
	var expire = new Date()
	// expires in 1 year
	expire.setTime(today.getTime()+1000*60*60*24*365)
	for (var i=0; i<document.qform.elements.length; i++) {
		switch (document.qform.elements[i].type) {
			case "text":
			case "hidden":
			case "textarea":
				if (document.qform.elements[i].name != "products") {
					cookie=cookie+"&"+escape(document.qform.elements[i].name)+"="+escape(document.qform.elements[i].value)
				}
				break;
			case "select-one":
				cookie=cookie+"&"+escape(document.qform.elements[i].name)+"="+escape(document.qform.elements[i].options[document.qform.elements[i].selectedIndex].value)
				break;
			case "select-multiple":
				for (var j=0; j < document.qform.elements[i].length; j++) {
					if (document.qform.elements[i].options[j].selected==true) {
						cookie=cookie+"&"+escape(document.qform.elements[i].name)+"="+escape(document.qform.elements[i].options[j].value)
					}
				}
			case "checkbox":
				cookie=cookie+"&"+escape(document.qform.elements[i].name)+"="+escape(document.qform.elements[i].checked)
				break;
			case "radio":
				if (document.qform.elements[i].checked == true) {
					cookie=cookie+"&"+escape(document.qform.elements[i].name)+"="+escape(document.qform.elements[i].value)
				}
				break;
		}
	}
	cookie=cookie.substr(1)
	document.cookie = cookiename +"=" + cookie + "; expires=" + expire.toGMTString()
	return
}

// This function reads in the information on the cookie, and sets the
// form with the correct details.

function readcookie() {
	MakeCookieArray(GetCookie(cookiename));
	for (var i=0; i < ckArray.length; i++) {
		var leng = ckArray[i].indexOf("=")
		if (leng >= 0) {
			var field = unescape(ckArray[i].substr(0,leng))
			var val = unescape(ckArray[i].substr(leng+1))
			var thisfield = document.qform[field]
			if (typeof(thisfield)=="undefined") {
			} else {
				switch (thisfield.type) {
					case "text":
					case "hidden":
					case "textarea":
						thisfield.value = val
						break;
					case "select-one":
						for (var j=0; j<thisfield.length; j++) {
							if (thisfield.options[j].value==val) {
								thisfield.selectedIndex=j
								j=thisfield.length
							}
						}		
						break;
					case "select-multiple":
						for (var j=0; j<thisfield.length; j++) {
							if (thisfield.options[j].value==val) {
								thisfield.options[j].selected=true
								j=thisfield.length
							}
						}		
						break;
					case "radio":
						for ( var j=0; j<document.qform[field].length; j++) {
							if (document.qform[field][j].value==val) {
								document.qform[field][j].checked = true;
							}
						}
						break;					
				}
			}
		}
	}
}

// Credit to inglis_geoff@Yahoo.com - sample script
//Get the cookie from a list of possible cookies.
function GetCookie (CookieName) {
  var cname = CookieName + "=";
  var i = 0;
  while (i < document.cookie.length) {
    var j = i + cname.length;
    if (document.cookie.substring(i, j) == cname){
	var leng = document.cookie.indexOf (";", j);
	if (leng == -1) leng = document.cookie.length;
	return document.cookie.substring(j, leng);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; //thats -1 plus 1, duh.
  }
  return "*";
}

//Parse Big  Cookie. A CookieCutter if you will.
function MakeCookieArray(cookieValue) {
  var i = 0,indx = 0, citemlen =0;
  ckArray = new Array();
   if ( cookieValue == null ) {ckArray[0]= "*";return}//Data has expired or never entered.
   if ( cookieValue == "*"  ) {ckArray[0]= "*";return}//Data has expired or never entered.
  while (citemlen < cookieValue.length) {
    citemlen=(cookieValue.indexOf("&", indx)>0)
	     ?cookieValue.indexOf("&", indx):cookieValue.length;
    ckArray[i]= cookieValue.substring(indx, citemlen); i++;
    indx = citemlen + 1;
  }
}

// This validates the field to make sure it has been completed correctly.

function validatefield(field) {
	var temp
	switch (field.validtype) {
		case "text":
				temp=ltrim(trim(field.value))
				if (temp.length==0) {
					alert(field.errormsg)
					field.focus()
					return false
				}
				break;
		case "listitem":
				temp=ltrim(trim(field.options[field.selectedIndex].value))
				if (temp.length==0) {
					alert(field.errormsg)
					field.focus()
					return false
				}
				break;
		case "multilist":
				var gotone=0
				for (var i=0; i < field.options.length; i++) {
					if (field.options[i].selected==true) {
						gotone=1
						i=field.options.length
					}
				}
				if (gotone==0) {
					alert(field.errormsg)
					field.focus()
					return false
				}
				break;
		case "email":
				if (!validateemail(field.value)) {
					alert(field.errormsg)
					field.focus()
					return false
				}
				break;
		case "radio":
				var RetVal=false
				for (var j=0; j<field.length; j++) {
					if (field[j].checked==true) {
						RetVal=true
					}
				}
				return RetVal
				break;			
		default:
	}
}

// This loops through the form and validates every field that has a
// validtype property set.

function validateform(frm) {
	var x
	for (var i=0; i<frm.elements.length; i++) {
		x=frm.elements[i]
//                if (x.type=='radio') {
//                      var y=document.qform[x.name]
//                      document.qform.code1.validtype="radio"
//              }        
		if (validatefield(frm.elements[i])==false) {
			return false
		}
	}
	return true
}

// This function checks any of the demographics to ensure they have
// been completed. It only checks them if they are included in the
// checkdems array

function validatedemogs(frm) {
  for (var e = 0; e < checkdems.length; e++) {
    var el
	for (var f = 0; f < frm.elements.length; f++) {
		if (frm.elements[f].name == checkdems[e]) {
			el=frm.elements[f]
			f=frm.elements.length
		}
	}
    if (el.type == 'text' || el.type == 'textarea' ||
        el.type == 'password' || el.type == 'file' ) { 
      if (el.value == '') {
        alert('Please fill out ' + demdescr[e]);
        el.focus();
        return false;
      }
    }
    else if (el.type.indexOf('select') != -1) {
      if (el.selectedIndex == -1) {
        alert('Please select an option for ' + demdescr[e]);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'radio') {
      var group = frm[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check one of the radio buttons for ' + demdescr[e]);
        el.focus();
        return false;
      }
    }
    else if (el.type == 'checkbox') {
      var group = frm[el.name];
      if (group.length) {
        var checked = false;
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
        if (!checked) {
          alert('Please check one or more checkboxes for ' + demdescr[e]);
          el.focus();
          return false;
        }
      }
    }
  }
  return true;
}

// Calls all the routines to validate the contact details, demographics,
// and ensure a postcode for UK addresses

function validateformandpcode(frm) {
        addformat(frm);
	if (!validateform(frm)) {
		return false
	}
	if (frm.country.options[frm.country.selectedIndex].value=="" && frm.pcode.value=="") {
		alert("We require a postcode for UK addresses")
		frm.pcode.focus()
		return false
	}

	if (!validatedemogs(frm)) {
		return false
	}

	var x
	for (var i=0; i<frm.elements.length; i++) {
		x=frm.elements[i]
                if (x.name=="ccno") {
                     // frm.cctype.options.value="SWIT"
                        if (!checkccdate(frm)) {
                        return false
                        }               

        if (frm.cctype.value=="SWIT" && Number(frm.ccswis.value)==0) {
                alert("We require an issue number for Switch Cards")
                frm.ccswis.focus()
		return false
	}
        }
        }
	return true
}


// routine to check email integrity

function validateemail(s) {
	s=ltrim(trim(s))
	s=s.toLowerCase()
	if (s.indexOf("@") < 1) {
		return false
	}
	var cAfterAt=s.substr(s.indexOf("@")+1)
	
	if (s.indexOf(" ") >= 0) {
		return false
	} else if (s.indexOf(",") >= 0) {
		return false
	} else if (s.indexOf("/") >= 0) {
		return false
	} else if (s.indexOf("\\") >= 0) {
		return false
	} else if (s.indexOf(";") >= 0) {
		return false
	} else if (s.indexOf("<") >= 0) {
		return false
	} else if (s.indexOf(">") >= 0) {
		return false
	} else if (s.indexOf("(") >= 0) {
		return false
	} else if (s.indexOf(")") >= 0) {
		return false
	} else if (cAfterAt.indexOf("@") >= 0) {
		return false
	} else if (cAfterAt.indexOf(".") <1) {
		return false
	} else if (cAfterAt.indexOf("..") >=0) {
		return false
	}
	
	var cReverse=reversestring(cAfterAt)
	if (cReverse.indexOf(".")<2 || cReverse.indexOf(".")>4) {
		return false
	}
	return true
} 

// reverse the characters in a string

function reversestring(s) {
	var RetVal=""
	for (var i=s.length-1; i>=0; i--) {
		RetVal=RetVal+s.substr(i,1)
	}
	return RetVal
}

// This function returns true if the argument is a positive number

function isaPosNum(s) {
	return (parseInt(s) > 0)
}

// return true if the character is A-Za-z

function isAlpha(s) {
	var temp
	if (s.length == 0) {
		return false
	}
	temp=s.toUpperCase()
	temp=temp.charAt(0)
	if (temp < "A" ) {
		return false
	}
	if (temp > "Z" ) {
		return false
	}	
	return true
}

// return true if 0-9

function isDigit(s) {
	var temp=s
	if (s.length == 0) {
		return false
	}
	temp=temp.charAt(0)
	if (temp < "0" ) {
		return false
	}
	if (temp > "9" ) {
		return false
	}	
	return true
}

// This function strips off leading spaces from a string

function ltrim(s) {
	var RetVal=s
	if (RetVal.length > 0) {
		while (RetVal.charAt(0) == " ") {
			RetVal=RetVal.substr(1)
		}
	}
	return RetVal
}


// strip off trailing spaces

function trim(s) {
	var RetVal=s
	if (RetVal.length > 0) {
		while (RetVal.charAt(RetVal.length-1) == " ") {
			RetVal=RetVal.substr(0,RetVal.length-1)
		}
	}
	return RetVal
}

// Turn string into titlecase. Respect McX.

function titlecase(s) {
	var temp1, temp2, temp3, cWord="", cNew=""
	s=s+"."
	for (var i=0; i < s.length; i++) {
		if (isAlpha(s.charAt(i))) {
			cWord=cWord+s.charAt(i)
		} else if (cWord.length > 0 ) {
			if (cWord.length == 1) {
				cWord = cWord.toUpperCase()
			} else {
				temp1 = cWord.charAt(0)
				temp2 = cWord.substr(1)
				cWord = temp1.toUpperCase()+temp2.toLowerCase()
				temp1 = cWord.substr(0,2)
				if (temp1.toUpperCase() == "MC" && cWord.length>3) {
					temp2=cWord.charAt(2)
					temp3=cWord.substr(3)
					cWord=temp1+temp2.toUpperCase()+temp3
				}
			}
			cNew=cNew+cWord
			cWord=""
			cNew=cNew+s.charAt(i)
		} else {
			cNew=cNew+s.charAt(i)
		}
	}
	return cNew.substr(0,cNew.length-1)
}

// Name case - respect function words in a name, such as de; la; ...

function namecase(s) {
	var temp;
	s=titlecase(s);
	for (var i=0; i < namex.length; i++) {
		temp=s.substr(0,namex[i].length+1);
		if (temp.toUpperCase()==namex[i].toUpperCase()+" ") {
			s=namex[i]+s.substr(namex[i].length);
		}
	}
	return s;
}

// format a UK phone number with spaces and brackets

function phoneformat(s) {
	var nDigCount=0, nStrCount=0, cNewStr="", cFormatType="", temp1, temp2
	s=trim(ltrim(s))
	while (nStrCount < s.length ) {
		temp1=s.substr(nStrCount,1)
		if (isAlpha(temp1) || temp1 == "+") {
			cNewStr=cNewStr+s.substr(nStrCount)
			nStrCount=s.length-1
		} else {
			if (isDigit(temp1)) {
				cNewStr=cNewStr+temp1
				nDigCount=nDigCount+1
			}
		}
		++nStrCount
	}
	s=cNewStr
	if (nDigCount==10 && s.substr(0,2)!="01" && s.substr(0,2)!="02") {
		cFormatType="46"
	} else if (nDigCount==11 && s.substr(0,2)=="08") {
		cFormatType="434"
	} else if (nDigCount==11 && s.substr(0,2)=="07") {
		cFormatType="56"
	} else if (nDigCount==11 && s.substr(0,2)=="02") {
		cFormatType="344"
	} else if (nDigCount==11 && (s.substr(0,3)=="011" || (s.substr(0,2)=="01" && s.substr(3,1)=="1"))) {
		cFormatType="434"
	} else if (nDigCount==11 && s.substr(0,2)=="01") {
		cFormatType="56"
	}
	switch (cFormatType) {
		case "46":
			s="("+cNewStr.substr(0,4)+") "+cNewStr.substr(4,6)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "434":
			s="("+cNewStr.substr(0,4)+") "+cNewStr.substr(4,3)+" "+cNewStr.substr(7,4)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "344":			
			s="("+cNewStr.substr(0,3)+") "+cNewStr.substr(3,4)+" "+cNewStr.substr(7,4)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "56":			
			s="("+cNewStr.substr(0,5)+") "+cNewStr.substr(5,6)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
	}
	return s
}

// format UK postcode with spaces. Correct O to 0.

function pcodeformat(s) {
	s=s.toUpperCase()
	var temp1=chrtran(chrtran(s,".","")," ","")
	var temp2=""
	if (temp1.substr((temp1.length)-3,1)=="O") {
		temp1=temp1.substr(0,(temp1.length)-3)+"0"+temp1.substr((temp1.length)-2,2)
	}
	for (var i=0; i<temp1.length; i++) {
		if (isAlpha(temp1.substr(i,1))) {
			temp2=temp2+"X"
		} else if (isDigit(temp1.substr(i,1))) {
			temp2=temp2+"9"
		} else {
			temp2=temp2+"?"
		}
	}
	switch (temp2) {
		case "XX99XX":
		case "X9X9XX":
		case "X999XX":
			s=temp1.substr(0,3)+" "+temp1.substr(3)
			break;
		case "XX999XX":
		case "XX9X9XX":
			s=temp1.substr(0,4)+" "+temp1.substr(4)
			break;
		case "X99XX":
			s=temp1.substr(0,2)+" "+temp1.substr(2)
			break;
		default:
			break;
	}
	return s
}

// format email address with capitalisation; remove spaces, etc.

function emailformat(s) {
	var cLeft="", cRight=""
	if (s.indexOf("@")>=0) {
		if (s.indexOf("@")>0) {
			cLeft=s.substr(0,s.indexOf("@"))
		}
		if (s.indexOf("@")<s.length-1) {
			cRight=s.substr(s.indexOf("@")+1)
		}
		if (cLeft == cLeft.toUpperCase()) {
			cLeft=cLeft.toLowerCase()
		}
		cRight=cRight.toLowerCase()
		s=cLeft+"@"+cRight
	}
	
	if (s.length > 0) {
		while (s.charAt(s.length-1) == "." || s.charAt(s.length-1) == ",") {
			s=s.substr(0,s.length-1)
		}
	}

	s=ltrim(s)
	
	if (!validateemail(s) && validateemail(chrtran(s," ",""))) {
		s=chrtran(s," ","")
	}

	if (!validateemail(s) && validateemail(chrtran(s,",","."))) {
		s=chrtran(s,",",".")
	}

	if (!validateemail(s) && validateemail(chrtran(chrtran(s," ",""),",","."))) {
		s=chrtran(chrtran(s," ",""),",",".")
	}
	return ltrim(s)
}

// standard function to translate a character string

function chrtran(s,a,b) {
	var RetVal=""
	for (var i=0; i<s.length; i++) {
		if (s.substr(i,1) == a) {
			RetVal=RetVal+b
		} else {
			RetVal=RetVal+s.substr(i,1)
		}
	}
	return RetVal
}

// general function to format a field based on formattype property

function formatfield(fld) {
	var RetVal=fld.value
	switch (fld.formattype) {
		case "lower":
			RetVal=ltrim(RetVal);
			RetVal=RetVal.toLowerCase();
			break;
		case "upper":
			RetVal=ltrim(RetVal);
			RetVal=RetVal.toUpperCase();
			break;
		case "title":
			RetVal=ltrim(RetVal);
			RetVal=titlecase(RetVal);
			break;
		case "name":
			RetVal=ltrim(RetVal);
			RetVal=namecase(RetVal);
			break;
		case "phone":
			RetVal=phoneformat(RetVal);
			break;
		case "number":
			RetVal=numberformat(RetVal);
			break;
		case "money":
			RetVal=currencyformat(RetVal);
			break;
		case "years":
			RetVal=yearsformat(RetVal);
			break;
		case "email":
			RetVal=emailformat(RetVal);
			break;
		case "pcode":
			RetVal=pcodeformat(RetVal);
			break;
	}
	return RetVal;
}

// convert a recognised alternative country name to the proper one

function countryconvert(s) {
	if (typeof(countrychange[s.toUpperCase()]) != "undefined") {
		s=countrychange[s.toUpperCase()]
	}
	return s.toUpperCase()
}

// check that a country is correct/alternative. Return the correct version

function validcountry(s) {
	if (typeof(countrychange[s.toUpperCase()]) != "undefined") {
		return true
	}
	for (var i=0; i<document.qform.country.length; i++) {
		if (s.toUpperCase()==document.qform.country[i].value.toUpperCase()) {
			return true
		}
	}
	return false	
}	

// check postcode is valid

function validpcode(s) {
	s=s.toUpperCase()
	var temp1=chrtran(chrtran(s,".","")," ","")
	var temp2=""
	if (temp1.substr((temp1.length)-3,1)=="O") {
		temp1=temp1.substr(0,(temp1.length)-3)+"0"+temp1.substr((temp1.length)-2,2)
	}
	for (var i=0; i<temp1.length; i++) {
		if (isAlpha(temp1.substr(i,1))) {
			temp2=temp2+"X"
		} else if (isDigit(temp1.substr(i,1))) {
			temp2=temp2+"9"
		} else {
			temp2=temp2+"?"
		}
	}
	switch (temp2) {
		case "XX99XX":
		case "X9X9XX":
		case "X999XX":
		case "XX999XX":
		case "XX9X9XX":
		case "X99XX":
		case "XX9":
		case "X9X":
		case "X99":
		case "XX9":
		case "XX9":
		case "X9":
			return true
			break;
		default:
			return false
			break;
	}
	return false
}
// Check that a Expiry Data on Credit Card 

function checkccdate(frm) {
if (checkcard(frm))
{
d=new Date()
dMonth=d.getMonth()
dMonth+=1
dYear=d.getYear()
dYear-=2000
var exmonth=Number(frm.ccexp1.options[frm.ccexp1.selectedIndex].value)
var exyear=Number(frm.ccexp2.options[frm.ccexp2.selectedIndex].value)
if (exyear<dYear) {
        alert('Credit Card has Expired Year')
        frm.ccexp2.focus()
        return false
        }

if (exyear == dYear && exmonth<dMonth) {
        alert('Credit Card has Expired Month')
        frm.ccexp1.focus()
        return false
        }

}
else
{
return false
}
return true
}
// Check that a Credit card is a Valid Number

function checkcard(frm) {
        var cc1=0
        var cc2=""
        var cc3=0
        var ccsum=0
        var add=0
        cc=ltrim(trim(frm.ccno.value))
        if (cc.length==0) {
        alert('Please Enter a Card Number')
        frm.ccno.focus()
        return false
        }
        cc1=cc.length
        cc1=cc1-1
        while (cc1>=0) {
        if (cc.substr(cc1,1)>="0" && cc.substr(cc1,1)<="9") {
        cc2=cc2+cc.substr(cc1,1)
        }
        cc1=cc1-1
        }
        cc1=0
        while (cc1<cc2.length) {
                                if (cc3==1) {
                                             if (cc2.substr(cc1,1)>="5")
                                                 {
                                                  add = Number(cc2.substr(cc1,1))*2 -9;
                                                  }
                                              else
                                                  {
                                                  add = Number(cc2.substr(cc1,1)) *2;
                                                  }
                                              }
                                              else
                                              {
                                                  add = Number(cc2.substr(cc1,1));
                                              }
                ccsum=ccsum+add;
                if (cc3==1) {
                cc3=0;
                } else {
                cc3=1;
                }

                cc1=cc1+1;
                }
        while (ccsum>0)
        {
        ccsum=ccsum-10
        }
        if (ccsum<0)
        {
        alert ('Your Card number is invalid')
        frm.ccno.focus()
        return false
        }

        if (cc.length>16) {
         if (checkcardtype(frm,"SWIT")) {
         frm.cctype.options.value="SWIT"
         return true
         }
        else {
        alert ('We do not accept Switch Cards \nPlease use another type of Credit Card')
        frm.ccno.focus()
        return false
         }
         }

        if (cc.length==15) {
         if (checkcardtype(frm,"AMEX")) {
         frm.cctype.options.value="AMEX"
         return true
         }
        else {
        alert ('We do not accept American Express \nPlease use another type of Credit Card')
        frm.ccno.focus()
        return false
         }
         }
         if (cc.substr(0,1)=="4") {
         if (checkcardtype(frm,"VISA")) {
         frm.cctype.options.value="VISA"
         return true
         }
        else {
        alert ('We do not accept VISA Cards \nPlease use another type of Credit Card')
        frm.ccno.focus()
        return false
         }
         }

         if (cc.substr(0,1)=="5") {
         if (checkcardtype(frm,"MAST")) {
         frm.cctype.options.value="MAST"
         return true
         }
        else {
        alert ('We do not accept MasterCard \nPlease use another type of Credit Card')
        frm.ccno.focus()
        return false
         }
         }

return true
}
//alert(frm.cctype.options.length)
         //alert(frm.cctype.options[0].value)
         //alert(frm.cctype.options[1].value)
         //alert(frm.cctype.options[2].value)
         //alert(frm.cctype.options[3].value)

function checkcardtype(frm,req) {
        var y
        for (var j=0; j<frm.cctype.options.length; j++) {
                y=frm.cctype.options[j]
                if (y.value==req) {
                return true
                        }
        }
      return false
}

// reformat address based on set rules - see the text line in each rule

function addformat(frm) {
	var add1=trim(ltrim(frm.add1.value))
	var add2=trim(ltrim(frm.add2.value))
	var town=trim(ltrim(frm.town.value))
	var county=trim(ltrim(frm.county.value))
	var pcode=trim(ltrim(frm.pcode.value))
	var country=trim(ltrim(frm.country[frm.country.selectedIndex].value))
 	var o=""
	
 	if (add1.toUpperCase()==add2.toUpperCase() && add1.length) {
		o=o+"removing address line 2 (matches add1)\n"
		add2=""
	}

	if (countryconvert(town.toUpperCase())==country.toUpperCase() && town.length) {
		o=o+"removing town - matches country\n"
		town=""
	}
	
	if (add2.toUpperCase()==town.toUpperCase() && add2.length) {
		o=o+"removing address line 2 (matches town)\n"
		add2=""
	}
	
	if (countryconvert(county.toUpperCase())==country.toUpperCase() && county.length) {
		o=o+"removing county - matches country\n"
		county=""
	}

	if (county.toUpperCase()==town.toUpperCase() && county.length) {
		o=o+"removing county - matches town\n"
		county=""
	}
	
	if (countryconvert(pcode.toUpperCase())==country.toUpperCase() && pcode.length) {
		o=o+"removing pcode - matches country\n"
		pcode=""
	}

	if (pcode.toUpperCase()==county.toUpperCase() && pcode.length) {
		o=o+"removing county - matches pcode\n"
		county=""
	}

	
	// if the county (or town if county is blank) is a valid country name,
	// and the country has not been set,
	// or recognised abbreviation or foreign spelling, then blank it out and
	// set the country appropriately

	if (county.length && !country.length && validcountry(county)) {
		country=countryconvert(county)
		county=""
		o=o+"County moved to country\n"
	}
	
	if (pcode.length && !country.length && validcountry(pcode)) {
		country=countryconvert(pcode)
		pcode=""
		o=o+"Postcode moved to country\n"
	}
	
	if (town.length && !country.length && validcountry(town)) {
		country=countryconvert(town)
		town=""
		o=o+"Town moved to country\n"
	}

	
	// If the county has a valid-looking UK postcode in it, and the postcode
	// field is blank, move the postcode
	
	if (county.length && !pcode.length && !country.length && validpcode(county)) {
		pcode=pcodeformat(county)
		county=""
		o=o+"County moved to postcode\n"
	}
	
	if (town.length && !pcode.length && !country.length && validpcode(town)) {
		pcode=pcodeformat(town)
		town=""
		o=o+"Town moved to postcode\n"
	}
	
	if (add2.length && !town.length) {
		o=o+"Moving add2 to town\n"
		town=add2
		add2=""	
	}

	if (county.toUpperCase()=="LONDON") {
	
		for (var i=0; i<londonboroughs.length; i++) {
			if (town.toUpperCase()==londonboroughs[i].toUpperCase()) {
				if (add2=="") {
					add2=titlecase(town)
				}
				town="LONDON"
				county=""
				i=londonboroughs.length
				o=o+"Moved London to town\n"
			}
		}				
	
	}
	
	if (frm.add1.value != add1)
		frm.add1.value=add1
	if (frm.add2.value != add2)
		frm.add2.value=add2
	if (frm.town.value != town)
		frm.town.value=town
	if (frm.county.value != county)
		frm.county.value=county
	if (frm.pcode.value != pcode)
		frm.pcode.value=pcode
	
	for (var j=0; j<frm.country.length; j++) {
		if (frm.country.options[j].value==country) {
			frm.country.selectedIndex=j
			j=frm.country.length
		}
	}		

	
//        if (o.length) 
//                alert("Changes made:\n\n"+o)

}


