// #############################################################################
// lets define the browser we have instead of multiple calls throughout the file
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));

// catch possible bugs with WebTV and other older browsers
var is_regexp = (window.RegExp) ? true : false;

// #############################################################################
// let's find out what DOM functions we can use
var DOMtype = '';
if (document.getElementById)
{
	DOMtype = "std";
}
else if (document.all)
{
	DOMtype = "ie4";
}
else if (document.layers)
{
	DOMtype = "ns4";
}

// make an array to store cached locations of objects called by fetch_object
var Myobjects = new Array();

// #############################################################################
// function to emulate document.getElementById
function fetch_object(idname, forcefetch)
{
	if (forcefetch || typeof(Myobjects[idname]) == "undefined")
	{
		switch (DOMtype)
		{
			case "std":
			{
				Myobjects[idname] = document.getElementById(idname);
			}
			break;

			case "ie4":
			{
				Myobjects[idname] = document.all[idname];
			}
			break;

			case "ns4":
			{
				Myobjects[idname] = document.layers[idname];
			}
			break;
		}
	}
	return Myobjects[idname];
}

// #############################################################################
// function to emulate document.getElementsByTagName
function fetch_tags(parentobj, tag)
{
	if (typeof parentobj.getElementsByTagName != 'undefined')
	{
		return parentobj.getElementsByTagName(tag);
	}
	else if (parentobj.all && parentobj.all.tags)
	{
		return parentobj.all.tags(tag);
	}
	else
	{
		return null;
	}
}

// array to store open attachment window references
var openmanage = new Array();

// #############################################################################
function manageattachments(url, width, height, hash)
{
	if (typeof(openmanage[hash]) != 'undefined' && openmanage[hash].closed == false)
	{
		openmanage[hash].focus();
	}
	else
	{
		openmanage[hash] = window.open(url, "Attach" + hash, "statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height);
	}
	return false;
}

// #############################################################################
// function to open a generic window
function openWindow(url, width, height)
{
	var dimensions = "";
	if (width)
	{
		dimensions += ",width=" + width;
	}
	if (height)
	{
		dimensions += ",height=" + height;
	}
	window.open(url, "Popup", "statusbar=no,menubar=no,toolbar=no,scrollbars=yes,resizable=yes" + dimensions);
	return false;
}

// #############################################################################
// function to do a single-line conditional
function iif(condition, trueval, falseval)
{
	return condition ? trueval : falseval;
}

// #############################################################################
// function to search an array for a value
var imagetypes = new Array('gif', 'jpg', 'jpeg', 'jpe', 'png');

function in_array(ineedle, haystack, caseinsensitive)
{
	var needle = new String(ineedle);

	if (caseinsensitive)
	{
		needle = needle.toLowerCase();
		for (i in haystack)
		{
			if (haystack[i].toLowerCase() == needle)
			{
				return i;
			}
		}
	}
	else
	{
		for (i in haystack)
		{
			if (haystack[i] == needle)
			{
				return i;
			}
		}
	}
	return -1;
}

// #############################################################################
// function to checkall
function CheckAll(formobj) 
{
	for (var i=0;i<formobj.elements.length;i++) 
	{
		var e = formobj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox')) 
		{
			e.checked = formobj.allbox.checked;
		}
	}
}

// #############################################################################
// function to CheckCheckAll
function CheckCheckAll(formobj) 
{
	var TotalBoxes = 0;
	var TotalOn = 0;
	for (var i=0;i<formobj.elements.length;i++) {
		var e = formobj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox')) 
		{
			TotalBoxes++;
			if (e.checked) 
			{
				TotalOn++;
			}
		}
	}
	if (TotalBoxes==TotalOn) 
	{
		formobj.allbox.checked=true;
	}
	else 
	{
		formobj.allbox.checked=false;
	}
}

// #############################################################################
// emulation of the PHP version of  construct_phrase() sprintf wrapper
function construct_phrase()
{
	if (!arguments || arguments.length < 1 || !is_regexp)
	{
		return false;
	}

	var args = arguments;
	var str = args[0];
	var re;

	for (var i = 1; i < args.length; i++)
	{
		re = new RegExp("%" + i + "\\$s", "gi");
		str = str.replace(re, args[i]);
	}
	return str;
}

// #############################################################################
// function to handle the different event models of different browsers
// and prevent event bubbling
function do_an_e(eventobj)
{
	if (!eventobj || is_ie)
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		return window.event;
	}
	else
	{
		eventobj.stopPropagation();
		eventobj.preventDefault();
		return eventobj;
	}
}

// #############################################################################
// set control panel frameset title
function set_cp_title()
{
	if (typeof(parent.document) != "undefined" && typeof(parent.document) != "unknown" && typeof(parent.document.title) == "string")
	{
		if (document.title != '')
		{
			parent.document.title = document.title;
		}
		else
		{
			parent.document.title = "51.CA";
		}
	}
}

// #############################################################################
// open control panel help window
function js_open_help(scriptname, actiontype, optionval)
{
	window.open(homeurl + "help.php?s=" + SESSIONHASH + "&do=answer&page=" + scriptname + "&pageaction=" + actiontype + "&option=" + optionval, "helpwindow", "toolbar=no,scrollbars=yes,resizable=yes,width=600,height=450");
}

// #############################################################################
// simple function to toggle the 'display' attribute of an object
function toggle_display(idname)
{
	obj = fetch_object(idname);
	if (obj)
	{
		if (obj.style.display == "none")
		{
			obj.style.display = "";
		}
		else
		{
			obj.style.display = "none";
		}
	}
	return false;
}

// #################################  Cookie Functions ############################
function set_cookie (name, value, seconds)
{
	var argv = set_cookie.arguments;
	var argc = set_cookie.arguments.length;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;

	if(seconds > 0)
	{
		expires = new Date();
		expires.setTime(expires.getTime() + seconds*1000);
	}

	document.cookie = name + "=" + escape (value) + 
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

}

// #############################################################################
// function to retrieve a cookie
function fetch_cookie(name)
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

// #############################################################################
// function to delete a cookie
function delete_cookie(name)
{
	var expireNow = new Date();
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}


// #############################################################################
function setSelectOptions(the_form, the_select, do_check)
{
    var selectObject = document.forms[the_form].elements[the_select];
    var selectCount  = selectObject.length;

    for (var i = 0; i < selectCount; i++) {
        selectObject.options[i].selected = do_check;
    } // end for

    return true;
}

// #############################################################################
function delete_word(frmObject,intValue)
{
	if(frmObject.value == intValue)
	{
		frmObject.value = "";
	}
}

// #############################################################################
function add_word(frmObject,intValue)
{
	if(frmObject.value.length < 1)
	{
		frmObject.value = intValue;
	}
} 

// #############################################################################
// zoom font
var NowFontSize = 14.8;
function doZoom(idname)
{
	fontObj = fetch_object(arguments[0]).style ;
	if (arguments[1] == "Minus")
	{
		if (NowFontSize <= 12.8) return ;
		fontObj.fontSize = (NowFontSize-2) + "px" ;
		NowFontSize = eval(NowFontSize-2) ;
	}
	else if (arguments[1] == "Plus")
	{
		if (NowFontSize >= 20.8) return ;
		fontObj.fontSize = (NowFontSize+2) + "px" ;
		NowFontSize = eval(NowFontSize+2) ;
	}
}


// #############################################################################
// 文字长度计算
function MyStrLen(str)
{
	str = MyTrim(str, true);

	return str.replace(/[^\x00-\xff]/g, "**").length;
}

// #############################################################################
// 删除头部的空白字串
function MyLTrim(str)
{
	//return str = str.replace(/^[\s　]+/g, "");
	return str = str.replace(/^[\s]+/g, "");
}

// 删除尾部的空白字串
function MyRTrim(str)
{
	//return str = str.replace(/[\s　]+$/g, "");
	return str = str.replace(/[\s]+$/g, "");
}

// 将其他空白字串带换成单一半形空格
function MyTrimRedupli(str)
{
	//return str = str.replace(/[\s　]+/g, " ");
	return str = str.replace(/[\s]+/g, " ");
}

// 左右空格全部删除
function MyTrim(str, trimRedupli)
{
	str = MyLTrim(str);
	str = MyRTrim(str);	
	if(trimRedupli)
	{
		str = MyTrimRedupli(str);
	}
	return str;
}

// #############################################################################
// 转换大小写
function fucCvtBigCh(str){
	var newstr='';
	var cnstr='　０１２３４５６７８９ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ｀～＠＃＄％︿＆＊＿－＝＋［｛］｝＼｜＇＂＜＞／';
	enarray=new Array(' ','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','`','~','@','#','$','%','^','&','*','_','-','=','+','[','{',']','}','\\','|','\'','\"','<','>','\/');

	for (var i=0;i<str.length;i++)
	{
		if (cnstr.indexOf(str.charAt(i))!=-1)
		{
			//说明有字符是全角
			newstr+=enarray[cnstr.indexOf(str.charAt(i))];
		}else{
			newstr+=str.charAt(i);
		}
	}

	return newstr;
}

// #############################################################################
// Tab Scripts

var tabmorelink = 'http://www.51.ca/';

function switchtab(displayid, targetobj, morelink)
{
	var ullist = targetobj.parentNode.parentNode.getElementsByTagName("li");

	for (var i=0; i< ullist.length; i++)
	{
		//deselect all tabs
		ullist[i].className = "" 
	}
	
	//highlight currently clicked on tab
	targetobj.parentNode.className = "selected";

	var displayobj = fetch_object(displayid);

	var ullist = displayobj.parentNode.getElementsByTagName("ul");
	for (var i=0; i< ullist.length; i++)
	{
		//deselect all tabs
		ullist[i].style.display = 'none';
	}
	
	displayobj.style.display = 'block';

	tabmorelink = morelink;
}

function switchtab_more(morelink) {
	if('' == tabmorelink)
		tabmorelink = morelink;
	
	if('' == tabmorelink)
		return;

	window.open(tabmorelink, "Popup");
}