/*
 * Libraries of the global client functions.
 */

/* 
 * Function to set cookie
 */
function Set_Cookie(name,value,expires,path,domain,secure) {
	document.cookie = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires.toGMTString() : "") +
		( (path) ? ";path=" + path : "") + 
		( (domain) ? ";domain=" + domain : "") +
		( (secure) ? ";secure" : "");
}

/* 
 * Function to read cookie
 */
function Get_Cookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;    var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

Set_Cookie('MasterCookie','MasterCookie');

// Check cookies
if (!Get_Cookie('MasterCookie'))
{
	alert("Dear User! Cookies must be activated in your browser.");
}

var alertdisplay = true;
var isError = false;

function getPosition (width, height) {
	var top = (screen.availHeight - height)/2;
	var left = (screen.availWidth - width)/2;
	return ",top=" + top + ",left=" + left;
}

var lastwin = null;

function makewin (daLink, win, options) {
	var namewin = "_blank";
	if (screen.availWidth > 800)
		var dim = ",width=710,height=560";
	else
		var dim = ",width=710,height=540";
	var pos = getPosition(710, 560);
	var optionswin = "toolbar=0,directories=0,status=0,menubar=0,copyhistory=0,screenx=30,screeny=20,scrollbars=yes,dependent=yes,resizable=yes" + pos + dim;
	if (win != null)
		namewin = win;
	if (options != null)
		optionswin = options;

	var lnkwin = window.open(daLink,namewin,optionswin);
//	var lnkwin = window.open(daLink,"_blank","toolbar=1,directories=1,status=0,menubar=1,copyhistory=0,screenx=30,screeny=20,width=710,height=470,scrollbars=yes,dependent=yes");	
	lastwin = lnkwin;
}

function makewin2 (daLink, win) {
	var namewin = "_blank";
	if (win != null)
		namewin = win;
	var namewin = "_blank";
	if (screen.availWidth > 800) {
		var dim = ",width=710,height=560";
		var pos = getPosition(710, 670);
	} else {
		var dim = ",width=710,height=470";
		var pos = getPosition(710, 580);
	}
	var lnkwin = window.open(daLink,namewin,"toolbar=1,directories=0,status=0,menubar=0,copyhistory=0,scrollbars=yes,dependent=yes,resizable=yes" + dim + pos);
}

function makewin3 (daLink, win) {
	var namewin = "_blank";
	if (win != null)
		namewin = win;
	var namewin = "_blank";
	if (screen.availWidth > 800) {
		var dim = ",width=710,height=560";
		var pos = getPosition(710, 670);
	} else {
		var dim = ",width=710,height=470";
		var pos = getPosition(710, 580);
	}
	var lnkwin = window.open(daLink,namewin,"resizable=1,toolbar=1,directories=0,status=0,menubar=1,copyhistory=0,scrollbars=yes,dependent=yes" + dim + pos);
}

function makewin4 (daLink, win) {
	makewin(daLink,win,"toolbar=0,directories=0,status=0,menubar=0,copyhistory=0,top=20,left=30,screenx=30,screeny=20,width=838,height=614,scrollbars=no,dependent=yes");
}

function isImage (ctrl) {
	var val = ctrl.value;
	val = val.toLowerCase();
	if (val != "") {
		if (val.lastIndexOf(".gif") > 0 || 
			val.lastIndexOf(".jpg") > 0 || 
			val.lastIndexOf(".jpeg") > 0 ||
			val.lastIndexOf(".png") > 0)
			return true;
		else {
			alert("Please select image file in gif, jpg or png formats.");
			ctrl.focus();
			return false;
		}
	}
	return true;
}

function isAudio (ctrl) {
	var val = ctrl.value;
	val = val.toLowerCase();
	if (val != "") {
		if (val.lastIndexOf(".mp3") > 0 || 
			val.lastIndexOf(".wav") > 0 || 
			val.lastIndexOf(".snd") > 0 ||
			val.lastIndexOf(".au") > 0 || 
			val.lastIndexOf(".aif") > 0 || 
			val.lastIndexOf(".aifc") > 0 || 
			val.lastIndexOf(".aiff") > 0 || 
			val.lastIndexOf(".wma") > 0 
		)
			return true;
		else {
			alert("Please select audio file in mp3, wav, wma or snd formats.");
			ctrl.focus();
			return false;
		}
	}
	return true;
}

function onTranslate() {
	if (screen.availWidth > 800)
		var dim = ",width=790,height=560";
	else
		var dim = ",width=790,height=540";
	var pos = getPosition(790, 560);
	var optionswin = "toolbar=0,directories=0,status=0,menubar=0,copyhistory=0,screenx=30,screeny=20,scrollbars=yes,dependent=yes,resizable=yes" + pos + dim;
	makewin('../popup/translatetext.aspx','translate',optionswin);
}

function onTranslate1() {
	if (screen.availWidth > 800)
		var dim = ",width=790,height=560";
	else
		var dim = ",width=790,height=540";
	var pos = getPosition(790, 560);
	var optionswin = "toolbar=0,directories=0,status=0,menubar=0,copyhistory=0,screenx=30,screeny=20,scrollbars=yes,dependent=yes,resizable=yes" + pos + dim;
	makewin('popup/translatetext.aspx','translate',optionswin);
}


/* 
 * Select value in the list
 */
function selectValue(list, selValue)
{
	for (i = 0; i < list.options.length; i++) {
		var oOption = list.options[i];
		var value = oOption.value;
		if (selValue == value) {
			oOption.selected = true;
			return;
		}
	}
}

/* 
 * Add option in the list
 */
function addOption(obj, options)
{
    var option = new Option();
	option.text = obj.name;
	option.value = obj.id;
    options[options.length] = option;
}

function showLogin(action, fromLogin, source, target) {
	// action = 0 - Open Logon Window
	// action = 1 - Close Logon Window
	// action = 2 - No Logon Window
	
	var secur = "";
	
	if (action == 0) {
		if (fromLogin == false) {
			makewin('popup/login.aspx?target=' + escape(target));
			if (source == "")
				window.location.href = "index.aspx" + secur;
			else
				window.location.href = source;
		}
		
	}
	else if (action == 1) {
		if (fromLogin == true) {	
			if (target == "")
				window.opener.location.href = "../index.aspx" + secur;
			else
				window.opener.location.href = target;
			window.close();			
		}
	}
}

function showStatus() {
	makewin('popup/member_profile.aspx'); // + window.location);
}

function showOrder(url) {
	this.close();
	makewin3(url);
}

function setFileName(value, obj) {
	var result = value.replace(/ /g, "");
	result = result.replace(/'/g, "");
	result = result.replace(/\"/g, "");
	result = result.replace(/&/g, "");
	result = result.replace(/\?/g, "");
	result = result.replace(/\./g, "");
	result = result.replace(/#/g, "");
	result = result.replace(/@/g, "");
	result = result.replace(/,/g, "");
			
	obj.value = result.toLowerCase() + ".html";
}

function backHome() {
	try	{
		window.opener.location.href = "../index.aspx";
		window.close();
	}
	catch(e) {
		window.close();
	}
	
}

function Buffer() {
	document.write('<IFRAME src="../images/space.gif" name="__buffer" id="__buffer" style="width:0; height:0; visibility: "hidden";"></IFRAME>');//hidden
//	document.write('<IFRAME src="../images/space.gif" name="__buffer" id="__buffer" style="width:200; height:300; visibility: "hidden";"></IFRAME>');//hidden
}

function DataBuffer(url, notifyMethod, timeoutValue, timeoutMethod, timeoutContainer) {
	DataBuffer.prototype.arrived = arrived;
	DataBuffer.prototype.alertTimeout = alertTimeout;
	DataBuffer.prototype.stopTimeout = stopTimeout;

	var timeout = null;
	var container = document.getElementById("__buffer");

	container.src = url;
	if (timeoutValue != null)
		timeout = setTimeout("DataBuffer.prototype.alertTimeout()", timeoutValue * 1000);

	function arrived(data) {
		if (timeout != null)
			clearTimeout(timeout);
		notifyMethod(data);
	}

	function alertTimeout() {
		container.src = "../images/space.gif";
		if (timeoutMethod != null)
			timeoutMethod(timeoutContainer);
	}
	
	function stopTimeout() {
		if (timeout != null)
			clearTimeout(timeout);
	}
}
