// =============================================================================================================
// Add Web
// =============================================================================================================
// Step 3 : Detail

String.prototype.trim=function() 
{
	return this.replace(/^\s*|\s*$/g,'');
}
String.prototype.ltrim=function() 
{
	return this.replace(/^\s*/g,'');
}
String.prototype.rtrim=function() 
{
	return this.replace(/\s*$/g,'');
}

function selectSubCat(){
	var cat_id=document.post_web.cat_id.value;
	createXMLHttpRequest();
	var time=new Date();
	xmlHttp.open("GET","/addweb/check_url.php?mode=3&time="+time+"&cat_id="+cat_id,true);
	xmlHttp.onreadystatechange=function(){ 
		if(xmlHttp.readyState==4&&xmlHttp.status==200){			
			document.getElementById("sub_cat").innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.send(null);
}

function checkDetail(){
	var url=document.post_web.url.value.trim();
	var title=document.post_web.titleth.value.trim();
	var desc=document.post_web.descth.value.trim();
	var key1=document.post_web.key1.value.trim();
	var key2=document.post_web.key2.value.trim();
	var key3=document.post_web.key3.value.trim();
	var keyword=document.post_web.keyword.value.trim();
	var cat_id=document.post_web.cat_id.value.trim();
	var sub_cat_id=document.post_web.sub_cat_id.value.trim();
	var name=document.post_web.name.value.trim();
	var email=document.post_web.email.value.trim();
        
	if(title==""||title.length< 3 ||(title.length>100)){
  		if(title=="")alert("กรุณากรอกชื่อโฮมเพจด้วยนะคะ");
		else if(title.length < 3 )alert("ชื่อโฮมเพจต้องมีความยาวไม่น้อยกว่า 3 ตัวอักษรค่ะ");
		else if(title.length>100)alert("ชื่อโฮมเพจต้องมีความยาวไม่เกิน 100 ตัวอักษรค่ะ");
		document.post_web.titleth.focus();           
 		return false;
	}
	if(desc==""||desc.length<20||desc.length>150){
		if(desc=="")alert("กรุณากรอกคำบรรยายด้วยนะคะ");
		else if(desc.length<20)alert("คำบรรยายต้องมีความยาวไม่น้อยกว่า 20 ตัวอักษรค่ะ");
		else if(desc.length>150)alert("คำบรรยายต้องมีความยาวไม่เกิน 150 ตัวอักษรค่ะ");
		document.post_web.descth.focus();           
		return false;
	}
	if(key1==""||key1.length<3||key1.length>20){
		if(key1=="")alert("กรุณากรอก Tag ด้วยนะคะ");
		else if(key1.length<3)alert("Tag ต้องมีความยาวไม่น้อยกว่า 3 ตัวอักษรค่ะ");
		else if(key1.length>20)alert("Tag ต้องมีความยาวไม่เกิน 20 ตัวอักษรค่ะ");
		document.post_web.key1.focus();
		return false;
	}
	if(key2==""||key2.length<3||key2.length>20){
		if(key2=="")alert("กรุณากรอก Tag ด้วยนะคะ");
		else if(key2.length<3)alert("Tag ต้องมีความยาวไม่น้อยกว่า 3 ตัวอักษรค่ะ");
		else if(key2.length>20)alert("Tag ต้องมีความยาวไม่เกิน 20 ตัวอักษรค่ะ");
		document.post_web.key2.focus();           
		return false;
	}
	if(key3==""||key3.length<3||key3.length>20){
		if(key3=="")alert("กรุณากรอก Tag ด้วยนะคะ");
		else if(key3.length<3)alert("Tag ต้องมีความยาวไม่น้อยกว่า 3 ตัวอักษรค่ะ");
		else if(key3.length>20)alert("Tag ต้องมีความยาวไม่เกิน 20 ตัวอักษรค่ะ");
		document.post_web.key3.focus();
		return false;
	}
	if(keyword==""||keyword.length<3||keyword.length>100){
		if(keyword=="")alert("กรุณากรอก Keyword ด้วยนะคะ");
		else if(keyword.length<3)alert("Keyword ต้องมีความยาวไม่น้อยกว่า 3 ตัวอักษรนะคะ");
		else if(keyword.length>100)alert("Keyword ต้องมีความยาวไม่เกิน 100 ตัวอักษรนะคะ");
		document.post_web.keyword.focus();           
		return false;
	}
	if(cat_id==0 || cat_id=="" || cat_id=="ยังไม่ระบุหมวดหลัก"){
  		alert("กรุณาเลือกหมวดหลักด้วยนะคะ");
 		return false;
	}
	if(name==""||name.length<2||name.length>30){
		if(name=="")alert("กรุณากรอกชื่อผู้กรอกด้วยนะคะ");
		else if(name.length<2)alert("ชื่อผู้กรอกต้องมีความยาวไม่น้อยกว่า 2 ตัวอักษรนะคะ");
		else if(name.length>30)alert("ชื่อผู้กรอกต้องมีความยาวไม่เกิน 30 ตัวอักษรนะคะ");
		document.post_web.name.focus();           
		return false;
	}
	if(email==""||!isValidEmail(email,true)||email.length>50){
  		if(email=="")alert("กรุณากรอกอีเมล์ด้วยนะคะ");
		else if(email.length>50)alert("อีเมล์ต้องมีความยาวไม่เกิน 50 ตัวอักษรนะคะ");
		else if(!isValidEmail(email,true))alert("คุณกรอกอีเมล์ไม่ถูกต้อง กรุณาตรวจสอบอีเมล์ใหม่อีกครั้ง");
		document.post_web.email.focus();           
 		return false;
	}	
        //alert(title);
	// save data
	createXMLHttpRequest();
	var time=new Date();
        //alert("/addweb/check_url.php?mode=4&time="+time+"&title="+title+"&desc="+desc+"&key1="+key1+"&key2="+key2+"&key3="+key3+"&keyword="+keyword+"&cat_id="+cat_id+"&sub_cat_id="+sub_cat_id+"&name="+name+"&email="+email+"&url="+url);
	xmlHttp.open("GET","/addweb/check_url.php?mode=4&time="+time+"&title="+title+"&desc="+desc+"&key1="+key1+"&key2="+key2+"&key3="+key3+"&keyword="+keyword+"&cat_id="+cat_id+"&sub_cat_id="+sub_cat_id+"&name="+name+"&email="+email+"&url="+url,true);
	xmlHttp.onreadystatechange=function(){ 
		if(xmlHttp.readyState==4&&xmlHttp.status==200){			
			if(xmlHttp.responseText=="1"){
                            //  alert(xmlHttp.responseText);
                            	document.getElementById("post_web").submit();
			}else alert(xmlHttp.responseText);
			//alert(xmlHttp.responseText);
			//document.getElementById("test").innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.send(null);
	return false;
}

// Step 2 : Check Url
function checkURL(){
	var url=document.check_site.url.value;
	if(url!=""&&url!="http://"&&url!="https://"&&url.length>0){
		if(url.length>=13){
			createXMLHttpRequest();
			var time=new Date();
			xmlHttp.open("GET","/addweb/check_url.php?mode=1&time="+time+"&url="+url,true);
			xmlHttp.onreadystatechange=function(){ 
				if(xmlHttp.readyState==4&&xmlHttp.status==200){			
					if(xmlHttp.responseText=="1"){
						SearchUrl(url);
					}else alert("URL ไม่ถูกต้องกรุณากรอกใหม่อีกครั้ง");
				}
			}
			xmlHttp.send(null);
		}else alert("URL ไม่ถูกต้องกรุณากรอกใหม่อีกครั้ง");
	}else alert("กรุณากรอก URL ด้วยนะคะ");
	document.check_site.url.focus(); 
	return false	
}
function SearchUrl(url){
	createXMLHttpRequest();
	var time=new Date();
	xmlHttp.open("GET","/addweb/check_url.php?mode=2&time="+time+"&url="+url);
	xmlHttp.onreadystatechange=function(){ 
		if(xmlHttp.readyState==4&&xmlHttp.status==200){			
			if(xmlHttp.responseText=="1"){
				document.getElementById("check_site").submit();
			}else alert("มี URL นี้แล้วในระบบ");
		}
	}
	xmlHttp.send(null);
}
// Step 1 : Accept
function checkAccept(){
	if(document.accept.check.checked)document.accept.submit();
	else alert("คุณต้องยอมรับเงื่อนไข");
	return false;
}
// =============================================================================================================
// 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;
// =============================================================================================================
// 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;
}