function GetByID(id)
{
   itm = null;
   if (document.getElementById)
   {
      itm = document.getElementById(id);
   }
   else if (document.all)
   {
      itm = document.all[id];
   }
   else if (document.layers)
   {
      itm = document.layers[id];
   }
   
   return itm;
}

function AddFavorite()
{
	if (document.all)
		window.external.addFavorite('http://www.dearcoupon.com','Dear Coupon');
	else if (window.sidebar)
		window.sidebar.addPanel('Coupon US', 'http://www.dearcoupon.com', "");
}

function QueryTip(url)
{
	$.get(
		url,
		function(data)
		{
			try
			{
				ShowTempTip(data);
			}
			catch (e)
			{
			}
		}
	);
	return false;
}

function IsMobile(ipt)
{
	var filter=/(^13{1}[0-9]{9}$)|(^15{1}[0-9]{9}$)/;
	if (!filter.test(ipt))        return false; 
	return true;   
}

function CheckNumber(value)
{
	var patten = /[^0-9]/;
		
	if(value <= 0)
		return false;	
	var patrn = /[0-9]+$/;
	if( patrn.test(value) ) return true;
}

function IsMoney(value)
{
	var patten = /^\d+(\.\d+)?$/;
	if( !patten.test(value) ) return false; 
	try
	{
	   if(parseFloat(value) != value) return false;
	} 
	catch(ex) 
	{
		return false; 
	}
	return true;
}

function isEmail(s)
{
	var patrn = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if( patrn.test(s) )
		return true;
	else
		return false;
}

function QueryCheck(ObjName, ObjValue, Context)
{
	if(Context != '' && ObjValue == Context)
	{
		var TipStr = '<font color="#66CC00">this '+ObjName+' is ok</font>';
		$('#Tip'+ObjName).html(TipStr);
	}
	else
	{
		$.get(AjaxURL+"&Opt=Check&"+ObjName+"="+encodeURI(ObjValue),function(data){$('#Tip'+ObjName).html(data);});
	}
}

function TimeOutHide(TipName)
{
	TipTime = typeof(arguments[1]) == "undefined" ? '3000' : arguments[1];

	$(TipName).hide();
	$(TipName).show();
	setTimeout('$("'+TipName+'").hide();', TipTime);
}
/*
ShowTempTip(Tip, TipTime, TipName)
*/
function ShowTempTip(Tip)
{
	var TipName; var TipTime;

	TipTime = typeof(arguments[1]) == "undefined" ? '3000' : arguments[1];
	TipName = typeof(arguments[2]) == "undefined" ? '#result-hints' : '#'+arguments[2];

	document.getElementById(TipName.replace('#', '')).style.left = ((document.body.clientWidth-300)/2) + "px";
	document.getElementById(TipName.replace('#', '')).style.top  = ((document.documentElement.clientHeight-100)/2) + "px";
	document.getElementById(TipName.replace('#', '')).style.position = 'absolute';
	$(TipName).html(Tip);
	TimeOutHide(TipName, TipTime);
}

function OpenDialog(html)
{
	var bwidth  = document.body.clientWidth;
	var bheight = document.body.clientHeight;
	
	mwidth  = typeof(arguments[1]) == "undefined" ? '300' : arguments[1];
	mheight = typeof(arguments[2]) == "undefined" ? '80' : arguments[2];

	var s = '<div id="TipBG" style="width:'+bwidth+'px;height:'+bheight+'px;left:0px;top:0px;position:absolute;z-index:999;background-color:#666666;filter:Alpha(Opacity=50, FinishOpacity=50, Style=1, StartX=0, StartY=0, FinishX=100, FinishY=100);opacity:0.5;" ></div><div id="Dialog"></div>';
	$("body").prepend(s);

	$('#Dialog').css("width"   ,mwidth);
	$('#Dialog').css("height"  ,mheight);
	$('#Dialog').css("left"    ,((bwidth-mwidth)/2) + "px");
	$('#Dialog').css("top"     ,((document.documentElement.clientHeight-mheight)/2) + "px");
	$('#Dialog').css("position","absolute");
	$('#Dialog').html(html);
	$('#Dialog').show();
}

function CloseDialog()
{
	$("div").remove('#TipBG');
	$('#Dialog').hide();
}


function GetTime()
{
	return new Date().getTime().toString().substr(8);
}


function SelectedVal(SelName, Val)
{
	if(Val=='') return false;
	$("select[@name="+SelName+"] option").each(function(i){
		if(this.value==Val) this.selected = true;
	});
}

function CheckedVal(BoxName, Val)
{
	if(Val=='') return false;
	$("input[@name="+BoxName+"]").each(function(i){
		if(this.value==Val) this.checked = true;
	});
}

/** **/
function SetHasValue(Str, ByName)
{
	if(Str.length>0)
	{
		var toArray = Str.split(',');
		var e = document.getElementsByName(ByName);
		for(q=0; q<toArray.length; q++)
		{
			for (i=0;i<e.length;i++) 
			{
				if ( toArray[q]==e[i].value)
					e[i].checked = true;
			}
		}
	}
}

function GetHasValue(ByName)
{
	var n; var Str = ''; var Arr = Array();
	var e = document.getElementsByName(ByName);
	for (n=0;n<e.length;n++) 
	{
		if(e[n].checked == true)
		{
			if(!isNaN(e[n].value))
			{
				Str = Str + e[n].value + ',';
			}
			else if(typeof(arguments[1]) != "undefined")
			{
				Arr = e[n].value.split('|');
				Str = Str + Arr[arguments[1]] + ',';
			}
		}
	}
	Str = Str.substring(0, Str.length-1);
	return Str;
}

function GetSumValue(ByName, Checked)
{
	var n; var Str = 0; var Arr = Array();
	var e = document.getElementsByName(ByName);
	for (n=0;n<e.length;n++) 
	{
		if(!isNaN(e[n].value))
		{
			Str = Str + parseFloat(e[n].value);
		}
		else if(typeof(arguments[2]) != "undefined")
		{
			Arr = e[n].value.split('|');
			if(Checked==true && e[n].checked == true)
				Str = Str + parseFloat(Arr[arguments[2]]);
			else if(Checked==false)
				Str = Str + parseFloat(Arr[arguments[2]]);
		}
	}
	Str = Str.toFixed(2);
	return Str;
}

function GetValues(ByName)
{
	var n; var Str = ''; var Arr = Array();
	var e = document.getElementsByName(ByName);
	for (n=0;n<e.length;n++) 
	{
		if(!isNaN(e[n].value) && e[n].value>0)
		{
			Str = Str + e[n].value + ',';
		}
		else if(typeof(arguments[1]) != "undefined")
		{
			Arr = e[n].value.split('|');
			Str = Str + Arr[arguments[1]] + ',';
		}
	}
	Str = Str.substring(0, Str.length-1);
	return Str;
}

function CountHasValue(ByName)
{
	var n; var Num = 0;
	var e = document.getElementsByName(ByName);
	for (n=0;n<e.length;n++) 
	{
		if(e[n].checked == true && !isNaN(e[n].value))
		{
			Num++;
		}
	}
	return Num;
}
/** **/


/** **/
function CountCheckBox(form)
{
	var c = 0;
	for (var i=0;i<form.elements.length;i++) 
	{
		var e = form.elements[i];
		if (e.checked == true && e.type=='checkbox' && e.rel!='NotInAll')
			c = c + 1;
	}
	return c;
}

function CheckAll(form) 
{
	for (var i=0;i<form.elements.length;i++)
	{
		var e = form.elements[i];
		if (e.name != 'chkall' && e.type=='checkbox' && e.rel!='NotInAll')
			e.checked = form.chkall.checked;
	}
}

function ConfirmOpt(form)
{
	if(CountCheckBox(form)<1)
	{
		alert('Please select');
		return false;
	}
	else return confirm('Are you sure?');
}

function Opt(form)
{
	if(CountCheckBox(form)<1)
	{
		alert('Please select');
		return false;
	}
	else return true;
}

function ActChange(form)
{
	if(Opt(form))
		form.submit();
	else
		return false;
}

function replaceAll(string, OldStr, NewStr)
{
	return string.replace(new RegExp(OldStr,"gm"),NewStr);
}

function addFavorite()
{
	var title = document.title;
	var url   = document.location.href;
	if (window.sidebar) 
		{ // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} 
	else if(window.external) 
		{ // IE Favorite
			window.external.AddFavorite(url, title); 
		}
	else if(window.opera && window.print) 
		{ // Opera Hotlist
			return true;
		}
}