/* javascript for general website */

//generic function to xml http request
function GetXmlHttpObject() {
	try { return new XMLHttpRequest(); } catch(e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}

	alert ("Your browser does not support AJAX!");
	return null;
}

//generic function to send post ajax
function sendPostAjax(xmlHttp, url, params){
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

//generic function to display the language text
var language = new Array();
function getLangText(text) {
	var msg = 'Sorry, there is no translation for the text "' + text + '" in javascript block';
	if (LANGUAGE != 'en') {
		if (language[text]) return language[text];
		else return msg;
	} else return text;
}

//generic function to go to the given url
function goUrl(url) {
	document.location.href = url;
	return false;
}

//generic function to go to the given url in new window
function openUrl(url) {
	window.open(url);
	return false;
}

//generic function to go to the given url in a popup
function popupUrl(url) {
    var tmp = timestamp();
    newwin = window.open(url, tmp, 'scrollbars,resizable,width=470,height=435') ;
    newwin.focus() ;
}

//generic function to go get the timestamp
function timestamp() {
    var date = new Date();
    return date.getTime();
}



//function to change the page no of the view for the site
function changeViewPerPages(obj, page, recordno, name){
    xmlHttp=GetXmlHttpObject();
	if (xmlHttp != null) {
		var url = WEBSITE_URL + "ajax/index.html";
		var params = "perpage=" + obj.value + "&option=changeviewperpage" + "&page=" + page + "&recordno=" + recordno + "&name=" + name;
		xmlHttp.onreadystatechange = changeViewPerPagesResponse;
		sendPostAjax(xmlHttp, url, params)
	}
	return false;
}

function changeViewPerPagesResponse() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText.split(AJAX_SEPARATOR);
			if (result[1] == 1) {
				document.location.href = result[2];
			} else {
				alert(result[1]);
			}
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}

//function to delete the records
function deleteRecord(obj) {
	var msg = getLangText("Do you want to delete the record?");
	if(confirm(msg)) {
		document.location.href = obj.href;
	}
	return false;
}

//function to remove the records from shopping cart
function removeRecord(obj) {
	var msg = getLangText("Do you want to remove?");
	if(confirm(msg)) {
		document.location.href = obj.href;
	}
	return false;
}

//function to cancel the records
function cancelRecord(obj) {
	var msg = getLangText("Do you want to cancel the record?");
	if(confirm(msg)) {
		document.location.href = obj.href;
	}
	return false;
}

//function to change the page no of the view for the site
function changeSortPage(name, page){
    xmlHttp=GetXmlHttpObject();
	if (xmlHttp != null) {
		var url = WEBSITE_URL + "ajax/index.html";
		var params = "option=changesort" + "&name=" + name + "&page=" + page;
		xmlHttp.onreadystatechange = changeSortPageResponse;
		sendPostAjax(xmlHttp, url, params)
	}
	return false;
}

function changeSortPageResponse() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var result = xmlHttp.responseText.split(AJAX_SEPARATOR);
			if (result[1] == 1) {
				window.location.reload();
			} else {
				alert(result[1]);
			}
		} else {
			alert("Error: "+ xmlHttp.statusText +" "+ xmlHttp.status);
		}
	}
}