// AJAX Request Authentication
var xmlHttpAuthen;
function createXMLHttpRequestAuthen() {
	xmlHttpAuthen = AjaxBrowserSupport();
}
  
function RequestAuthen() { 
	createXMLHttpRequestAuthen();
    xmlHttpAuthen.onreadystatechange = callBackAuthen;
    xmlHttpAuthen.open("GET", "ajax/authentication.aspx" , true);
    xmlHttpAuthen.send(null);
}

function callBackAuthen() {
	//document.getElementById('divResult').innerHTML = "<b class=normalBlack>ระบบกำลังทำการล็อกอินอัตโนมัติ กรุณารอสักครู่...</b>";
    if(xmlHttpAuthen.readyState == 4) {
        if(xmlHttpAuthen.status == 200) {
			var errCode = xmlHttpAuthen.responseText;
			var msg ;
							
			if (errCode == '000'){ //success
				window.location.href = 'newsservice.aspx' ;
				return true;
			}else if(errCode == '001'){ //IP Invalid
				window.location.href = 'http://www.iqnewsclip.com' ;
				return true; 
			}else if(errCode == '005'){ // User Expire
				msg = 'ท่านไม่สามารถเข้าใช้งานได้ เนื่องจากหมดอายุบริการ<br>กรุณาติดต่อเจ้าหน้าที่ฝ่ายขายเพื่อต่ออายุบริการ โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg;
			}else if(errCode == '003'){ // Concurrence Over Limit
				msg = 'ขณะนี้มีผู้ใช้งานเต็มตามจำนวนที่รับบริการ กรุณาเข้าใช้งานใหม่อีกครั้งในภายหลัง<br>สอบถามข้อมูลเพิ่มเติม โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg;
			}else{ // Service Fail (002=Login(user/pwd), 004=Service)
				msg = 'ขออภัย ระบบไม่สามารถให้บริการได้ในขณะนี้<br>กรุณาติดต่อเจ้าหน้าที่ โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg + '<br><br>Error Code: ' + xmlHttpAuthen.responseText ;
			}
		}else{
			msg = 'ขออภัย ระบบไม่สามารถให้บริการได้ในขณะนี้<br>กรุณาติดต่อเจ้าหน้าที่ โทร. 02 2535000 ต่อ 222, 444';
			document.getElementById('divResult').innerHTML = msg + '<br><br>Error Status: ' + xmlHttpAuthen.status ;
        }
    }
}

function RequestAuthenIP(IP, Password) { 
	createXMLHttpRequestAuthen();
    xmlHttpAuthen.onreadystatechange = callBackAuthenIP;
    //alert(Password);
    xmlHttpAuthen.open("GET", "ajax/IPAuthentication.aspx" + "?ip=" + IP + "&pass=" + Password , true);
    xmlHttpAuthen.send(null);
}

function callBackAuthenIP() {
	//document.getElementById('divResult').innerHTML = "<b class=normalBlack>ระบบกำลังทำการล็อกอินอัตโนมัติ กรุณารอสักครู่...</b>";
   if(xmlHttpAuthen.readyState == 4) {
        if(xmlHttpAuthen.status == 200) {
			var errCode = xmlHttpAuthen.responseText;
			var msg ;
			
			//Disable loading image
			document.getElementById("divLoadingProgress").style.cssText = "display: none; overflow: visible;"
			
			//Reset Message
			msg = '';
			document.getElementById('divResult').innerHTML = msg;

			if (errCode == '000'){ //success
				window.location.href = 'newsservice.aspx' ;
				return true;
			}else if(errCode == '001'){ //IP Invalid
				window.location.href = 'http://www.iqnewsclip.com' ;
				return true; 
			}else if(errCode == '005'){ // User Expire
				msg = 'ท่านไม่สามารถเข้าใช้งานได้ เนื่องจากหมดอายุบริการ<br>กรุณาติดต่อเจ้าหน้าที่ฝ่ายขายเพื่อต่ออายุบริการ โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg;
			}else if(errCode == '003'){ // Concurrence Over Limit
				msg = 'ขณะนี้มีผู้ใช้งานเต็มตามจำนวนที่รับบริการ กรุณาเข้าใช้งานใหม่อีกครั้งในภายหลัง<br>สอบถามข้อมูลเพิ่มเติม โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg;
			}else if(errCode == '006'){
				msg = 'ท่านไม่สามารถเข้าใช้งานได้ เนื่องจากรหัสผ่านไม่ถูกต้อง';
				document.getElementById('divResult').innerHTML = msg;
			}else{ // Service Fail (002=Login(user/pwd), 004=Service)
				msg = 'ขออภัย ระบบไม่สามารถให้บริการได้ในขณะนี้<br>กรุณาติดต่อเจ้าหน้าที่ โทร. 02 2535000 ต่อ 222, 444';
				document.getElementById('divResult').innerHTML = msg + '<br><br>Error Code: ' + xmlHttpAuthen.responseText ;
			}
		}else{
			msg = 'ขออภัย ระบบไม่สามารถให้บริการได้ในขณะนี้<br>กรุณาติดต่อเจ้าหน้าที่ โทร. 02 2535000 ต่อ 222, 444';
			document.getElementById('divResult').innerHTML = msg + '<br><br>Error Code: ' + xmlHttpAuthen.status ;
        }
    }else
    {
		//Reset Message
		msg = '';
		document.getElementById('divResult').innerHTML = msg;
			
		//Display loading animation
		document.getElementById("divLoadingProgress").style.cssText = "display: inline; overflow: visible;"
    }
}

// AJAX Request SearchResult PageLoad
var xmlHttp;
function createXMLHttpRequest() {
	xmlHttp = AjaxBrowserSupport();
}
  
function RequestGetResult(parameter) { 
	createXMLHttpRequest();
    xmlHttp.onreadystatechange = callBackGetResult;
    xmlHttp.open("GET", "ajax/GetResult.aspx" + parameter , true);
    xmlHttp.send(null);
}

function callBackGetResult() {
	document.body.style.cursor = 'wait';
	document.getElementById('CtrlSearch1_SearchResult').innerHTML = "<br><b class=normalBlack>กำลังค้นหา...</b>";
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			document.getElementById('CtrlSearch1_SearchResult').innerHTML = xmlHttp.responseText;
			DisableButtonSearch(document.getElementById('hdnError').value) ;
		}else if(xmlHttp.status == 600){
			window.location.href='default.aspx?err=004';
		}else{
			document.getElementById('CtrlSearch1_SearchResult').innerHTML = StatusResult(xmlHttp.status);
        }
    }
	document.body.style.cursor = 'default';
}

// AJAX Request SearchResult Page Next
function RequestPageNext() { 
	var parameter = document.getElementById('hdnPageNext').value;
	RequestGetResult(parameter);
}

// AJAX Request SearchResult Page Previous
function RequestPagePrev() { 
	var parameter = document.getElementById('hdnPagePrev').value;
	RequestGetResult(parameter);
}

// AJAX Request Search Result Button
var xmlHttpSearchResult;
function createXMLHttpRequestSearchResult() {
	xmlHttpSearchResult = AjaxBrowserSupport();
}

function RequestGetSearchResult(iPage,searchtype) { 
	createXMLHttpRequestSearchResult();
	var pForm = document.forms[0];
	var pBody = getRequestBodySearchResult(pForm); 
	var pRadio = '' ;
	if (iPage != 1) { pRadio = document.getElementById('CtrlSearch1_rbtBasic').checked ; }
	var parameter = "?stype=" + searchtype + "&rbt=" + pRadio ;
	pForm.action = "ajax/GetResult.aspx" + parameter ;
	xmlHttpSearchResult.open("POST", pForm.action, true);
	xmlHttpSearchResult.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttpSearchResult.onreadystatechange = callBackGetSearchResult;
    xmlHttpSearchResult.send(pBody);        
}
function getRequestBodySearchResult(pForm) {
    var nParams = new Array();
    for (var i=0 ; i < pForm.elements.length; i++) {
        var pParam = encodeURIComponent(pForm.elements[i].name);
        pParam += "=";
        pParam += encodeURIComponent(pForm.elements[i].value);
        nParams.push(pParam);
    } 
    return nParams.join("&");        
}

function callBackGetSearchResult() {
	document.body.style.cursor = 'wait';
	document.getElementById('CtrlSearch1_SearchResult').innerHTML = "<br><b class=normalBlack>กำลังค้นหา...</b>";
    if(xmlHttpSearchResult.readyState == 4) {
        if(xmlHttpSearchResult.status == 200) {
			document.getElementById('CtrlSearch1_SearchResult').innerHTML = xmlHttpSearchResult.responseText;
			document.getElementById('chkAddAll').checked = false;
			DisableButtonSearch(document.getElementById('hdnError').value) ;
		}else if(xmlHttpSearchResult.status == 600){
			window.location.href='default.aspx?err=004';
		}else{
			document.getElementById('CtrlSearch1_SearchResult').innerHTML = StatusResult(xmlHttpSearchResult.status);
        }
    }
	document.body.style.cursor = 'default';
}

//Check Browser Support
function AjaxBrowserSupport()
  {  
  var objXML ;
  try { // Firefox, Opera 8.0+, Safari    
		objXML =new XMLHttpRequest();   
  }catch (e){    // Internet Explorer    
		try {    
			 objXML =new ActiveXObject("Msxml2.XMLHTTP");      
		}catch (e){ 
		     try{ 
		         objXML =new ActiveXObject("Microsoft.XMLHTTP"); 
		     }catch (e){        
				alert("Your browser does not support AJAX!");  return false; 
             }      
        }    
  }  
	return objXML ;
}

// Get Status Error
function StatusResult(codestatus) {
	var desc ;
	if (codestatus == 404 ){
		desc = "Page does not exist (404).";
	}else if (codestatus >= 401 && codestatus < 402) {       
   		desc = "Access denied (401)." ;
   	}else if (codestatus >= 500 && codestatus < 600){
   		desc = "500 Internal Server Error on remote site." ;
    }else{
   		desc = "Server is down or does not exist.";
   	}	
   	DisableButtonSearch('nonews');
   	return "<br><b class=normalRed>ระบบไม่สามารถให้บริการได้<br>"+ desc + "</b>" ;
}

// Disable Button กรณีไม่มีข่าวหรือเกิด Error
function DisableButtonSearch(desc) {
	if (desc == 'nonews'){
		document.getElementById('CtrlSearch1_btnSearch').disabled = true ;
		document.getElementById('CtrlSearch1_txtSearch').disabled = true ;
		document.getElementById('CtrlSearch1_btnReset').disabled = true ;
	}else if (desc == 'searchErr') {       
		document.getElementById('CtrlSearch1_rbtBasic').checked = false ;
		document.getElementById('CtrlSearch1_rbtNowDate').checked = true ;
		document.getElementById('CtrlSearch1_txtDateFrom').innerText = "" ;
		document.getElementById('CtrlSearch1_txtDateTo').innerText = "" ;
   	}	
} 
