var xmlhttp = false;
// Check if we are using IE.
try {
	// If the javascript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	// If not, then use the older active x object.
	try {
		// If we are using IE.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		// Else we must be using a non-IE browser.
		xmlhttp = false;
	}
}
// If we are using a non-IE browser, create a JavaScript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
}
/**
 * get the page with query and insert it on the div
 * 
 * @param layer_id
 * @param page
 * @param query
 * @return
 */
function ajaxPage(layer_id, page, query) {
	document.getElementById(layer_id).innerHTML = 'loading ... ';
	$('#'+layer_id).load(page+'?'+query);
}
/**
 * change class by given id and class name
 * 
 * @param id
 * @param class_name
 * @return
 */
function changeClass(id, class_name) {
	document.getElementById(id).className = class_name;
}
/**
 * check if value is number or not
 * 
 * @param sText
 * @return bool
 */
function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
/**
 * empty any div by get the id
 * 
 * @param id
 * @return
 * 
 * function emptyDiv(id) { document.getElementById(id).innerHTML = ''; }
 */
cell = false;
/**
 * check the cell phone is numeric and 10 digits
 * 
 * @param id
 * @param phone
 * @return
 */
function checkCellPhone(id, phone) {

	if (phone != '') {
		if (IsNumeric(phone)) {
			if (phone.length == 10) {
				document.getElementById(id).innerHTML = '<img src="images/correct.png" align="absmiddle"> <span class="correct">the cell phone is correct</span>';
				cell = true;
			} else {
				document.getElementById(id).innerHTML = '<img src="images/error.png" align="absmiddle"> <span class="error">rewrite the cell phone correctly</span>';
				cell = false;
			}
		} else {
			document.getElementById(id).innerHTML = '<img src="images/error.png" align="absmiddle"> <span class="error">rewrite the cell phone correctly</span>';
			cell = false;
		}
	}
}
/**
 * get any ajax page by giving the page and query without output data
 */
function ajaxPageNoOutput(page, query) {
	xmlhttp.open("GET", page + '?' + query);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			location.reload(true);
		}
	}
	xmlhttp.send(null);
}
/**
 * change coupon status
 * @param message
 * @param status
 * @param id
 * @return
 */
function changeStatus(message, status, id) {
	var msg = confirm(message);
	if (msg)
		ajaxPageNoOutput('ajax/coupon.ajax.php', 'id=' + id + '&status='
				+ status);
}
/**
 * get the page with query and insert it on the div
 * 
 * @param layer_id
 * @param page
 * @param query
 * @return
 */
function ajaxPageLoad(layer_id, page, query) {
	xmlhttp.open("GET", page + '?' + query);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById(layer_id).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}
/**
 * open pop up window
 * @param URL
 * @return
 */
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=520,height=440,left = 0,top = 790');");
	}
/**
 * print pop up window and close it after print
 * @return
 */
function printCoupon()
{
	window.print();
	window.close();
}
/**
 * redirect page with wait time param
 * @param url
 * @param time
 * @return
 */
function redirectPage(url,time)
{
	setTimeout(window.top.location = url, time);
}
