	$(function() {
		$('#register').validate({
			rules: {
				cdisplayname: {
					required: true,
					chkFormatUsername: true,
					rangelength: [3, 10],
					remote: {
						url: "check.php?op=chkDupUsersByUsername",
						type: "get",
						data: {
							  serviceID: function() {
								return $("#serviceID").val();
							  }
						}
					}
				},
				email: {
					required: true,
					email: true,
					remote: {
						url: "check.php?op=chkDupUsersByEmail",
						type: "get",
						data: {
							  serviceID: function() {
								return $("#serviceID").val();
							  }
						}
					}		
				},
				cpassword2: {
					required: true,
					equalTo: "#cpassword1"
				},
				cname: {
					required: true
				},
				csurname: {
					required: true
				},
				cbirth_year: {
					required: true,
					remote: {	
						url: "check.php?op=checkDate",
						type: "get",
						data: {
							  cbirth_month: function() {
								return $("#cbirth_month").val();
							  },
							  cbirth_date: function() {
								return $("#cbirth_date").val();
							  }
						}
					}	
				},
				caddress: {
					required: true
				},
				cpostcode:{
					required: true,
					digits:true,
					maxlength: 5,
					minlength: 5
				},
				cphone: {
					required: true
				},
				cmobile: {
					required: true
				},
				cposition: {
					required: true
				},
				cjobtype: {
					required: true
				},
				cincome: {
					required: true
				},
				cwebsite: {
					required: true
				},
				cim: {
					required: true
				},
				cjobcategory: {
					required: true
				},
				cpersonal: {
					required: true
				},
				creligion: {
					required: true
				},
				ccompany: {
					required: true
				},
				cprivacy: {
					required: true
				},
				code:{
					required: true,
					maxlength: 4,
					minlength: 4					
				}
			},
			messages: {
				cdisplayname: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					remote: $.format("<strong>{0}</strong> มีผู้ใช้งานในระบบแล้ว")
				},
				email: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					email: "กรุณาใส่ข้อมูลให้ถูกต้องด้วยค่ะ",
					remote: $.format("<strong>{0}</strong> มีผู้ใช้งานในระบบแล้ว")
				},
				cpassword2:{
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					equalTo: "ไม่ตรงกับรหัสผ่านด้านบน"
				},
				cname: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				csurname: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cbirth_year: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					remote: $.format("โปรดใส่ข้อมูลให้ถูกต้อง")
				},
				caddress: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cpostcode:{
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					digits: "โปรดใส่ข้อมูลให้ถูกต้อง",
					maxlength: "โปรดใส่ข้อมูลให้ถูกต้อง",
					minlength: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cphone: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cmobile: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cposition: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cjobtype: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cincome: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cwebsite: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cim: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cjobcategory: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cpersonal: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				creligion: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				ccompany: {
					required: "โปรดใส่ข้อมูลให้ถูกต้อง"
				},
				cprivacy: {
					required: "กรุณาเลือกยอมรับนโยบายการใช้งานสมาชิกด้วย"
				},
				code:{
					required: "โปรดใส่ข้อมูลให้ถูกต้อง",
					maxlength: "โปรดใส่ข้อมูลให้ถูกต้อง",
					minlength: "โปรดใส่ข้อมูลให้ถูกต้อง"
				}
			},
			errorPlacement: function(error, element) {
				if (element.is(':radio')){
					error.appendTo(element.next());
					//error.appendTo(element.parent().next());
					//error.appendTo( element.parent().parent().next() );
				}else if (element.attr("id") == "cbirth_date"){
					error.appendTo(element.next().next().next());
				}else if (element.attr("id") == "cbirth_month"){
					error.appendTo(element.next().next());
				}else if (element.is(':checkbox')){
					error.appendTo(element.next().next());
				}else{
					error.appendTo(element.next());
				}
			}
		});

	});


jQuery.validator.addMethod("chkFormatUsername", function(value, element, param) {
					 var regexp = /^([a-zA-Z0-9])+([a-zA-Z0-9_.-])+([a-zA-Z0-9])+$/;
                      if(regexp.test(value)){
						 return true;
					  }else{
					        return false;
					  }
                }, "โปรดใส่ข้อมูลให้ถูกต้อง"); 


function parseUri(sourceUri){
				var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"],
				uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri),
				uri = {};
				
				for(var i = 0; i < 10; i++){
								uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
				}
								
				/* Always end directoryPath with a trailing backslash if a path was present in the source URI
				Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key */
				if(uri.directoryPath.length > 0){
								uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
				}
				return uri;
}

var this_url = parseUri(window.location.href+'');
var tmp1 = this_url['domain'].split('.');
subdomain = tmp1[tmp1.length-3];
if(subdomain == 'WWW' || subdomain == 'www'){
				subdomain = '';
}else{
				subdomain = subdomain + '.' ;                    
}
if(typeof page == 'undefined') {
				var tmp2 = this_url['path'].split('/');
												page = subdomain + 'posttoday';

				if(tmp2.length > 1){
								for(var i=0; i<3; i++){
												if( tmp2[i] != '' ){
																if(typeof tmp2[i] != 'undefined')
																page  += ' >> ' + tmp2[i];
												}
								}
				}else{
								page = page + ' >> index';
				}
}

__th_page = page.replace('.php','');

function magicImages(sPathNames,ImageName) {
	document.images[ImageName].src=sPathNames;
}

function checkExt(value,exts,extExcept,spanID,fileName,imgName){
var isNN = navigator.appName == "Netscape";
var act = false;
var exts = exts.toLowerCase();
var extExcept = extExcept.toLowerCase();
var pathValue = value.toLowerCase();
	
	if(exts==''){
	alert("Undefine file extension\n\n");
		magicImages('images/none.gif',imgName);
		document.getElementById(fileName).value='';
		document.getElementById(spanID).innerHTML="<input type=\"file\" name=\"" + fileName + "\" id=\"" + fileName + "\" onChange=\"checkExt(this.value,'pjpeg|jpeg|jpg|gif','bmp|png','" + spanID + "','" + fileName + "','" + imgName + "')\" class=\"input_file\" />";
        return false;
	}
    if(pathValue=="") {
		magicImages('images/none.gif',imgName);
		document.getElementById(fileName).value='';
		document.getElementById(spanID).innerHTML="<input type=\"file\" name=\"" + fileName + "\" id=\"" + fileName + "\" onChange=\"checkExt(this.value,'pjpeg|jpeg|jpg|gif','bmp|png','" + spanID + "','" + fileName + "','" + imgName + "')\" class=\"input_file\" />";
		return false;
	} else {
		if(!pathValue.match(exts)){
			alert("กรุณาอัพโหลดไฟล์ใหม่ในรูปแบบ.JPG , .GIF , .PNG เท่านั้น");
			//alert("ไม่อนุญาติไฟล์นามสกุล นี้ค่ะ: \"" + pathValue + "\"\nอนุญาติให้ใช้ไฟล์นามสกุล เหล่านี้ค่ะ: "+exts.replace(/\|/g,',')+" \n\n");
			magicImages('images/none.gif',imgName);
			document.getElementById(fileName).value='';
			//document.getElementById(spanID).innerHTML="<input type=\"file\" name=\"" + fileName + "\" id=\"" + fileName + "\" onChange=\"checkExt(this.value,'pjpeg|jpeg|jpg|gif','bmp|png','" + spanID + "','" + fileName + "','" + imgName + "')\" class=\"input_file\" />";
			return false;
		}
		if(pathValue.match(extExcept)){
			alert("กรุณาอัพโหลดไฟล์ใหม่ในรูปแบบ.JPG , .GIF , .PNG เท่านั้น");
			//alert("ไม่อนุญาติไฟล์นามสกุล นี้ค่ะ: \"" + pathValue + "\"\nไม่อนุญาติให้ใช้ไฟล์นามสกุล เหล่านี้ค่ะ: "+extExcept.replace(/\|/g,',')+" \n\n");
			magicImages('images/none.gif',imgName);
			document.getElementById(fileName).value='';
			//document.getElementById(spanID).innerHTML="<input type=\"file\" name=\"" + fileName + "\" id=\"" + fileName + "\" onChange=\"checkExt(this.value,'pjpeg|jpeg|jpg|gif','bmp|png','" + spanID + "','" + fileName + "','" + imgName + "')\" class=\"input_file\" />";
			return false;
		}
		if(isNN){
		  var nImg = document.getElementById('cavatar');
			if( nImg.files[0].fileSize >= 200000 ){
				alert("ขนาดภาพผิดมาตราฐานขอให้อัพโหลดใหม่ที่ไม่เกิน 200 K \r\n");
			}else{
			   return true;
			}
		}else{
			magicImages(pathValue,imgName);
			return true;
		}
	}
}

function getSize(){
var isNN = navigator.appName == "Netscape";
	if(isNN){
	}else //IE
		{
		var y = document.images;
		var errorChk = false;
		var msg_error = "";
		var imglength1 = 0;
		var chkValidate = "";
			for (i=0;i<y.length;i++){
				if (y[i].name == "img1") {
					imglength1 += (y[i].fileSize)*1;
					if (imglength1 >= 200000) { // 153600
						if (errorChk == false) {
							chkValidate = document.getElementById("cavatar").focus();
						}
						errorChk = true;
						msg_error += "Images size more than 200 K \r\n";
					}
				}
			}
			if (errorChk == true) {
				alert(msg_error);
				chkValidate;
			} else {
				//submit();
			}
		}
}
function checkMe( aName , aValue ){
var any = document.register.aName;
if (aName.checked)
    {
	  aName.value=aValue;
    }
}

function mailThisUrl(emailTo, subjectTo, bodyTo){
	var mail;
	mail = "mailto:" + emailTo;
	if(subjectTo!=''){
		mail += "?subject=" + subjectTo;
	}
	if(bodyTo != ''){
		mail += "&body=" + bodyTo;
	}
	location.href = mail;
}
