function checkBrowser()
{
	if (!navigator.userAgent) return false;
	var ua = navigator.userAgent;
	var msie = ua.indexOf("MSIE ");
	if (msie == -1) return false;

	if (5 > parseInt(ua.substring (msie+5, ua.indexOf(".", msie))))
		return false;
	
	if (!navigator.platform) return false;
	var win = navigator.platform.indexOf("Win32");
	if (win == -1) return false;

	return true;
}

var g_isIE = checkBrowser();

function checkCookie()
{
	if (!navigator.cookieEnabled) return false;

	var t = new Date();
	document.cookie = "testcookie=" + t.getTime() + "; path=/";
	if (document.cookie.indexOf("testcookie", 0) == -1) return false;

	return true;
}

function setCookie(Param, value, expire_indays)
{
	if (expire_indays)
	{
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 86400*1000*expire_indays);
		document.cookie = Param + "=" + escape(value) + "; expires=" + expire.toUTCString() + "; path=/";
	}
	else
	{
		document.cookie = Param + "=" + escape(value) + "; path=/";
	}
}

function getCookie(label)
{
	label = label + "=";
	var labelLen = label.length;
	var cLen = document.cookie.length;
	var i = 0;
	var cEnd;
	while (i < cLen)
	{
		var j = i + labelLen;
		if (document.cookie.substring(i,j) == label)
		{
			cEnd = document.cookie.indexOf(";",j);
			if (cEnd == -1)
			{
				cEnd = document.cookie.length;
			}
			return unescape(document.cookie.substring(j,cEnd));
		}
		i++;
	}
	return "";
}

function centerElement(e)
{
	var element = YAHOO.util.Dom.get(e);
	var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
	var scrollY = document.documentElement.scrollTop || document.body.scrollTop;

	var viewPortWidth = YAHOO.util.Dom.getViewportWidth();
	var viewPortHeight = YAHOO.util.Dom.getViewportHeight();

	var elementWidth = element.offsetWidth;
	var elementHeight = element.offsetHeight;

	var x = (viewPortWidth / 2) - (elementWidth / 2) + scrollX;
	var y = (viewPortHeight / 2) - (elementHeight / 2) + scrollY;

	var pos = [parseInt(x, 10), parseInt(y, 10)];

	YAHOO.util.Dom.setXY(element, pos);
}

function showErrText(id,len)
{
	var e = YAHOO.util.Dom.get(id);
	e.value = e.value.trim();
	if (e.value == "" || e.value.checkName(len))
	{
		return true;
	}

	showErr(id);
	return false;
}

function showErrEmail(id)
{
	var e = YAHOO.util.Dom.get(id);
	e.value = e.value.trim();
	if (e.value == "" || e.value.checkEmail())
	{
		return true;
	}

	showErr(id);
	return false;
}

function showErrPass(id)
{
	var e = YAHOO.util.Dom.get(id);
	if (e.value == "" || e.value.checkPass())
	{
		return true;
	}

	showErr(id);
	return false;
}

function showErrPhone(id)
{
	var e = YAHOO.util.Dom.get(id);
	e.value = e.value.trim();
	if (e.value == "" || e.value.checkPhone())
	{
		return true;
	}

	showErr(id);
	return false;
}

function showErr(id)
{
	var d = YAHOO.util.Dom.get(id + "_ERR");
	if (!d) return ;
	YAHOO.util.Dom.setStyle(d, "display", "block");
	var region = YAHOO.util.Dom.getRegion(d);

	var r = YAHOO.util.Dom.getRegion(id);
	var xy = [r.left,r.bottom+1];

	var w = (document.documentElement.scrollLeft || document.body.scrollLeft) + YAHOO.util.Dom.getViewportWidth();
	var h = (document.documentElement.scrollTop || document.body.scrollTop) + YAHOO.util.Dom.getViewportHeight();

	var width = region.right - region.left;
	var height = region.bottom - region.top;

	if (xy[0] + width > w && w > width) xy[0] = w-width;
	if (xy[1] + height > h && h > height) xy[1] = h-height;
	YAHOO.util.Dom.setXY(d, xy);
}

function hideErr(id)
{
	YAHOO.util.Dom.setStyle(id + "_ERR", "display", "none");
	var e = YAHOO.util.Dom.get(id);
	e.focus();
	e.select();
	return false;
}


function y2k(number)
{
	return (number < 1000) ? number + 1900 : number;
}

function addTip(id, msg)
{
	YAHOO.util.Event.addListener(id, "mouseover", showTip, msg);
	YAHOO.util.Event.addListener(id, "mouseout", hideTip);
}

function showTip(e, msg)
{
	var d = YAHOO.util.Dom.get("divGKTIP");
	if (!d) return ;
	YAHOO.util.Dom.setStyle(d, "display", "block");
	YAHOO.util.Dom.get("GKTIP").innerHTML = msg;

	var region = YAHOO.util.Dom.getRegion(d);
	var xy = YAHOO.util.Event.getXY(e);
	var w = (document.documentElement.scrollLeft || document.body.scrollLeft) + YAHOO.util.Dom.getViewportWidth();
	var h = (document.documentElement.scrollTop || document.body.scrollTop) + YAHOO.util.Dom.getViewportHeight();

	var width = region.right - region.left;
	var height = region.bottom - region.top;

	if (xy[0] + width > w && w > width) xy[0] = w-width;
	else xy[0] += 15;
	if (xy[1] + height > h && h > height) xy[1] = h-height;
	else xy[1] += 15;
	YAHOO.util.Dom.setXY(d, xy);
}

function hideTip(e)
{
	YAHOO.util.Dom.setStyle("divGKTIP", "display", "none");
}


String.prototype.containsonly = function(validstr)
{
	if (this==validstr || validstr.length==0 || this.length==0) return true;

/*
	var re = validstr;

	if (!re.exec)
	{
		re = /([^\w\s])/gi;
		restr = "[" + validstr.replace(re,"\\ /$1").replace("/\\ \//gi","\\") + "]*";
		re = new RegExp(restr, "gi");
	}

	return(re.exec(this) == this);
*/
}

// returns true if the string contains only alphabetic data or is empty.
String.prototype.isAlpha = function()
{ 
	return(this.containsonly("/[A-Z]*/gi"));
}

// returns true if the string contains only numeric data or is empty.
String.prototype.isNumeric = function()
{ 
	return(this.containsonly("/[0-9]*/gi"));
}


// returns true only if this string contains all the characters in validstr.
String.prototype.containsall = function(validstr)
{
	if ((this.length==0&&this!=validstr)) return(false);
	return(validstr.containsonly ? validstr.containsonly(this) : false );
}


// trims white space off both ends of this string and returns the result.
String.prototype.trim = function()
{  
	return(this.replace("/^\s*([\s\S]*\S+)\s*$|^\s*$/",'$1'));
}



String.prototype.checkEmail = function()
{
	if (this.length > 63) return false;
	return (this.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.info)|(\.us)|(\..{2,2}))$)\b/gi)?true:false);
}

String.prototype.checkPass = function()
{
	if (this.length < 6 || this.length > 31) return false;
	return (this.match(/^[A-Z0-9]*$/gi)?true:false);
}

String.prototype.checkName = function(max_len)
{
	if (this.length > max_len) return false;
	return true;
}

String.prototype.checkPhone = function()
{
	if (this.length > 31) return false;
	var num = this.replace(/[^0-9]/gi, "");
	if (num.length == 11)
	{
		if (num.charAt(0) != "1") return false;
	}
	else if (num.length == 10)
	{
		if (num.charAt(0) == "0") return false;
	}
	else return false;
	return true;
}

String.prototype.checkZIP = function()
{
	if (this.length != 5) return false;
	return (this.match(/^[0-9]{5}$/gi)?true:false);
}

String.prototype.checkAcct = function()
{
	if (this.length != 15 && this.length != 16) return false;
	return(this.containsonly("/[0-9]*/gi"));
}

String.prototype.checkCVV2 = function()
{
	if (this.length != 3 && this.length != 4) return false;
	return(this.containsonly("/[0-9]*/gi"));
}

function YahooMap(url)
{
	var hwndYahoo = window.open(url, "", "width=800,height=600,resizable=1,scrollbars=1,toolbar=1,location=0,directoryes=0,menubar=1,status=0");
	if (!hwndYahoo)
	{
		alert("Failed to open Map page. Some pop-up blocking programs doesn't recognize correlations within the same web page. To continue with purchasing, please check pop-up window blocker or browser settings");
		return ;
	}
}

function intval(v)
{
	var i = parseInt(v);
	if (isNaN(i)) return 0;
	return i;
}

function clickACCESSORIES()
{
	if (YAHOO.util.Dom.getStyle("divACCESSORIES", "display") == "block")
	{
		YAHOO.util.Dom.setStyle("divACCESSORIES", "display", "none");
		return ;
	}

	YAHOO.util.Dom.setStyle("divACCESSORIES", "display", "block");
	centerElement("divACCESSORIES");

	return ;
}

function clickDNC()
{
	if (YAHOO.util.Dom.getStyle("divDNC", "display") == "block")
	{
		YAHOO.util.Dom.setStyle("divDNC", "display", "none");
		return ;
	}

	YAHOO.util.Dom.setStyle("divDNC", "display", "block");
	centerElement("divDNC");

	return ;
}

function clickAMAZON()
{
	var hwnd = window.open("http://www.amazon.com/gp/search?ie=UTF8&keywords=microphone&tag=shoutclast-20&index=pc-hardware&linkCode=ur2&camp=1789&creative=9325");
	if (!hwnd)
	{
		alert("Failed to open the page.\nSome pop-up blocking programs doesn't recognize correlations within the same web page.\n To continue, please check pop-up window blocker or browser settings.");
		return ;
	}
}

function clickBBB()
{
	var hwnd = window.open("http://www.bbbonline.org/cks.asp?id=1070209175249");
	if (!hwnd)
	{
		alert("Failed to open the page.\nSome pop-up blocking programs doesn't recognize correlations within the same web page.\n To continue, please check pop-up window blocker or browser settings.");
		return ;
	}
}

function clickBBBP()
{
	var hwnd = window.open("http://www.bbbonline.org/cks.asp?id=20704099173585374");
	if (!hwnd)
	{
		alert("Failed to open the page.\nSome pop-up blocking programs doesn't recognize correlations within the same web page.\n To continue, please check pop-up window blocker or browser settings.");
		return ;
	}
}

function clickBlog()
{
	var hwnd = window.open("http://shoutclast.blogspot.com");
	if (!hwnd)
	{
		alert("Failed to open the page.\nSome pop-up blocking programs doesn't recognize correlations within the same web page.\n To continue, please check pop-up window blocker or browser settings.");
		return ;
	}
}

function clickBookmark()
{
	var hwnd = window.open("/bookmark.php");
	if (!hwnd)
	{
		alert("Failed to open the page.\nSome pop-up blocking programs doesn't recognize correlations within the same web page.\n To continue, please check pop-up window blocker or browser settings.");
		return ;
	}
}

function pvperr2str(code)
{
	if (code == 0) return "no error";
	else if (code == 1) return "system failure";
	else if (code == 2) return "security check failed";
	else if (code == 3) return "file not found";
	else if (code == 4) return "file already exist";
	else if (code == 5) return "system failure";
	else if (code == 6) return "network connection failed";
	else if (code == 7) return "too big file";
	else if (code == 8) return "server failure";
	else if (code == 9) return "no data";
	else if (code == 10) return "Phonevite Recorder is updated. please update Phonevite Recorder, then try again.";
	else if (code == 11) return "User cancelled.";
	else return "unknown error";
}

function pvphone(e164)
{
	return "("+e164.substr(1,3)+") "+e164.substr(4,3)+"-"+e164.substr(7,4);
}

function makeE164(phone)
{
	if (phone.length > 31) return false;
	var num = phone.replace(/[^0-9]/gi, "");
	if (num.length == 11)
	{
		if (num.charAt(0) != "1") return "";
	}
	else if (num.length == 10)
	{
		num = "1" + num;
	}
	else return "";
	return num;
}

function GKWAIT(containerId)
{
	this.containerId = containerId;

	YAHOO.util.Dom.setStyle(containerId, "display", "none");
	YAHOO.util.Dom.setStyle(containerId, "position", "absolute");
	YAHOO.util.Dom.setStyle(containerId, "width", "250px");
	YAHOO.util.Dom.setStyle(containerId, "height", "50px");
	YAHOO.util.Dom.setStyle(containerId, "background-color", "#bb3030");
	YAHOO.util.Dom.setStyle(containerId, "font-size", "20px");
	YAHOO.util.Dom.setStyle(containerId, "color", "#ffffff");
	YAHOO.util.Dom.setStyle(containerId, "clear", "both");
	YAHOO.util.Dom.setStyle(containerId, "opacity", "0.8");

	YAHOO.util.Dom.get(containerId).innerHTML = "<div style=\"float:left;width:30px;margin:10px 10px 0px 30px\"><img src=\"/images/wait.gif\"></div><div style=\"float:left;width:150px;margin-top:20px\">&nbsp;&nbsp;&nbsp;Please Wait...</div>";
}

GKWAIT.prototype.show = function()
{
	YAHOO.util.Dom.setStyle(this.containerId, "display", "block");
	centerElement(this.containerId);
}

GKWAIT.prototype.hide = function()
{
	YAHOO.util.Dom.setStyle(this.containerId, "display", "none");
}

/* XML */
function kkxml_value(xml, tag)
{
	var node = xml.getElementsByTagName(tag)[0].firstChild;
	if (node) return node.nodeValue;
	return null;
}


function xml2error_code(xml)
{
	return intval(kkxml_value(xml, "code"));
}

function xml2error_reason(xml)
{
	return kkxml_value(xml, "reason");
}

function xml2id(xml)
{
	return intval(kkxml_value(xml, "id"));
}

if (typeof Phonevite == "undefined") {
	var Phonevite = {};
}

function fix_ffcaret(dlg)
{
	/* ff caretfix https://bugzilla.mozilla.org/show_bug.cgi?id=167801#c84 */
	if (YAHOO.env.ua.gecko)
	{
		YAHOO.util.Dom.setStyle(dlg.form, "overflow", "auto");
		dlg.showEvent.subscribe(function()
		{
			var el = dlg.form;
			el.style.display = "none";
			setTimeout(function(){el.style.display = "block";}, 0);
		}, this, true);
	}
}

function pvRemoveChildren(element) {
	element = YAHOO.util.Dom.get(element);
	if (element) {
		while (element.firstChild) {
		  element.removeChild(element.firstChild);
		}
	}
}

function pvj2h(str) {
	return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

function pvpng(img_element, img_path) {
	var el = YAHOO.util.Dom.get(img_element);
	if (YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
		el.src = "/images/blank.gif";
		if (img_path) {
			el.style.filter = "progid:DXimageTransform.Microsoft.AlphaimageLoader(src='" + img_path + "',sizingMethod='crop')";
		}
		else { el.style.filter = null; }
	}
	else {
		if (img_path) { el.src = img_path; }
		else { el.src = "/images/blank.gif"; }
	}
}
