urlbox_default = "Gỏ hoặc Copy và dán URL vào đây";

window.onload = function () {
	// add event to url
	var urlbox = document.getElementById('url');
		if (urlbox) {
		urlbox.onfocus = function () {
			this.className = '';
			if (this.value == urlbox_default) {
				this.value = "";
				this.select();
			}
		}
		urlbox.onblur = function () {
			if (this.value == "" || this.value == "http://" || this.value == urlbox_default) {
				this.value = urlbox_default
				this.className = 'empty';
			}
		}
		if (urlbox.value == "") {
			urlbox.onblur();
		} else {
			urlbox.onfocus();
		}
	}
}


var http = createRequestObject();

function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}

function getURL() {
	urlbox = document.getElementById('url');

	if (urlbox && (urlbox.value == "" || urlbox.value == urlbox_default || urlbox.value == 'http://')) {
		alert("Bạn chưa nhập địa chỉ URL cần rút gọn.\nThử lại nhé!");
		return false;
	}

	http.open('POST','geturl.php');
	field = document.getElementById('miniurl');
	url_value = encodeURIComponent(document.getElementById('url').value);
	c_url_value = encodeURIComponent(document.getElementById('c_url').value);
	field.innerHTML="Loading...<br> <img src='"+LOADING_IMG+"'>";
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
	http.send('geturl=1&url='+url_value+'&c_url='+c_url_value);
	
	return false;
}

function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML=http.responseText;
			ReloadIMG('sc_img');
		}
  	}
	catch(e){}
	finally{}
}


function reportURL() {
	url_report = encodeURIComponent(document.getElementById("url_report").value);
    email_report = encodeURIComponent(document.getElementById("email_report").value);
    reason_report = encodeURIComponent(document.getElementById("reason_report").value);
	antifloodcode = encodeURIComponent(document.getElementById("scode_report").value);
	
	if ( trim(url_report) == "" || trim(email_report) == "" || trim(reason_report) == "" || trim(antifloodcode) == "") {
		alert("Bạn cần nhập đầy đủ các thông tin để có thể gởi Report!");
		return false;
	}
	if (reason_report.length > 3000) {
		alert('Nội dung bạn nhập dài đến '+reason_report.length+' ký tự.\n Để tránh Spam nên chúng tôi hiện tại giới hạn phần nội dung chỉ đến 3000 ký tự.\n Bạn chỉ cần ghi ngắn gọn lý do Report thôi.\n Nếu bạn cần trao đổi thêm xin vui lòng liên hệ qua Email admin@77url.com. \n Cảm ơn bạn!');
		//alert("Nội dung bạn nhập dài hơn 1000 ký tự.\nBạn chỉ cần ghi ngắn gọn lý do Report thôi.\n Cảm ơn bạn!");
		return false;
	}
	http.open('POST','report.php');
	field = document.getElementById('rs_report');
	field.innerHTML="Loading...<br> <img src='"+LOADING_IMG+"'>";
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
	http.send('report=1&url='+url_report+'&email='+email_report+'&reason='+reason_report+'&antifloodcode='+antifloodcode);

	return false;
}

function ReloadIMG(img_name) {
	ran_n = Math.random();
	document[img_name].src="antiflood.php?c="+ran_n;
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function change_pvurl(){
	var pvurl_status = readCookie('previewurl');
	
	if (pvurl_status == 1) {
		document.getElementById('pv_show').src = PVURL_D_IMG;
	//	document.cookie='previewurl=0';
		setCookie('previewurl',0,365);
	}
	else {
		document.getElementById('pv_show').src = PVURL_E_IMG;
	//	document.cookie='previewurl=1';
		setCookie('previewurl',1,365);
	}
}
