// =============================================================================================================
// Http Request
// =============================================================================================================
// create XML Http Request
function createXMLHttpRequest(){
	try{
		xmlHttp=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	}catch(e){
		alert('There was a problem creating the XMLHttpRequest object'); 
	}
}
var xmlHttp;
// =============================================================================================================
// Http Request
// =============================================================================================================
function RatingStar(link_id){
	var uri = document.location.href; 
	var tmp=uri.split("#");
	var id=tmp[1];
	var rating;
	if(id){
		if(id=="vote-1")rating=1;
		else if(id=="vote-2")rating=2;
		else if(id=="vote-3")rating=3;
		else if(id=="vote-4")rating=4;
		else if(id=="vote-5")rating=5;
		else rating=0;
	}
	/*if(rating){
		createXMLHttpRequest();
		var time=new Date();
		xmlHttp.open("GET","/webdetail/ratings.php?time="+time+"&link="+link_id+"&rating="+rating,true);
		xmlHttp.onreadystatechange=function(){ 
			if(xmlHttp.readyState==4&&xmlHttp.status==200){			
				//alert(xmlHttp.responseText);
				document.getElementById('star-rating-result').innerHTML=xmlHttp.responseText;
				//if(xmlHttp.responseText!="1")alert(msg);
				//else document.getElementById(accept).submit();
			}
		}
		xmlHttp.send(null);
		return false;
		//alert(link_id);
	}*/
}	
function checkComment(captcha){
	var v1=document.comment.name.value;
	var v2=document.comment.email.value;
	var v3=document.comment.messege_comment.value;
	var v4=document.comment.captcha.value;
	if(v1==0||v1.length<3){
		if(v1==0)alert("กรุณากรอกชื่อของคุณด้วยนะคะ");
		else if(v1.length<3)alert("ชื่อต้องมีความยาวมากกว่า 3 ตัวอักษรนะคะ");
		document.comment.name.focus();           
 		return false;
	}
	if(v2==0||!isValidEmail(v2,true)){
  		if(v2==0)alert("กรุณากรอกอีเมล์ด้วยนะคะ");
		else if(!isValidEmail(v2,true))alert("คุณกรอกอีเมล์ไม่ถูกต้อง กรุณาตรวจสอบอีเมล์ใหม่อีกครั้ง");
		document.comment.email.focus();           
 		return false;
	}
	if(v3==0||v3.length<3){
  		if(v3==0)alert("กรุณากรอกข้อความด้วยนะคะ");
		else if(v3.length<3)alert("ข้อความต้องมีความยาวมากกว่า 3 ตัวอักษรนะคะ");
		document.comment.messege_comment.focus();           
 		return false;
	}
	if(v4==0||v4.length<6){
  		if(v4==0)alert("กรุณากรอกรหัสโพสด้วยนะคะ");
		else if(v4.length<6)alert("คุณกรอกรหัสโพสไม่ถูกต้อง กรุณากรอกใหม่อีกครั้ง");
		document.comment.captcha.focus();           
 		return false;
	}
	checkCaptcha('captcha',captcha,'คุณกรอกรหัสโพสไม่ถูกต้อง กรุณากรอกใหม่อีกครั้ง',"comment");
	return false;
}

// =============================================================================================================
// Function
// =============================================================================================================
function textCounter( field, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    return false;
  }
}

function isValidEmail(email, required) {
    if (required==false) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}