// $Id: scripts.js,v 1.3 2010/03/19 16:54:52 jyu Exp $

var current_tabs = new Array()

function get_tab(tabid, containerid)
{
	if ( current_tabs[containerid] == null ) current_tabs[containerid] = 1;
	document.getElementById(containerid+current_tabs[containerid]).style.display = 'none';
	document.getElementById(containerid+tabid).style.display = 'block';
	document.getElementById(containerid+'tab'+current_tabs[containerid]).removeAttribute('className');
	document.getElementById(containerid+'tab'+current_tabs[containerid]).removeAttribute('class');
	document.getElementById(containerid+'tab'+tabid).setAttribute('className', 'current');
	document.getElementById(containerid+'tab'+tabid).setAttribute('class', 'current');
	current_tabs[containerid] = tabid;
	return false;
}




function newwin(targetUrl,width,height)
{
	var currentTime = new Date();
	var winId = currentTime.getHours()+currentTime.getMinutes()+currentTime.getSeconds();
	var win_params="width="+width+",height="+height+",status=no,scrollbars=yes,directories=no,location=no,menubar=yes,toolbar=no,resizable=yes,screenX=200,screenY=5,left=200,top=5";
	var win=window.open("", "popwin"+winId, win_params, true);
	win.location = targetUrl;
	var browser = navigator.appName;
	win.focus();
}

////////////////////////////////////
// for copyright request
function request_page(data)
{
	requestWindow = window.open('/common/requestarticle.php?' + data, 'email_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=540,height=425');
}

////////////////////////////////////
// image popup

function image_popup(file,width,height)
{
	var newHeight = Number(Number(height) + 20);
	var newWidth = Number(Number(width) + 20);
	imgWin = window.open('/common/imagepopup.php?img='+file+'&w='+width+'&h='+height, 'Image', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+newWidth+',height='+newHeight);
	imgWin.moveTo(30,30);
	imgWin.focus();
}


////////////////////////////////////
// email popup

function email_page(data)
{
	emailWindow = window.open('/common/emailarticle.php?emailinfo=' + data, 'email_window', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=512,height=430');
}

////////////////////////////////////
// printer-friendly version popup

function print_page(url)
{
	var opts = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=536,height=500';
	printWindow = window.open(url, 'print_window', opts);
}

function checkEmailForm(theForm)
{
	var TO		= theForm.TO.value;
	var FROM	= theForm.FROM.value;
	var BCC		= theForm.BCC.value;
	var regExp	= /[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})/; // email validator

	toValid =	TO.search(regExp);
	fromValid =	FROM.search(regExp);
	bccValid =	BCC.search(regExp);

	if (TO == "") {
		theForm.TO.focus();
		alert("You must first enter the email address of the person \rto whom you would like to send this article.");
		return false;
	} else if (toValid == -1) {
		theForm.TO.focus();
		alert("You must first enter a valid email address in the \"To\" field.");
		return false;
	} else if (FROM == "") {
		theForm.FROM.focus();
		alert("You must first enter your email address in the \"Your Email Address\" field.");
		return false;
	} else if (fromValid == -1) {
		theForm.FROM.focus();
		alert("You must first enter a valid email address in the \"Your Email Address\" field.");
		return false;
	} else if (BCC != "" && bccValid == -1) {
		theForm.BCC.focus();
		alert("You must first enter a valid email address in the \"Bcc\" field.");
		return false;
	}
	return true;
}

////////////////////////////////////
// font-sizing

var isIE = (navigator.appName == 'Microsoft Internet Explorer');
var factor = 0;
var adj = 0;
var units = 4;

function setElement(obj, addl)
{
	var re = /^(a|b|i|em|strong|span|p)$/i;
	if (obj.style && ( obj.nodeType == 1 || obj.nodeType == 3 ) ) {
		if (re.test(obj.tagName) && obj.className!='header')
			return;
		if ( isIE )
			z = obj.currentStyle;
		else
			z = window.getComputedStyle(obj,null);
		newFsize = (parseInt(z.fontSize)+addl)+'px';
		obj.style.fontSize = newFsize;
		//obj.style.lineHeight = 'auto';
		for (var y=0; y<obj.childNodes.length; y++)
			setElement(obj.childNodes[y], addl); // recursive
	}
}

function changeSize(opt)
{
	var head = document.getElementById('storyheader');
	var block = document.getElementById('storybody');
	var addl = 0;

	if (opt>factor && factor<2) { // increase
		addl = (opt-factor)*units;
		factor = opt;
	}
	else if (opt<factor) { // decrease
		addl = ((factor-opt)*units)*-1;
		factor = opt;
	}
	else {
		return; // no change
	}
	setElement(head,addl);
	setElement(block,addl);
}


////////////////////////////////////
// Story Toolbox

var storytabs = [ '1','2','3','4','5' ];

function set_tabs(id)
{
	for (var i in storytabs) {
		if (storytabs[i]==id) {
			document.getElementById('tab'+storytabs[i]).className = 'current';
			document.getElementById('block'+storytabs[i]).style.display = 'block';
		}
		else {
			document.getElementById('tab'+storytabs[i]).className = 'NULL';
			document.getElementById('block'+storytabs[i]).style.display = 'none';
		}
	}
}

function process_email(theForm)
{
	checkEmailForm(theForm);
	callACAC('/common/emailarticle.php', getPOSTParams(theForm), 'block1', 'POST');
	emailref = true;
}

function story_get_tab(id)
{
	set_tabs(id);
	switch (id) {
		case '1': // email
			if (emailref == true) {
				callACAC('/common/emailarticle.php', 'data=' + emailvals, 'block1');
				emailref = false;
			}
			break;
		case '2':  // comments
			if (commref == true) {
				getCommentForm(rurl, iid);
				commref = false;
			}
			break;
		case '3': // print
			break; // just switch tab
		case '4': // reuse
			request_page('{emailmask}');
			break; // just switch tab
	}
}

////////////////////////////////////
// PowerNews Email Feature

function email_nl(emailvals)
{
	var b = document.getElementById('block1');
	b.style.border = '1px solid #E9E8E8';
	b.style.padding = '18px';
	callACAC('/common/emailarticle.php', 'data=' + emailvals, 'block1');
	emailref = false;
}

////////////////////////////////////
// Ajax Functions

function XHR()
{
	try {
		obj = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(e) {
		try {
			obj = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
		} catch (e) {
			try {
				obj = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
			} catch (E) {
				obj = false;
			}
		}
	}
	return obj;
}

function SetDiv(obj)
{
	if(obj.req.readyState == 4) {
		if(obj.req.status == 200) {
			obj.div.innerHTML = obj.req.responseText;
		} else {
			if (err_msg)
				obj.div.innerHTML = err_msg+"\n"+obj.req.responseText;
		}
	}
}

function AlertResult(obj)
{
	if(obj.req.readyState == 4)
		if (obj.req.status == 200)
			alert(obj.req.responseText); // what to do as a result
}

function AjaxGet(urlstr, div)
{
	this.div = document.getElementById(div);
	if (arguments[2])
		this.div.innerHTML = arguments[2];
	this.req = XHR(); // OK
	this.req.open('GET', urlstr, true);
	var obj = this;
	this.req.onreadystatechange = function() { SetDiv(obj); };
	this.req.send(null);
}

function AjaxFetch(urlstr, funcname)
{
	this.req = XHR(); // OK
	this.req.open('GET', urlstr, true);
	var obj = this;
	eval('var func = '+funcname+';');
	this.req.onreadystatechange = function() { func(obj); };
	this.req.send(null);
}

function AjaxPost(urlstr, params, div)
{
	this.div = document.getElementById(div);
	this.req = XHR(); // OK
	this.req.open('POST', urlstr, true);
	var obj = this;
	this.div.innerHTML = ' ';
	this.req.onreadystatechange = function() { SetDiv(obj); };
	this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	this.req.setRequestHeader("Content-length", params.length);
	this.req.setRequestHeader("Connection", "close");
	this.req.send(params);
}


function get_latest(key, type)
{
	AjaxPost('/lateststories.php', 'ajax=1&key=' + key + '&type=' + type, 'latestStoriesBlock');
}



function checkInfoRequest(theForm)
{
	var firstname = theForm.firstname.value;
	var lastname = theForm.lastname.value;
	var email = theForm.email.value;
	var company = theForm.company.value;
	var regExp = /[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})/; // email validator

	emailValid = email.search(regExp);

	if (firstname == "" || firstname == "First Name") {
		theForm.firstname.focus();
		alert("Please enter a First Name.");
		return false;
	} else if (lastname == "" || lastname == "Last Name") {
		theForm.lastname.focus();
		alert("Please enter a Last Name.");
		return false;
	} else if (email == "" || email == "Email" || emailValid == -1) {
		theForm.email.focus();
		alert("Please enter a valid e-mail address.");
		return false;
	} else if (company == "" || company == "Company") {
		theForm.company.focus();
		alert("Please enter a Company.");
		return false;
	}
	return true;
}
