function test_date_day(val) {
  var pattern = /^[1-9]\d{0,1}$/;

  if( val.match(pattern)==null )
  {
    return false;
  }
  
  return true;
}

// test value for year entry in date
function test_date_year(val) {
  var pattern = /^[1-9]\d{3}$/;

  if( val.match(pattern)==null)
  {
    return false;
  }
  return true;
}

// test number (maximum length is 32 digits)
function test_number(val)
{
  var pattern = /^\d{1,32}$/;

  if( val.match(pattern)==null)
  {
    return false;
  }
  return true;
}

// test email - approximate
function test_email(val)
{

  var pattern = /^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
  if( pattern.test(val) )
  {
    return true;
  }
  return false;

  
}

function test_phone_number(val) {
  var pattern = /[^0-9, ]/;

  if( val.match(pattern)) {
    return false;
  }
  return true;
}

function test_radiobutton(rd) {
	pass = false;	
	for (i=rd.length-1; i > -1; i--) {		
		if (rd[i].checked == true) {
			pass = true;			
			break;			
		}
	}
	return pass;
}

function get_radiobutton(rd) {
	for (i=rd.length-1; i > -1; i--) {		
		if (rd[i].checked == true) {		
			return rd[i].value;
			break;			
		}
	}
	return false;
}

function test_selectbox(sb) {   
	if (sb.options[sb.selectedIndex].value == '' ) {
		return false;
	}
	return true;
}

function test_checkbox(cb) {
	pass = false;
    if (!cb.length) {
        if (cb.checked == true) {
            pass = true;		
        }
    } else { 
    	for (i=cb.length-1; i > -1; i--) {		
    		if (cb[i].checked == true) {
    			pass = true;			
    			break;			
    		}
    	}
    }
	return pass;
}

function get_myelement(obj) {
  if(document.getElementById){ //DOM
    css = document.getElementById(obj).style;
  } else {
    if(document.all){// IE
    	css = document.all[obj].style;
    }
    if(document.layers){//NS
      css = document.layers[obj];
    }
  }
   return css;
}

function get_radiobutton_value(rd) {
	pass = false;
	for (i=rd.length-1; i > -1; i--) {		
		if (rd[i].checked == true) {
			return rd[i].value			
		}
	}	
}

function get_selectbox_value(sb) {
	return sb.options[sb.selectedIndex].value;
}

function get_selectbox_value_title(sb) {
	return sb.options[sb.selectedIndex].text;
}


function get_checkbox_value(cb) {
	for (i=cb.length-1; i > -1; i--) {
		if (cb[i].checked == true) {
			return cb[i].value;
			break;
		}
	}
	return false;
}

function get_checkbox_status(cb, val) {
	for (i=cb.length-1; i > -1; i--) {
		if (cb[i].checked == true && cb[i].value == val) {
			return true;
			break;
		}
	}
	return false;
}

function test_date(y,m,d) { 
	
	if (m.charAt(0) == "0") { 
		m = parseInt(m.charAt(1)) - 1; 
	} else { 
		m = parseInt(m)-1; 
	} 
	
	if (d.charAt(0) == "0") { 
		d = parseInt(d.charAt(1)); 
	} 
	
	var check = new Date(); 
	check.setFullYear(y,m,d); 
	
	mytest = check.getFullYear() + "-" + check.getMonth()+ "-" + check.getDate(); 
	mydate = y+"-"+ m+"-"+d; 
	
	if (mydate != mytest) { 
		return false; 
	} 
	return true; 
} 

function get_myelement_hmtl(obj) {
  if(document.getElementById){ //DOM
    css = document.getElementById(obj);
  } else {
    if(document.all){// IE
    	css = document.all[obj];
    }
    if(document.layers){//NS
      css = document.layers[obj];
    }
  }
   return css;
}

function is_too_young() {
  byear = get_selectbox_value(frm.elements.year);    
  bmonth = get_selectbox_value(frm.elements.month); 
  bday = get_selectbox_value(frm.elements.day);
  if (byear == '' || bday == '' || bmonth == '') {
    return false;
  }
  under14 = false;  
  var myDate=new Date()
  
  if(byear != "" && bmonth != "" && bday != "") {
    if((myDate.getFullYear() -byear) < 13) {
      under14 = true;
    }
    else { 
      if((myDate.getFullYear() -byear) == 13 ) {
        if((myDate.getMonth()+1) < bmonth ) {
          under14 = true;
        }
        else {
          if((myDate.getMonth()+1) == bmonth ) {
            if(myDate.getDate() < bday ) {
              under14 = true;
            }
          }
        }
      }
    }
  }  
  return under14;
}

function is_13_to_17()
{
  byear = get_selectbox_value(frm.elements.year);    
  bmonth = get_selectbox_value(frm.elements.month); 
  bday = get_selectbox_value(frm.elements.day);
  if (byear == '' || bday == '' || bmonth == '') {
    return false;
  }
  result = false;  
  var myDate=new Date()
  
  if(byear != "" && bmonth != "" && bday != "") 
  {
    if((myDate.getFullYear() -byear) > 13 && (myDate.getFullYear() -byear) < 18) 
    {
      result = true;
    }
    else 
    { 
      if((myDate.getFullYear() -byear) == 13 ) 
      {
        if((myDate.getMonth()+1) > bmonth ) 
        {
          result = true;
        }
        else 
        {
          if((myDate.getMonth()+1) == bmonth ) 
          {
            if(myDate.getDate() >= bday ) 
            {
              result = true;
            }
          }
        }
      }
      else if((myDate.getFullYear() -byear) == 18 ) 
      {
        if((myDate.getMonth()+1) < bmonth ) 
        {
          result = true;
        }
        else 
        {
          if((myDate.getMonth()+1) == bmonth ) 
          {
            if((myDate.getDate()) < bday ) 
            {
              result = true;
            }
          }
        }
      }
    }
  }  
  return result;
}

function set_warning(attr) {
     location.hash = 'atitle_' + attr;
     c = get_myelement('title_' + attr);
     c.color="red";
}

function test_complex_phonenumber(country) {
    // Test and convert area code.
    phonenumber = frm.elements.phonenumber.value;
    areacode = frm.elements.areacode.value;
    
    //strip out unwanted characters
    phonenumber = phonenumber.replace(/[^0-9,\,-]*/gi,"");
    areacode = areacode.replace(/[^0-9]*/gi,"");
    
    frm.elements.phonenumber.value = phonenumber;
    frm.elements.areacode.value = areacode;
    
    areacode_count = areacode.match(/[0-9]{1}/g);     
    phonenumber_count = phonenumber.match(/[0-9]{1}/g);
    
    if (!areacode_count) {
        return false;
    }
      
    if (!phonenumber_count) {
        return false;
    }
    
    // CA and US country must be 3 digits, others must just be numbers
    if (country == 'US' || country == 'CA') {
       if (areacode_count.length != 3) {
            return false;
       }    
    } else {
       if (areacode_count.length < 1) {
            return false;
       }  
    }
   
    //Test Phone number 4-11 digits
    if (phonenumber_count.length < 4 || phonenumber_count.length > 11) {
        return false;
    }     
    return true;
}

function telephone_country_code_generator(country) {
    if (country == 'US' || country == 'CA') {
        return 1;
    }
    if (country == 'UK') { return '011'; }    
    if (country == 'ZMB') { return '260'; }    
    if (country == 'ZWE') { return '263'; }    
    if (country == 'YMN') { return '967'; }    
    if (country == 'VZ') { return '58'; }
    if (country == 'VT') { return '84'; }
    if (country == 'VAT') { return '39'; }
    if (country == 'VNT') { return '678'; }
    if (country == 'UZB') { return '998'; }
    if (country == 'URG') { return '598'; }
    if (country == 'URE') { return '380'; }
    if (country == 'UAE') { return '971'; }
    if (country == 'UGD') { return '256'; }
    if (country == 'TRK') { return '1-649'; }
    if (country == 'TRM') { return '993'; }
    if (country == 'TUR') { return '90'; }
    if (country == 'TUN') { return '216'; }
    if (country == 'TRI') { return '1-868'; }
    if (country == 'TON') { return '676'; }
    
    
    if (country == 'AFG') { return '93'; }    
    if (country == 'ALB') { return '355'; }
    if (country == 'ALG') { return '213'; }
    if (country == 'ASA') { return '1-684'; }
    if (country == 'AND') { return '376'; }
    if (country == 'ANG') { return '244'; }
    if (country == 'AGU') { return '1-264'; }
    if (country == 'ANT') { return '1-268'; }    
    if (country == 'AR') { return '54'; }
    if (country == 'AM') { return '374'; }
    if (country == 'AL') { return '61'; }
    if (country == 'AUT') { return '43'; }
    if (country == 'AZ') { return '994'; }
    if (country == 'BHM') { return '1-242'; }
    if (country == 'BAH') { return '973'; }    
    if (country == 'BNG') { return '880'; }
    if (country == 'BBD') { return '1-246'; }
    if (country == 'BLR') { return '375'; }
    if (country == 'BEL') { return '32'; }
    if (country == 'BLZ') { return '501'; }
    if (country == 'BEN') { return '229'; }	 
    if (country == 'BMD') { return '1-441'; } 
    if (country == 'BHT') { return '975'; }	 
    if (country == 'BOL') { return '591'; }	 
    if (country == 'BOT') { return '267'; }	 
    if (country == 'BZ') { return '55'; }	 
    if (country == 'BLG') { return '359'; }	 
    if (country == 'BKF') { return '226'; }	 
    if (country == 'BRN') { return '673'; }	 
    if (country == 'BUR') { return '257'; }	 
    if (country == 'KMP') { return '855'; }	 
    if (country == 'CAM') { return '237'; }
    if (country == 'CV') { return '238'; }	 
    if (country == 'CMN') { return '1-345'; }	 
    if (country == 'CHD') { return '235'; }	 
    if (country == 'CL') { return '56'; } 
    if (country == 'CHN') { return '86'; }	 
    if (country == 'CO') { return '57'; }	 
    if (country == 'CMR') { return '269'; }	 
    if (country == 'CGO') { return '243'; }	 
    if (country == 'CKI') { return '682'; }	 
    if (country == 'CRI') { return '506'; }	 
    if (country == 'CRO') { return '385'; }	 
    if (country == 'CYP') { return '357'; }	 
    if (country == 'CZ') { return '420'; }	 
    if (country == 'DN') { return '45'; }	 
    if (country == 'DJ') { return '253'; }	 
    if (country == 'DOM') { return '1-767'; }	 
    if (country == 'DMR') { return '1-809'; }	 
    if (country == 'EC') { return '593'; }	 
    if (country == 'EGY') { return '20'; }	 
    if (country == 'ELS') { return '503'; }	 
    if (country == 'EGU') { return '240'; }	 
    if (country == 'EST') { return '372'; }	 
    if (country == 'ETH') { return '251'; }	 
    if (country == 'FRI') { return '298'; }	 
    if (country == 'FLK') { return '500'; }	 
    if (country == 'FJI') { return '679'; }	 
    if (country == 'FI') { return '358'; }	 
    if (country == 'FR') { return '33'; }	 
    if (country == 'FG') { return '594'; }	 
    if (country == 'FP') { return '689'; }	 
    if (country == 'GBN') { return '241'; }	 
    if (country == 'GMB') { return '220'; }	 
    if (country == 'GGI') { return '995'; }	 
    if (country == 'DE') { return '49'; }	 
    if (country == 'GHN') { return '233'; }	 
    if (country == 'GBR') { return '350'; }	 
    if (country == 'GRE') { return '30'; }	 
    if (country == 'GRL') { return '299'; }	 
    if (country == 'GUD') { return '590'; }	 
    if (country == 'GUM') { return '1-671'; }	 
    if (country == 'GUT') { return '502'; }
    if (country == 'GUN') { return '224'; }	 
    if (country == 'GUY') { return '592'; }	 
    if (country == 'HAI') { return '509'; }	 
    if (country == 'HON') { return '504'; }	 
    if (country == 'HK') { return '582'; }	 
    if (country == 'HU') { return '36'; }	 
    if (country == 'IC') { return '354'; }	 
    if (country == 'IN') { return '91'; }	 
    if (country == 'NDO') { return '62'; }	 
    if (country == 'IRE') { return '353'; }	 
    if (country == 'ISR') { return '972'; }	 
    if (country == 'IT') { return '39'; }	 
    if (country == 'Jamaica	JAM') { return '1-876'; }	 
    if (country == 'JP') { return '81'; }	 
    if (country == 'KZK') { return '7'; }	 
    if (country == 'KEN') { return '254'; }	 
    if (country == 'KIR') { return '686'; }	 
    if (country == 'KO') { return '82'; }	 
    if (country == 'KUW') { return '965'; }	 
    if (country == 'KYR') { return '996'; }	 
    if (country == 'Laos	LAO') { return '856'; }	 
    if (country == 'LAT') { return '371'; }	 
    if (country == 'LEB') { return '961'; }	 
    if (country == 'LES') { return '266'; }	 
    if (country == 'LIB') { return '231'; }	 
    if (country == 'LIE') { return '423'; }	 
    if (country == 'LIT') { return '370'; }	 
    if (country == 'LU') { return '352'; }	 
    if (country == 'MAC') { return '853'; }	 
    if (country == 'MAD') { return '261'; }	 
    if (country == 'MLW') { return '265'; }	 
    if (country == 'MY') { return '60'; }	 
    if (country == 'MLD') { return '960'; }	 
    if (country == 'MLI') { return '223'; }	 
    if (country == 'MLT') { return '356'; }	 
    if (country == 'MAT') { return '222'; }	 
    if (country == 'MAU') { return '230'; }	 
    if (country == 'MX') { return '52'; } 
    if (country == 'MIC') { return '691'; }	 
    if (country == 'MOL') { return '373'; }	 
    if (country == 'MON') { return '377'; }	 
    if (country == 'MNG') { return '976'; }	 
    if (country == 'MGR') { return '382'; }	 
    if (country == 'MNT') { return '1-664'; }	 
    if (country == 'MRC') { return '212'; }	 
    if (country == 'MZQ') { return '258'; }	 
    if (country == 'NMB') { return '264'; }	 
    if (country == 'NAU') { return '674'; }	 
    if (country == 'NPL') { return '977'; }		
    if (country == 'NL') { return '31'; }	 
    if (country == 'NLA') { return '599'; }	 
    if (country == 'NWC') { return '687'; } 
    if (country == 'NWZ') { return '64'; }	 
    if (country == 'NIC') { return '505'; }	 
    if (country == 'NIGR') { return '227'; }	 
    if (country == 'NGR') { return '234'; }	 
    if (country == 'NO') { return '47'; }	 
    if (country == 'OM') { return '968'; }	 
    if (country == 'PK') { return '92'; }	 
    if (country == 'PN') { return '507'; }	 
    if (country == 'PNG') { return '675'; }	 
    if (country == 'PAR') { return '595'; }	 
    if (country == 'PU') { return '51'; }	 
    if (country == 'PH') { return '63'; }	 
    if (country == 'PO') { return '48'; }	 
    if (country == 'PT') { return '351'; }	 
    if (country == 'PR') { return '1-787'; }	 
    if (country == 'QA') { return '974'; } 
    if (country == 'RO') { return '40'; }	 
    if (country == 'RU') { return '7'; }	 
    if (country == 'RW') { return '250'; }	 
    if (country == 'SAM') { return '685'; }	 
    if (country == 'SM') { return '966'; }	 
    if (country == 'SNG') { return '221'; }	 
    if (country == 'SER') { return '381'; }	 
    if (country == 'SEY') { return '248'; }	 
    if (country == 'SRL') { return '232'; }	 
    if (country == 'SI') { return '65'; }	 
    if (country == 'SVR') { return '421'; }	 
    if (country == 'SLV') { return '386'; }	 
    if (country == 'SLM') { return '677'; }	 
    if (country == 'SOM') { return '252'; }	 
    if (country == 'RSA') { return '27'; }	 
    if (country == 'ESP') { return '34'; }	 
    if (country == 'SRI') { return '94'; }	 
    if (country == 'STH') { return '290'; }	 
    if (country == 'STK') { return '1-869'; }	 
    if (country == 'STL') { return '1-758'; }	 
    if (country == 'STV') { return '1-784'; }	 
    if (country == 'SUR') { return '597'; }	 
    if (country == 'SZ') { return '268'; }	 
    if (country == 'SW') { return '46'; }	 
    if (country == 'SCH') { return '41'; }	 
    if (country == 'TWN') { return '886'; }	 
    if (country == 'TAJ') { return '992'; }	 
    if (country == 'TAN') { return '255'; }	 
    if (country == 'THA') { return '66'; }	 
    if (country == 'TG') { return '676'; }	 
    if (country == 'VI') { return '1-340'; }
    if (country == 'MQ') { return '1-808'; }	
    if (country == 'MK') { return '389'; }
    if (country == 'JDN') { return '962'; }	 
    if (country == 'IRN') { return '98'; }	 
    if (country == 'CUB') { return '53'; }	 
    if (country == 'SYR') { return '963'; }	 
    if (country == 'ARU') { return '267'; }	 
    if (country == 'SUD') { return '249'; }
    
    //if (country == 'Canary Islands	CNI') { return ''; }	 
    //if (country == 'Guernsey Isle	GUR') { return ''; }	 
    //if (country == 'Ivory Coast	IVC') { return ''; }	
    //if (country == 'La Reunion	LAR') { return ''; }	
    //if (country == 'New Hebrides	NWH') { return ''; }	
    //if (country == 'Zaire	ZAI') { return ''; }	 
    //if (country == 'Myanmar	MYN') { return ''; }	 	
    //if (country == 'Palmyra Atoll	LQ') { return ''; }	 
    //if (country == 'Navassa Island	BQ') { return ''; }	 
    //if (country == 'Kingman Reef	KQ') { return ''; }	 
    //if (country == 'Baker, Howland, and Jarvis Islands	FQ') { return ''; } 
    //if (country == 'Johnston Atoll	JQ') { return ''; }	 
    //if (country == 'Wake Island	WQ') { return ''; }	 
    //if (country == 'Northern Mariana Islands	CQ') { return ''; }	 
    //if (country == 'Isle of Man	MAN') { return ''; }	
    //if (country == 'Libyan Arab Jamahiriya	LBY') { return ''; }
        
    return '---';

}
