function Inint_AJAX() {
	try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
	try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE	
	alert("XMLHttpRequest not supported");
	return null;
}
var xmlhttp = Inint_AJAX();
function isExisting(obj){
  if (document.getElementById(obj)) { return true; } else { return false; }
} 
function xss_ajax(url) {  	 	
    var script_id = null;  
    var script = document.createElement('script');  
    script.setAttribute('type', 'text/javascript');  
    script.setAttribute('src', url);  
    script.setAttribute('id', 'script_id');  
    script_id = document.getElementById('script_id');		 
    if(script_id){  
	    document.getElementsByTagName('head')[0].removeChild(script_id);  
    }  
    document.getElementsByTagName('head')[0].appendChild(script);  
}
function msg(data){
	if(data[0]<1700){
		if(isExisting('main_index')) document.getElementById('main_index').innerHTML=data['main_page_th'];
		if(isExisting('register_header')) document.getElementById('register_header').innerHTML=data['register_header_th'];
		if(isExisting('main_area')) document.getElementById('main_area').innerHTML=data['register_form_th'];
	}else{		
		if(isExisting('main_index')) document.getElementById('main_index').innerHTML=data['main_page_th'];
		if(isExisting('main_area')) document.getElementById('main_area').innerHTML=data['full_record_th'];		
	}
	//setInterval(xss_ajax("http://goto.canon.co.th/photomarathon2009/thailand/register_count.php"),1);
}
function callback(data) { 	
	var txt = '\n';  
    for(var key in data) { 
		txt += data[key];
	    txt += "\n";  
    }	
  	alert(txt);  
} 
// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}
function dateWithin(beginDate,endDate,checkDate) {
	var b,e,c;
    b = Date.parse(beginDate);
    e = Date.parse(endDate);
    c = Date.parse(checkDate);
    if((c <= e && c >= b)) {
		return true;
    }
    return false;
}
function ChkShowDate(date_start,date_end){	
	var time=new Date();
	var month=time.getMonth() + 1;
	var date=time.getDate();
	var year=time.getFullYear();
	var current_date=month + '/' + date + '/' + year;
	/*if(dateWithin(date_start,date_end,current_date)==true){
		xss_ajax("http://goto.canon.co.th/photomarathon2009/thailand/register_count.php");
		document.getElementById('show1').style.display="none";
		document.getElementById('show2').style.display="inline";		
	}else{
		document.getElementById('show1').style.display="inline";
		document.getElementById('show2').style.display="none";
	}*/
	xss_ajax("http://goto.canon.co.th/photomarathon2009/thailand/register_count.php");
}
function ChkShowDate2(date_start,date_end){	
	var time=new Date();
	var month=time.getMonth() + 1;
	var date=time.getDate();
	var year=time.getFullYear();
	var current_date=month + '/' + date + '/' + year;	
	xss_ajax("http://goto.canon.co.th/photomarathon2009/thailand/register_count.php");
	var trv;
	xmlhttp.open("GET","http://goto.canon.co.th/photomarathon2009/thailand/register_count.php",true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4 && xmlhttp.status==200){
			trv=xmlhttp.responseText;
			alert(trv);
		}	
	}
	xmlhttp.send(null);	
	if(dateWithin(date_start,date_end,current_date)==true){		
		if(isExisting('main_area')) document.getElementById('main_area').innerHTML=data['register_form_th'];		
	}else{
		if(isExisting('main_area')) document.getElementById('main_area').innerHTML=data['full_record_th'];
	}	
}
function checkPunc(num) {
    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }
    return false;
}
function CalcKeyCode(aChar) {
	var character = aChar.substring(0,1);
	var code = aChar.charCodeAt(0);
	return code;
}
function checkNumber(val) {
	var strPass = document.getElementById(val);
	var strLength = strPass.value.length;
	var lchar = strPass.value.charAt((strLength) - 1);
	var cCode = CalcKeyCode(lchar);
	if ((cCode < 48 || cCode > 57) && cCode != 46) {
		var myNumber = strPass.value.substring(0, (strLength) - 1);
		strPass.value = myNumber;
	}
	return false;
}

function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co.th)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {		
		alert("รูปแบบอีเมลไม่ถูกต้อง");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("รูปแบบอีเมลไม่ถูกต้อง");
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("รูปแบบอีเมลไม่ถูกต้อง");
			return false;
   		}
	}
	if (user.match(userPat)==null) {
		alert("รูปแบบอีเมลไม่ถูกต้อง");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("รูปแบบอีเมลไม่ถูกต้อง");
				return false;
		   }
		}
		//return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("รูปแบบอีเมลไม่ถูกต้อง");
			return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("รูปแบบอีเมลไม่ถูกต้อง");
		return false;
	}

	if (len<2) {
		alert("รูปแบบอีเมลไม่ถูกต้อง");
		return false;
	}
	//return true;		
}
function reload(){
	var msg="กรุณาระบุ : ";
	if(document.getElementById('name_th').value == ''){
		alert(msg+"ชื่อ-นามสกุล(ตามบัตรประจำตัวประชาชน)");
		document.getElementById('name_th').focus();
		return false;
	}
	if(document.getElementById('name_en').value == ''){
		alert(msg+"ชื่อ-นามสกุล(ภาษาอังกฤษ)");
		document.getElementById('name_en').focus();
		return false;
	}
	if(document.getElementById('id_card').value == ''){
		alert(msg+"เลขที่บัตรประจำตัวประชาชน");
		document.getElementById('id_card').focus();
		return false;
	}
	if(document.getElementById('id_card').value.length < 13){
		alert("เลขที่บัตรประจำตัวประชาชนสั้นเกินไป");
		document.getElementById('id_card').focus();
		return false;
	}
	if(document.getElementById('contact1').value == ''){
		alert(msg+"เบอร์ติดต่อ");
		document.getElementById('contact1').focus();
		return false;
	}
	if(document.getElementById('email').value == ''){
		alert(msg+"ที่อยู่อีเมล์");
		document.getElementById('email').focus();
		return false;
	}
	if(emailCheck(document.getElementById('email').value) == false){
		document.getElementById('email').focus();
		return false;	
	}
	if(document.getElementById('con_email').value == ''){
		alert(msg+"ยืนยันที่อยู่อีเมล์");
		document.getElementById('con_email').focus();
		return false;
	}
	if(emailCheck(document.getElementById('con_email').value) == false){
		document.getElementById('con_email').focus();
		return false;	
	}
	if(document.getElementById('email').value != document.getElementById('con_email').value){
		alert("ที่อยู่อีเมล์ไม่ตรงกัน");
		document.getElementById('con_email').focus();
		return false;
	}
	if(document.getElementById('address').value == ''){
		alert(msg+"ที่อยู่");
		document.getElementById('address').focus();
		return false;
	}
	if(document.getElementById('dob').value == ''){
		alert(msg+"วันเดือนปีเกิด");
		document.getElementById('dob').focus();
		return false;
	}
	if(document.getElementById('brand[0]').checked==false && document.getElementById('brand[1]').checked==false){
		alert(msg+"ประเภทของกล้อง");
		document.getElementById('brand[0]').focus();
		return false;
	}
	if(document.getElementById('model').value == ''){
		alert(msg+"รุ่นกล้อง");
		document.getElementById('model').focus();
		return false;
	}
	if(document.getElementById('terms').checked==false){
		alert("กรุณายอมรับ : ข้อตกลงและเงื่อนไข");
		document.getElementById('terms').focus();
		return false;
	}
}
