// 	we have to include
//	helper.js
//  validate-form.js
//  before use this file in the html page.

// Alert text
var txt_alert = new Array();
txt_alert['main'] = "กรุณากรอกข้อมูลให้ถูกต้องด้วยค่ะ";
txt_alert['email'] = "กรุณาใส่อีเมล์ให้ถูกต้องด้วยค่ะ";
txt_alert['nickname'] = "กรุณาใส่ชื่อที่แสดงให้ผู้อื่นเห็นด้วยค่ะ";
txt_alert['name'] = "กรุณาใส่ชื่อจริงด้วยค่ะ";
txt_alert['lastname'] = "กรุณาใส่นามสกุลด้วยค่ะ";
txt_alert['address'] = "กรุณาใส่ที่อยู่ด้วยค่ะ";
txt_alert['postcode'] = "กรุณาใส่รหัสไปรษณีย์ให้ถูกต้องด้วยค่ะ";
txt_alert['telephone'] = "กรุณาใส่เบอร์โทรศัพท์ให้ถูกต้องด้วยค่ะ";
txt_alert['birthdate'] = "กรุณาใส่วันเกิดด้วยค่ะ";
txt_alert['password'] = "กรุณาใส่รหัสผ่านด้วยค่ะ";
txt_alert['repassword'] = "กรุณายืนยันรหัสผ่านด้วยค่ะ";
txt_alert['shop_name'] = "กรุณาใส่ชื่อร้านค้าด้วยค่ะ";
txt_alert['shop_address'] = "กรุณาใส่ที่อยู่ด้วยค่ะ";
txt_alert['shop_postcode'] = "กรุณาใส่รหัสไปรษณีย์ร้านค้าให้ถูกต้องด้วยค่ะ";
txt_alert['captcha_code'] = "กรุณาใส่ตัวอักษรในภาพให้ถูกต้องด้วยค่ะ";
txt_alert['identity_id'] = "กรุณาใส่หมายเลขบัตรประจำตัวประชาชนให้ถูกต้องด้วยค่ะ";
txt_alert['shop_telephone'] = "กรุณาใส่เบอร์โทรศัพท์ติดต่อร้านค้าด้วยค่ะ";

function CheckPassword()
{
	var password = document.getElementById('password');	
	
	if(password.value.length == 0)
	{
		//getRef(password).style["borderColor"]= "#FF0000";
		
		document.getElementById(password.id).style.borderStyle = "solid";
		document.getElementById(password.id).style.borderWidth = "1px 1px 1px 4px";
		document.getElementById(password.id).style.borderColor = "#FF0000";	
		document.getElementById(password.id).style.paddingLeft = "3px";	
		//document.getElementById("password_desc").innerHTML = txt_alert[password.id];
		return false;
	}
	else
	{
		document.getElementById(password.id).style.borderStyle = "solid";
		document.getElementById(password.id).style.borderWidth = "1px 1px 1px 4px";
		document.getElementById(password.id).style.borderColor = "#33CC33";
		document.getElementById(password.id).style.paddingLeft = "3px";	
		//document.getElementById("password_desc").innerHTML = "";
		return true;
	}
}

function CheckConfirmPassword()
{
	var password = document.getElementById('password');	
	var repassword = document.getElementById('repassword');
		
	if(repassword.value.length == 0)
	{
		document.getElementById(repassword.id).className = ClassInvalid;
		document.getElementById("repassword_desc").innerHTML = txt_alert[repassword.id];	
		return false;
	}
	else 
	{
		if(password.value != repassword.value)
		{
			document.getElementById(repassword.id).className = ClassInvalid;
			document.getElementById("repassword_desc").innerHTML = "รหัสผ่านและการยืนยันรหัสผ่านไม่ตรงกัน";		
			return false;
		}
		else
		{
			//alert(password.value + " , " + repassword.value);
			document.getElementById(repassword.id).className = ClassValid;
			document.getElementById("repassword_desc").innerHTML = "";	
			return true;
		}
	}
}

function CheckEmail(input)
{	
	var input_name = input.id;
	var input_desc = input_name + "_desc";
	
	if(!IsEmail(input.value.Trim()))
	{		
		document.getElementById(input_name).className = ClassInvalid;
		document.getElementById(input_desc).innerHTML = txt_alert[input_name];
		return false;
	}
	else
	{
		document.getElementById(input_name).className = ClassValid;
		document.getElementById(input_desc).innerHTML = "";	
		return true;
	}
}

function CheckNickname(input)
{
	var input_name = input.id;
	var input_desc = input_name + "_desc";
	
	if(input.value.Trim().length == 0)
	{
		document.getElementById(input_name).className = ClassInvalid;
		document.getElementById(input_desc).innerHTML = txt_alert[input_name];
		return false;
	}
	else
	{
		document.getElementById(input_name).className = ClassValid;
		document.getElementById(input_desc).innerHTML = "";	
		return true;
	}	
}

function CheckThaiIdentityID(input)
{
	var input_name = input.id;
	var input_desc = input_name + "_desc";
	if(IsThaiPid(input.value))
	{
		document.getElementById(input_name).className = ClassValid;
		document.getElementById(input_desc).innerHTML = "";	
		return true;
	}
	else
	{
		document.getElementById(input_name).className = ClassInvalid;
		document.getElementById(input_desc).innerHTML = txt_alert[input_name];
		return false;
	}
}

function CheckCaptcha(input)
{
	var input_name = input.id;
	var input_desc = input_name + "_desc";
	
	jQuery.get(document.getElementById('web_server_tak').value + "/member/ValidateCaptcha/" + input.value, 
			   {}, 
			   function(data)
	{												
	 //alert(data);
	 	if(data == "1")
		{
			document.getElementById(input_name).className = ClassValid;
			document.getElementById(input_desc).innerHTML = "";	
			return true;
		}
		else if(data == "0")
		{
			document.getElementById(input_name).className = ClassInvalid;
			document.getElementById(input_desc).innerHTML = txt_alert[input_name];
			return false;	
		}
	});
}

function CheckBirthday()
{
	var date =  document.getElementById('day').value;
	var month =  document.getElementById('month').value;
	var year = document.getElementById('year').value;	
		
	if(!IsWhole(date) || !IsWhole(month) || !IsWhole(year))
	{		
		document.getElementById("birthdate_desc").innerHTML = txt_alert["birthdate"];
		return false;
	}
	else
	{
		document.getElementById("birthdate_desc").innerHTML = "";	
		return true;
	}
}