/*
    * Author               :  Shanmugaraja
    * Date of Creation     :  26-Apr-2002.
    * Last Modified        :  17 Dec 2002.
    * Modified by          :  Shanmugaraja
*/



/* This function is used to display the month link */
function DisplayCalendar1(pageName, year, month)
{
    var month_diff;
    var valid = true;
    var current_date = new Date();

	month_diff=DateDifference(month, year);

	if (current_date.getFullYear()<year || (current_date.getFullYear()==year
		&&(current_date.getMonth()+1)<=month))
	{
		url_str=(parseInt(month_diff))+'m.';
	}
	else
	{
		url_str='n'+(parseInt(month_diff))+'m.';
	}

	document.frmCalendar.action=url_str+'1m.'+pageName;
	//document.frmCalendar.submit();
	window.location=url_str+'1m.'+pageName;
	return true;
}


/* This function is used to display the next
 * and previous month calendar */

function DisplayCalendar(pageName, move)
{
    var month_diff;
    var valid = true;
    var eregnum = new RegExp("^[0-9]{4}$");
    var current_date = new Date();
    var month = document.frmCalendar.cmbMonth.options[document.frmCalendar.cmbMonth.selectedIndex].value;
    var year = document.frmCalendar.txtYear.value;

    if(!year.match(/^[0-9]{4}$/))
    {
        alert("Please enter a 4 digit number");
        document.frmCalendar.txtYear.focus();
        valid = false;
    }
    else if((year < 1970 && !(year==1969 && month==12 && move=='1')) ||
             (year > 2037 && !(year==2038 && month==1 && move=='-1'))||
             (year==1970 && month==1 && move=='-1') ||
             (year==2037 && month==12 && move=='1'))
    {
        alert("Please enter an year between 1969 and 2038");
        document.frmCalendar.txtYear.focus();
        valid = false;
    }
    else
    {
        month_diff=DateDifference(month, year);

        if (current_date.getFullYear()<year || (current_date.getFullYear()==year
            &&(current_date.getMonth()+1)<=month))
        {
            if (parseInt(month_diff)==0 && move=='-1')
            {
                url_str='n1m'
            }
            else
            {
                url_str=(parseInt(month_diff)+parseInt(move))+'m';
            }
        }
        else
        {
            if (parseInt(month_diff)==1 && move=='1')
            {
                url_str='0m'
            }
            else
            {
                url_str='n'+(parseInt(month_diff)-parseInt(move))+'m';
            }
        }

        document.frmCalendar.action=pageName.replace(/^(n)*[0-9]+[m]+/, url_str);
        document.frmCalendar.submit();
    }
    if (!valid && move==0)
    {
        return false;
    }
}

//This function is to find the difference in time in months
function DateDifference(month, year)
{
    month=parseInt(month);
    year=parseInt(year);
    var current_date=new Date();
    var year_diff = year - current_date.getFullYear();
    var diff_month=0;
    var curr_month = current_date.getMonth()+1;

    if( year_diff >= 0)
    {
        diff_month =Math.abs(12-curr_month+month+(year_diff-1)*12);
    }
    else
    {
        diff_month =Math.abs(12-month)+curr_month+(Math.abs(year_diff)-1)*12;
    }

    return diff_month;
}

//This function is to set value for the month combo
function SetMonthValue()
{
    var month;
    month = document.frmCalendar.hidMonth.value;

    if (document.frmCalendar.hidMonth.value.length > 1 &&
    document.frmCalendar.hidMonth.value.substr(0,1)=='0')
    {
        month = document.frmCalendar.hidMonth.value.substr(1);
    }

    for(i=0;i<document.frmCalendar.cmbMonth.length;i++)
    {
        if (document.frmCalendar.cmbMonth.options[i].value==month)
        {
            document.frmCalendar.cmbMonth.selectedIndex = i;
        }
    }
}

//This function is used to display the description.
function DisplayDescription(img_path,img_name,img_position,title,title_url,desc, url, char_count, char_align_count)
{
	var rel_char_count;
	var item_detail;

	//replace the p tags at the begining and end.
	desc=desc.replace(/^\<p[^\>]*\>/,'');
	desc=desc.replace(/\<\/p\>$/,'');

	desc=desc.replace(/&nbsp;/g,' ');
	desc=desc.replace(/&quot;/g,'"');
	desc=desc.replace(/&ldquo;/g,'"');
	desc=desc.replace(/&rdquo;/g,'"');
	desc=desc.replace(/&rsquo;/g,"'");

	item_detail = '';

	//if the image name is not null, align is as per the requirement.
	if (img_name!=' ' && img_name!='')
	{
		//align right only if the description have enough characters
		if (img_position == 'right' && GetDescLength(desc,0,1) > char_align_count)
		{
			item_detail = '<img src="'+img_path+img_name+'" align="right" border="0"/>';
		}
		else
		{
			item_detail = '<img src="'+img_path+img_name+'" align="left" border="0"/>';
		}
	}

	//if the title url is not null, give hot link to title.
	if (title_url!='')
	{
		if (title_url.search(/\/\//) != -1)
		{
			item_detail += '<a href="'+title_url+'"><B>'+title.replace(/`/g,"'")+'</B></a><br>';
		}
		else
		{
			item_detail += '<a href="//'+title_url+'"><B>'+title.replace(/`/g,"'")+'</B></a><br>';
		}
	}
	else
	{
		item_detail += '<font color="#000000"><B>'+title.replace(/`/g,"'")+'</B></font><br>';
	}

	if (char_count == 0)
	{
		item_detail += desc
		item_detail=item_detail.replace(/`/g,"'");
		document.write(item_detail);
		return;
	}

	rel_char_count=GetDescLength(desc, char_count,0);
	item_detail += desc.substring(0,rel_char_count);
	item_detail += '</b></i></a>';


	if (GetDescLength(desc,0,1) > char_count)
	{
		item_detail += ' <a href="'+url+'">[Continue]</a>';
	}
	
	item_detail = item_detail.replace(/`/g,"'");
	document.write(item_detail);
}


function DisplayEventTitle(title,char_count)
{
	var rel_char_count;
	var item_detail;

	//replace the p tags at the begining and end.
	title=title.replace(/^\<p[^\>]*\>/,'');
	title=title.replace(/\<\/p\>$/,'');

	title=title.replace(/&nbsp;/g,' ');
	title=title.replace(/&quot;/g,'"');
	title=title.replace(/&ldquo;/g,'"');
	title=title.replace(/&rdquo;/g,'"');
	title=title.replace(/&rsquo;/g,"'");

	item_detail = '';

	item_detail += title.replace(/`/g,"'");

	if (char_count == 0)
	{
		//item_detail += title
		item_detail=item_detail.replace(/`/g,"'");
		document.write(item_detail);
		return;
	}

	rel_char_count=GetDescLength(title, char_count,0);
	item_detail = title.substring(0,rel_char_count);

	if (GetDescLength(title,0,1) > char_count)
	{
		item_detail += '...';
	}
	
	item_detail = item_detail.replace(/`/g,"'");

	document.write("<font size='1' face='Arial,Helvetica,Geneva,Swiss,SunSans-Regular' color='#000000'>"+item_detail+"</font>");
}



function GetDescLength(desc, char_count, abs_char_flag)
{
	var i;
	var str_count = 0;
	var str_count_flag = true;

	for (i=0;i<desc.length ; i++)
	{
		//if any html tag starts then stop counting the characters
		if (desc.charAt(i)=='<')
		{
			str_count_flag=false;
		}
		//count the characters out side the html tags
		if (str_count_flag==true)
		{
			str_count++;
		}

		/*break the description at a space after the required character
		  and out side the html tags. */
		if (str_count >= char_count && desc.charAt(i)==' ' && str_count_flag==true && char_count!=0)
		{
			break;
		}

		//start counting the characters as soon as the html tag ends.
		if (desc.charAt(i)=='>')
		{
			str_count_flag=true
		}
	}
	if (abs_char_flag==1)
	{
		return str_count;
	}
	else
	{
		return i;
	}
}

//this function is used to assign form action and submit the form.
function SetFormAction(formName,formValue)
{
    eval("document."+formName+".action = '"+formValue+"';");
    eval("document."+formName+".submit();");
}

function Search()
{
    words = document.frmHeader.words.value;
    document.frmHeader.action="http://search.tagnet.org/search.cgi?q="+words;
    document.frmHeader.submit();
}

//function to call the life talk live radio
function funLifeTalk()
{
    window.open('http://www.lifetalk.net/listenHere.html','popupWin','status=no,scrollbars=no,width=470,height=295,left=300,top=200,resizable=no');
}

//For rollover color change
function rolloverchange(tableCell,changeFlag,imgpath) {
	if (changeFlag==1) 
	{
		//tableCell.style.backgroundImage = 'url("http://topics.tagnet.org/images/rollovercolor.gif")';
		tableCell.style.backgroundImage = 'url("'+imgpath+'rollovercolor.gif")';
	}
	else 
	{
		//tableCell.style.backgroundImage = 'url("http://topics.tagnet.org/images/rollovercolor-1.gif")';
		tableCell.style.backgroundImage = 'url("'+imgpath+'rollovercolor-1.gif")';
	}
}


function OpenURL(url)
{
    var regex_null = new RegExp('^ *$');
    if (regex_null.test(url))
    {
        window.open("about:blank", 'test_window');
    }
    else if (url.search("://") == -1)
    {
        window.open("//"+url, 'test_window');
    }
    else
    {
        window.open(url, 'test_window');
    }
}
