var xmlHttp;
var xmlHttp1;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
    }else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
        xmlHttp1 = new XMLHttpRequest();
    }
}

function getRequestBody(oForm) {
    var aParams = new Array();
    
    for (var i=0 ; i < oForm.elements.length; i++) {
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
    } 
    
    return aParams.join("&");
}

function trim(str){
	return str.replace(/^\s+|\s+$/g, ''); 
}

function cmdDis(command){
  document.getElementById('cmdSend').disabled=command
  document.getElementById('cmdReset').disabled=command;			  
}

function chkReplace(value){
	
	result=value.replace("&","//**//");
	result=result.replace("=","**//**");
	
	return result;	
}

function getForm_f(){
	//var last=parseInt(document.form_f.elements.length)-parseInt(3);
	var last=parseInt(document.form_f.elements.length)-parseInt(1);
	var result="";

	for (var i=0;i<last;i++){
		if(document.form_f.elements[i].getAttribute("type")!= 'radio'){
			var value=chkReplace(document.form_f.elements[i].value);
			result=result+encodeURIComponent(document.form_f.elements[i].id)+"="+ encodeURI(value)+"&";
		}else if(document.form_f.elements[i].getAttribute("type")=='radio' && document.form_f.elements[i].checked==true){
			result=result+encodeURIComponent(document.form_f.elements[i].name)+"="+ encodeURI(document.form_f.elements[i].value)+"&";
		}//-----end if
	}//----end for
	
	return result;
}


function getReset_f(form_f){
	document.form_f.reset();
	//var last=parseInt(document.form_f.elements.length)-parseInt(2);
	/*var last=document.form_f.elements.length;
	var mess=new Array('ชื่อ - นามสกุล *','ที่อยู่','เบอร์โทรศัพท์ที่ติดต่อได้ *','Fax','อีเมล์','ข้อความ','รหัสยืนยันความปลอดภัย','ตกลง','ยกเลิก');
	
	
	for (var i=0;i<last;i++){
		document.form_f.elements[i].value=mess[i];
	}*/
}

function Enter_f(e,val){
   var keyPressed;
   var val;
   
   if(window.event){
      keyPressed = window.event.keyCode; // IE
       if (keyPressed==13){
			getCheck_f();
	   }
   }else{
      keyPressed = e.which; // Firefox      
       if (keyPressed==13){
			getCheck_f();
	   }
    }
}

function Check_f(obj,message,check,clear){
	alert(message);	
	if(clear=="Yes"){obj.value="";}
	obj.focus();
	loading_f('none');
	//cmdDis(false);
	return false;
}

function CheckEmail_f(obj,message,check,clear){
	 if(obj.value.indexOf('@',0)==-1 || obj.value.indexOf('.',0)==-1){
		alert(message);	
		if(clear=="Yes"){obj.value="";}
		obj.focus();
		loading_f('none');
		//cmdDis(false);
		return false;
	}else{
		return true;
	}
}

function loading_f(command){
	document.getElementById('loading_f').style.display=command;	
}

function swap(objNone,objInline){
	document.getElementById(objNone).style.display="none";
	document.getElementById(objInline).style.display="inline";
}

function isThaichar_f(str,obj){
	var orgi_text="_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.0123456789 ";
	var str_length=str.length;
	var str_length_end=str_length-1;
	var isThai=true;
	var Char_At="";
	for(i=0;i<str_length;i++){
		Char_At=str.charAt(i);
		if(orgi_text.indexOf(Char_At)==-1){
			isThai=false;
		}   
	}
	if(str_length>=1){
		if(isThai==false){
			obj.value=str.substr(0,str_length_end);
		}
	}
	return isThai; // ถ้าเป็น true แสดงว่าเป็นภาษาไทยทั้งหมด
}

function getCheck_f(){//alert('products');
	//cmdDis(true);
	loading_f('inline');

	var check_f=true;
	//var txttype_f=document.getElementById('txttype_f');
	var txttype_f_description=document.getElementById('txttype_f_description');
	var txtproducts=document.getElementById('txtproducts');
	var txtname_f=document.getElementById('txtname_f');
	var txttel_f=document.getElementById('txttel_f');
	var txtemail_f=document.getElementById('txtemail_f');
	var txtdescription_f=document.getElementById('txtdescription_f');
	var txtcallback=document.getElementById('txtcallback');
	var txtcallback_time=document.getElementById('txtcallback_time');
	var txtcode_f=document.getElementById('txtcode_f');
	//document.f1.r1[i].checked
	if(check_f==true && ((document.form_f.txttype_f[0].checked==true) && (trim(txttype_f_description.value)=="" || trim(txttype_f_description.value)=="ระบุกรมธรรม์"))){
		check_f=Check_f(txttype_f_description,'กรุณาระบุกรมธรรม์ค่ะ',check_f,'Yes');
		return false;
	}//alert(check_f);
	if(check_f==true && (trim(txtproducts.value)=="")){
		check_f=Check_f(txtproducts,'กรุณาเลือกประกันภัยค่ะ',check_f,'Yes');
		return false;
	}
	if(check_f==true && (trim(txtname_f.value)=="" || trim(txtname_f.value)=="ชื่อ - นามสกุล")){
		check_f=Check_f(txtname_f,'กรุณากรอกชื่อ - นามสกุลค่ะ',check_f,'Yes');
		return false;
	}
	if(check_f==true && (trim(txttel_f.value)=="" || trim(txttel_f.value)=="เบอร์โทรศัพท์ที่ติดต่อได้")){
		check_f=Check_f(txttel_f,'กรุณากรอกเบอร์โทรศัพท์ที่ติดต่อได้ค่ะ',check_f,'Yes');
		return false;
	}
	if(check_f==true && (trim(txtemail_f.value)=="" || trim(txtemail_f.value)=="อีเมล์")){
		check_f=Check_f(txtemail_f,'กรุณากรอกอีเมล์ค่ะ',check_f,'Yes');
		return false;
	}
	if(check_f==true && (trim(txtemail_f.value)!="" && trim(txtemail_f.value)!="อีเมล์")){
		if(txtemail_f.value.indexOf('@',0)==-1 || txtemail_f.value.indexOf('.',0)==-1){
			alert('รูปแบบอีเมล์ไม่ถูกต้อง');	
			txtemail_f.value='';
			txtemail_f.focus();
			loading_f('none');
			return false;
		}
	}
	if(check_f==true && (trim(txtdescription_f.value)=="" || trim(txtdescription_f.value)=="รายละเอียด")){
		check_f=Check_f(txtdescription_f,'กรุณากรอกรายละเอียดค่ะ',check_f,'Yes');
		return false;
	}
	if(check_f==true && (trim(txtcode_f.value)=="" || trim(txtcode_f.value)=="รหัสยืนยันความปลอดภัย")){
		check_f=Check_f(txtcode_f,'กรุณากรอกรหัสยืนยันความปลอดภัยค่ะ',check_f,'Yes');
		return false;
	}
}

function stopUpload_f(success){
	
	if(success==1){
		var check_f=true;
		var txtcode_f=document.getElementById('txtcode_f');
		
		Check_f(txtcode_f,'รหัสยืนยันความปลอดภัยไม่ถูกต้อง  กรุณากรอกใหม่อีกครั้งค่ะ',check_f,'Yes');
		loading_f('none');
		return false;
	}else{
		swap('showForm_f','showMessage_f');
		if(success==2)
			document.getElementById('showBody_f').innerHTML='ขอบคุณค่ะ เราได้รับข้อมูลของท่านแล้ว<br/>เราจะติดต่อกลับในภายหลังค่ะ<br /><br /><u onclick="getReset_f();swap(\'showMessage_f\',\'showForm_f\');" style="cursor:pointer;">ย้อนกลับ</u>';
		else
			document.getElementById('showBody_f').innerHTML='ขออภัยค่ะ กรุณาคลิ๊ก<u onclick="swap(\'showMessage_f\',\'showForm_f\');" style="cursor:pointer;">ที่นี่</u>เพื่อส่งแบบฟอร์มอีกครั้งค่ะ';
		loading_f('none');
	}
}

