function Is()
{
agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion);
this.minor = parseFloat(navigator.appVersion);
this.ns = ((agent.indexOf('mozilla') != -1) &&
(agent.indexOf('spoofer') == -1) &&
(agent.indexOf('compatible') == -1) &&
(agent.indexOf('opera') == -1) &&
(agent.indexOf('webtv') == -1) &&
(agent.indexOf('hotjava') == -1));
this.ns6 = (this.ns && (this.major >= 5));
this.ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
this.ie6 = (this.ie && (this.major == 4) && (agent.indexOf("msie 6.") != -1));
this.ie7 = (this.ie && (this.major == 4) && (agent.indexOf("msie 7.0b") != -1));
this.w3dom = (this.ns6 || this.ie6 ||this.ie7);
this.safari = (agent.indexOf('safari') != -1);
this.mac = (agent.indexOf('mac') != -1);
this.firefox = (agent.indexOf('firefox') != -1);
}

var is = new Is();
// global url get parameters
GL_param  = new Array();
GL_search = document.location.search.substring(1).split('&');
for (i in GL_search) {
	GL_t = GL_search[i].split('=');
	GL_param[GL_t[0]] = GL_t[1];
}

function getParam(name)
{
	if (GL_param[name])
		return GL_param[name];
	else
		return '';
}

function getId(id)
{
if (id=='' || id==null) return null;
return document.getElementById(id);
}

function getDomain()
{
	var d = document.location.host.split('.');
	var dom = d[d.length - 2]+'.'+d[d.length - 1];
	return dom;
}

function baseUrl()
{
	return (location.protocol+'//'+location.host);
}

function createCookie(c_name,value,expiredays)
{
	if (expiredays){
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
	}
	document.cookie=c_name+"="+escape(value)+((expiredays==null)?"":"; expires="+exdate.toGMTString())+"; path=/; domain="+getDomain()+";";
}

function readCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return null;
}

function delete_cookie(c_name)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()-365);
	document.cookie=c_name+"=; expires="+exdate.toGMTString()+"; path=/; domain="+getDomain()+";";
}


function callMatrix(p)
{
	stopLoad();
	top.location = baseUrl()+'/main.php';
}

function callMain(p)
{
	stopLoad();
	top.location = baseUrl()+'/main.php?'+p;
}

function callView(p)
{
	stopLoad();
	var url = baseUrl()+'/view.php?ret=1&'+p;
	if (p == 'A=Upload')
		top.location = url;
	else
		document.location = url;
}

function callPage(p)
{
	stopLoad();
	document.location = baseUrl()+'/page.php?'+p;
}

function goRef(r,t)
{
	if (t=='') document.location.href=r;
	else window.open(r,t);
}

function goHome(p)
{
	top.location = location.protocol+'//'+location.host+'/index.php';
}

function upgrade(p)
{
	top.location = location.protocol+'//'+location.host+'/products.php';
}

function urlEncode(url){
	return escape(url);
}

function resetMatrix()
{
	parent.matrixprm = null;
	parent.fullList = null;
	parent.loadList = false;
	parent.resultList = null;
	parent.selectList=null;
	parent.selectItem=null;
	callView('A=Matrix');
}

// logout
function delCookies()
{
   delete_cookie('U');
   delete_cookie('H');
   delete_cookie('M');
   delete_cookie('N');
   delete_cookie('G');
   delete_cookie('S');
   delete_cookie('PL');
}

function logout()
{
   delCookies();
   parent.location = 'signout.php';
}

// misc tools
function dateFormat()
{
	var American='%m/%d/%Y';
	var European='%d/%m/%Y';

	var d=new Date('10/28/1976');
	var s=d.toLocaleDateString();
	if(s.charAt(0)=='2')
		return European
	else
		return American;
}

function ucword(str)
{
   // split string
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);

   // convert case
   firstChar = firstChar.toUpperCase();
   remainChar = remainChar.toLowerCase();

   return firstChar + remainChar

}

function formatDate(dat)
{
	d = dat.getDate();
	m = dat.getMonth()+1;
	y = dat.getFullYear();

	d = (d > 9) ? d : ('0'+d);
	m = (m > 9) ? m : ('0'+m);
	y = (y < 100) ? y : (y.toString().substring(2));

	if(dat.toLocaleDateString().charAt(0)=='2')
		return (d+"/"+m+"/"+y); // European
	else
		return (m+"/"+d+"/"+y); // American
}

function objProp(o)
{
	s = '';
	n = 0;
	for (i in o){
		n++;
		s+= '-' + i + ':' + o[i] + '\n';
		if ((n % 20) == 0) {
			alert(s);
			s = '';
		}
	}
	alert(s);
}

function inArray(val,arr)
{
	for (var i in arr){
		if (arr[i] == val) return true;
	}
	return false;
}

var preload_image = new Image();
function preloadImage(u)
{
	preload_image.src=u;
}

function s3Url(f,sz,buck,akid){
	if (f.s == "") return (imgProc(sz));
	u = 'http://s3.amazonaws.com/'+buck+'/';
	u+= f.h+'_tn_crop'+sz;
	u+= '?AWSAccessKeyId='+akid;
	u+= '&Expires='+f.s;
	return u;
}

function addOption(sid,optxt,opval)
{
	opval = (opval == null) ? optxt : opval;
	var sel = getId(sid);
	var elm = new Option(optxt,opval,false,false);
	sel.options[sel.length] = elm;
}

function getOptionsText(sel,arrayLst){
	var ret = '';
	for (var i = 0; i < sel.options.length; i++) {
		if (inArray(sel.options[i].value,arrayLst))
			ret += ','+sel.options[i].innerHTML;
	}
	return (ret.substr(1));
}

function checkEmail(email) //http://www.javascriptkit.com
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	return (filter.test(email));
}
//--
var ACC=new Array();
ACC['à']='a'; ACC['á']='a'; ACC['â']='a'; ACC['ã']='a'; ACC['ä']='a'; ACC['å']='a';
ACC['è']='e'; ACC['é']='e'; ACC['ê']='e'; ACC['ë']='e';
ACC['ì']='i'; ACC['í']='i'; ACC['î']='i'; ACC['ï']='i';
ACC['ò']='o'; ACC['ó']='o'; ACC['ô']='o'; ACC['õ']='o'; ACC['ö']='o';
ACC['ù']='u'; ACC['ú']='u'; ACC['û']='u'; ACC['ü']='u';
ACC['ç']='c';
function stripAccents(str)
{
	str = str.toLowerCase();
	var out = '';
	for (var i=0; i<str.length; i++)
		out += (ACC[str.charAt(i)] == null) ? str.charAt(i) : ACC[str.charAt(i)];
	return out;
}

function trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}

function ltrim(s) {
    return s.replace(/^\s+/, '');
}

function rtrim(s) {
    return s.replace(/\s+$/, '');
}

function stopLoad()
{
	if (window.stop)
		window.stop();
	else
		document.execCommand("Stop");
}

function imgPath(img){
	var p = '/themes/'+theme_name+'/imgs/'+img;
	preloadImage(p);
	return (p);
}

function imgColor(img){
	var p = '/themes/'+theme_color+'/imgs/'+img;
	preloadImage(p);
	return (p);
}

function imgProc(sz){
	var p = '/themes/orangepurple/imgs/proc/proc'+sz+'.jpg';
	return (p);
}

function localDate(d)
{
	var dt = d.toGMTString().split(' ');
	return (dt[1] + ' ' + dt[2] + ' ' + dt[3]);
}

function dateForm(d){
	try{
	var dt = d.split('-');
		dx = new Date();
		dx.setDate(dt[2]);
		dx.setMonth(dt[1] - 1);
		dx.setFullYear(dt[0]);
		return localDate(dx);
	}
	catch(e){
		return '';
	}
}

function dispId(lst)
{
	L = lst.split(',');
	for (i in L)
		getId(L[i]).style.display = 'inline';
}

function hideId(lst)
{
	L = lst.split(',');
	for (i in L) getId(L[i]).style.display = 'none';
}

function toggleDisplayId(id)
{
	getId(id).style.display = (getId(id).style.display == 'none') ? 'block' : 'none';
}

function resizeListHeight(list_id)
{
	var lst = getId(list_id);
	lst.style.height = '50px';
	lst.style.height = lst.offsetParent.offsetHeight+'px';
}

function isEnable(b_id){
	return (getId(b_id).className.indexOf('_d') < 0);
}

function initImgKey(path){
	p = (path) ? path : '';
	var d = SJAX(p+'gethtest.php','GET',false);
	createCookie('HTC',d.substr(0,3));
	createCookie('HT',d.substr(3));
	getId('IMGKEY').src = p+'widgets/humantest.php?nocache='+(new Date());
	delWhenComplete();
}

function delWhenComplete()
{
	if (getId('IMGKEY').complete == true)
		delete_cookie('HTC');
	else
		window.setTimeout("delWhenComplete()", 500);
}

function getSelectedCookie()
{
	var sel=Array();
	var cookie=readCookie("SEL")
	if(!cookie) return sel;
	var lst=cookie.split(',');
	for(i in lst){
		sel[parseInt(lst[i])]=true;
	}
	return sel;
}

var selectItems;
function getSelected()
{
	var i;
	var cnt=0;
	var lst=Array();
	for(i in selectItems){
		if((i!=0) && (selectItems[i]==true)) lst[cnt++]=i;
	}
	return lst;
}

function getSelectedItems()
{
	var sel=Array();
	try {
		var psl=parent.selectList;
	}
	catch(e){
		var psl = null;
	}
	if(!psl) return sel;
	var lst=psl.split(',');
	for(i in lst){
		sel[parseInt(lst[i])]=true;
	}
	return sel;
}

function initList()
{
	parent.selectList = null;
	parent.loadList = true;
}

function textMaxLength(obj,len)
{
	if (obj.value.length>len) obj.value=obj.value.substring(0,len);
}

function randBetween(num1,num2)
{
	var gen = Math.random()*(num2-num1);
	return (Math.round(num1+gen))
}

var arbzk = (randBetween(1000,2000))+'@';
function contactUs()
{
	var c1 = '2padteam.com';
	var c2 = 'mailto:contact';
	return (c2+arbzk+c1);
}

function testKey(e)
{
	if(window.event) // IE
		return e.keyCode;
	else if(e.which) // Firefox
		return e.which;
}

function pageWidth(){return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight(){return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
function scrWidth(){return document.documentElement.scrollWidth != null? document.documentElement.scrollWidth: document.body.scrollWidth != null? document.body.scrollWidth:null;}
function scrHeight(){return document.documentElement.scrollHeight != null? document.documentElement.scrollHeight: document.body.scrollHeight != null? document.body.scrollHeight:null;}

function btnOver(o){if (o.className.indexOf('_d') == -1) o.className+=" col2";}
function btnOut(o){o.className=o.className.replace(" col2","");}


//--Languages--
function chooseLang(){getId("LangBox").style.display = 'block';}
function hideLang(){getId("LangBox").style.display = 'none';}

function setLang(ln)
{
  	delete_cookie('L');
	createCookie('L',ln);

	var toploc=false;
	var d=SJAX('/setuserlang.php?lang='+ln);
  	var dl = document.location;

  	uhost = dl.hostname.replace('www.','');
  	uhost = uhost.replace('en.','');
  	uhost = ((ln=='en')?'en.':'www.')+uhost;

  	upath = dl.pathname;
  	if (upath.indexOf('view.php')){
  		toploc=true;
  		upath = upath.replace('view.php','main.php');
  	}

  	usearch = dl.search.replace('lang=<?=$lang?>','');

  	url = dl.protocol+'//'+uhost+upath+usearch;

  	if (toploc===true)
    	top.location = url;
    else
	    document.location = url;
}

//--New combo box--
function comboxInit(id)
{
	element = document.getElementById(id+'_list');
	element.onkeydown = comboxKeyDown;
	if (element.captureEvents) element.captureEvents(Event.KEYDOWN);
}
function comboxOpen(obj)
{
	var prefix = obj.id.split('_');
	var obj = document.getElementById(prefix[0]+'_list');
	if (obj.style.display == "block"){
		obj.style.display = "none";
	}
	else {
		obj.style.display = "block";
		obj.focus();
	}
}
function comboxSetValue(obj)
{
	var prefix = obj.id.split('_');
	document.getElementById(prefix[0]+'_label').innerHTML = obj.options[obj.selectedIndex].innerHTML;
}
function comboxKeyDown(e)
{
	if (!e) var e = window.event
	if (e.keyCode) var code = e.keyCode;
	else if (e.which) var code = e.which;
	if (code==13) comboxOpen(this);
}

function fieldMandatory(e,fname){
	if (e.value == ""){
		e.focus();
		alert(TJAX('The field [$p1] is required',fname));
		return false;
	}
	return true;
}
