function XMLHTTPRequest(){
	loading_init();
    var xhr;
	try {xhr = new ActiveXObject("Msxml2.XMLHTTP");} 
	catch (e) {
		try {xhr = new ActiveXObject("Microsoft.XMLHTTP");} 
		catch (e) {xhr = false;}
		if (!xhr && typeof XMLHttpRequest != 'undefined') xhr = new XMLHttpRequest();
	}
	return xhr;
}
function getObject(str){
	if(typeof str == "Object" || typeof str == "object" ) return str;
	else return document.getElementById(str);
}

function sendRequestHttp(req,url,params,httpMethod){	
	req.open(httpMethod,url,true);
	req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	req.send(params);	
}
function gotoTop(){
    $( 'html, body' ).animate( { scrollTop: 0 }, 'slow' );
}

function goToAnchor(id){
    $('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}

function getPage(obj, params){	
    if (typeof obj == "string" || typeof obj == "String") obj = getObject(obj);
    var xhr=new XMLHTTPRequest();	
	xhr.onreadystatechange=function(){
	    
		if(xhr.readyState == 4){
			if (xhr.status==200){
				var xmlDoc = xhr.responseXML;
				//alert(xhr.responseText);
				if (!xmlDoc || !xmlDoc.documentElement){
                    loading_error("Lổi kết nối!");
					return;
				}
                obj.innerHTML = xmlDoc.getElementsByTagName('data')[0].firstChild.nodeValue;
                loading_success();
			}
		}
	}    
    
	sendRequestHttp(xhr, txtfile ,params,"POST");	
}



function change_language(obj){
    var xhr=new XMLHTTPRequest();	
	xhr.onreadystatechange=function(){	
		if(xhr.readyState == 4){
			if (xhr.status==200){
				window.location.reload();
			}
		}
	}
	sendRequestHttp(xhr, txtfile ,"module=language&page=change","POST");
}

//
// getPageSize()
// Returns array with page width, height and window width, height

function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function isNumber(str){
    str = str.toString();
    var re = /^[-]?\d*\.?\d*$/;
    if (!str.match(re)||str.length=="") return false;
	else  return true;
}

function formatCommas(numString) {
    var re = /(-?\d+)(\d{3})/;
    while (re.test(numString)) {
        numString = numString.replace(re, "$1,$2");
    }
    return numString;
}

function isEmail(vl) {
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!vl.match(re)) {        
        return false;
    } else {
        return true;
    }
}

//********************/

function validateImg(imgUrl){
	if(imgUrl){
	    validformFile = /(.gif|.GIF|.jpg|.JPG|.png|.PNG)$/;
	    if(!validformFile.test(imgUrl)){
	      	alert('Support *.gif| *.jpg| *.png');
		  	return false;
		}
	}else {return false;}
}

function showbox(id,show){	
	var obj = getObject(id);	
	if(!show) obj.style.display='none';
	else obj.style.display=(obj.style.display != 'block')?"block":"none";
}

var txtfile = "modules.ajax.post.php";
var xmlfile = "modules.ajax.xml.php";


//var num_counter = LIFETIME;
function counter_online(){
    if (num_counter > 0 ){
		num_counter --;
        ivCounter = window.setTimeout("counter_online()", 1000);
	}    
    
	else if(num_counter == 0){
        var xhr= new XMLHTTPRequest();
    	xhr.onreadystatechange=function(){
    	    if(xhr.readyState == 4){
    			if (xhr.status==200){
    			    num_counter = LIFETIME;
                    window.clearTimeout(ivCounter);
                    ivCounter = window.setTimeout("counter_online()", 1000);                    
    			}
    		}
    	}
    	xhr.open("POST",'counter.php', true);
        xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    	xhr.send("");
	}
}
//var ivCounter = window.setTimeout("counter_online()", 1000);

function changeCaptcha(){
    var num = Math.random();
    document.getElementById('captcha_login').src='captcha/captcha.php?name='+num;    
}

function setCookie(c_name, value, exdays){
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value)+";path="+COOKIE_PATH + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());    
    document.cookie=c_name + "=" + c_value;
    alert(document.cookie);
}


function subscript(obj){
    var emailfrom = obj.elements['emailfrom'].value;
    var emailto = obj.elements['emailto'].value;
    
    if(!isEmail(emailfrom)){
        obj.elements['emailfrom'].className='text_red';
        loading_error("Email không hợp lệ!");
    }else if(!isEmail(emailto)){
        obj.elements['emailto'].className='text_red';
        loading_error("Email không hợp lệ!");
    }
    
    else{
        var xml="<?xml version='1.0' encoding='UTF-8'?><root><itnet module='subscript'>";
    	xml += "<params>";
        xml += "<node><![CDATA["+emailfrom+"]]></node>";
        xml += "<node><![CDATA["+emailto+"]]></node>";
    	xml += "</params></itnet></root>";
    	
    	var xhr=new XMLHTTPRequest();	
    	xhr.onreadystatechange=function(){
    		if(xhr.readyState == 4){
    			if (xhr.status==200){
		            var t = xhr.responseText;
                    if (isNumber(t)){
                        obj.elements['emailfrom'].className='green';
                        obj.elements['emailto'].className='green';
                        obj.elements['emailfrom'].value='Email người gửi';
                        obj.elements['emailto'].value = 'Email người nhận';
        			    loading_success();
                    }
                    else{
                        loading_error(t);
                    }
    			}
    		}		
    	}
    	xhr.open("POST", xmlfile, true);
    	xhr.setRequestHeader("Content-Type","text/xml");
    	xhr.send(xml);
    }
}

