// JavaScript Document
function checkPhoneField(phone){
    num=phone
    phone = phone.replace(/[\(\)\- ,\. ]/g,"");

    if (/\D/.test(phone)){
         alert('error:Invalid Characters')
         return num
    }

    if (phone.length > 11) return phone

    if (phone.length == 11)
      if ( /^1/.test(phone)) return phone.replace(/\d(\d{3})(\d{3})(\d{4})/,'1 ('+'$1'+') '+'$2'+'-'+'$3')
      else {
        alert('error:Invalid Long Distance Digit')
        return num
      }

    if (phone.length == 10) return phone.replace(/(\d{3})(\d{3})(\d{4})/,'('+'$1'+') '+'$2'+'-'+'$3')

    if (phone.length == 7) return phone.replace(/(\d{3})(\d{4})/,'$1'+'-'+'$2')

    if (phone.length > 1) {
      alert('error:Invalid number of digits')
      return num
    }

    return phone
}
function checkPostalCode(pc){
	text=pc;
	pc = pc.replace(/[\(\)\- ,\.]/g,"");
	pc = pc.toUpperCase();
	
	//test for valid canadian postal code
	if(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/.test(pc)) return pc.replace(/([A-Z][0-9][A-Z])([0-9][A-Z][0-9])/,'$1 $2');
	
	//test for valid US zip or zip + 4 digits
	else if(/^\d{5}$/.test(pc)) return pc;
	
	else if(/^\d{9}$/.test(pc)) return pc.replace(/(\d{5})(\d{4})/,'$1'+'-'+'$2');
	
	//the format matches neither canadian nor USA
	else if(pc.length > 0){
			alert('Please enter the postal/zip code in one of these formats:\n\nCanada:\nA#A #A#\n\nUSA:\n##### or #####-####');
			return text;
	}
	
	return pc;
}
