//var loadingId=0;
//var loadingSignal=new Array("|","\\","--","/");

/*
get how much pixels of object what's string offsettype("left" or "top")
*/
function getposOffset(whatObj, offsettype){
	var totaloffset=(offsettype=="left")? whatObj.offsetLeft : whatObj.offsetTop;
	var parentEl=whatObj.offsetParent;
	while (parentEl!=null) {
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}
/*
in string itemName+itemSerial set to visible/hidden
*/
function clickItem(itemName,itemSerial){
	if (document.getElementById(itemName+""+itemSerial).style.display!="block"){
		document.getElementById(itemName+""+itemSerial).style.display = "block";
		document.getElementById(itemName+""+itemSerial).style.visibility = "visible";
	}
	else{
		document.getElementById(itemName+""+itemSerial).style.display = "none";
		document.getElementById(itemName+""+itemSerial).style.visibility = "hidden";
	}
}
/*
Open a new browser window
*/
function wopen(){
	var w=wopen.arguments;
	targetDocument="_blank";
	currentWidth=screen.availWidth;
	currentHeight=screen.availHeight;
	var openUrl='';
	switch(w.length){
		case 0:
			return;
			break;
		case 4:
			currentHeight=w[3];
		case 3:
			currentWidth=w[2];
		case 2:
			targetDocument=w[1];
		case 1:
			openUrl=w[0];
	}
	newDocument=open(openUrl,targetDocument,"height="+currentHeight+",width="+currentWidth+",status=0,top=0,left=0,location=0,menubar=0,personalbar=0,resizable=1,dependent=1,directories=0,alwaysLowered=1");
}
function changeText(objName,str){
	if(document.getElementById){
		document.getElementById(objName).innerHTML=str;
	}
	else if(document.all){
		document.all[objName].innerHTML=str;
	}
	else if(document.layers){
		with(document.layers[objName].document){
			open();
			write(str);
			close();
		}
	}
}
function changeClass(d,c){d.className=c;}
function allReplace(findText,replaceText,str){if(findText.length<1)return str;while(str.indexOf(findText)>=0){str=str.replace(findText,replaceText);}return str;}
function str2Element(s){
	str=str.replace(/\"/,"\\\"");
//	str=allReplace("\"","\\\"",str);
	return str;
}
function changeLang(lang){
	if(!lang.match(/^[\d\w\-_]{2,6}$/))return;
	docURL=document.location.href;
	n=docURL.lastIndexOf("/");
	docURL=docURL.substr(n+1);
	docURL=docURL.replace(/#*$/,'');
	docURL=docURL.replace(/\?lang=[\d\w\-_]+/,'');
	docURL=docURL.replace(/&lang=[\d\w\-_]+/,'');
	docURL=docURL.replace(/lang=[\d\w\-_]+/,'');
	if(docURL.length<1){
		location.href=docURL+'?lang='+lang;
	}
	else if(docURL.indexOf('?')>=0){
		location.href=docURL+'&lang='+lang;
	}
	else{
		location.href=docURL+'?lang='+lang;
	}
}
function setCookie(c_name,value,expiredays){var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());}
function getCookie(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 '';}
function gotoUrl(url){self.location=url;}
function $(id){return findObj(id);}
function isId(s){
	if(typeof s == 'Array'){
		for(i=0;i<s.length;i++)
			if(!s[i].match(/^[a-zA-Z][a-zA-Z0-9_]*$/))return false;
	}
	else{
		if(!s.match(/^[a-zA-Z][a-zA-Z0-9_]*$/))return false;
	}
	return true;
}
function findObj(theObj, theDoc){
	var p, i, foundObj;
	if(!theDoc) theDoc = document;
	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length){
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	for (i=0; !foundObj && i < theDoc.forms.length; i++){
		foundObj = theDoc.forms[i][theObj];
	}
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++){
		foundObj = findObj(theObj,theDoc.layers[i].document);
	}
	if(!foundObj && document.getElementById)
		foundObj = document.getElementById(theObj);
	else if(!foundObj)
		foundObj=null;

	return foundObj;
}
function substr_count(str,needle){
	var x=0;
	var n=0;
	while(x>=0){
		x=str.indexOf(needle,x);
		if(x>=0){n++;x++;}
	}
	return n;
}
function switchPanel(domId){
	o=findObj(domId);
	if(o.style.visibility=='hidden'){
		o.style.visibility='visible';
		o.style.display='';
	}
	else{
		o.style.visibility='hidden';
		o.style.display='none';
	}
}
/*

calc function

*/

function calc_pressKey(){
	o=findObj('pageBusy');
	changeClass(o,'floatFrame');
	o.style.top='30px';
	o.style.left='30px';
	o.style.width='660px';
	o.style.height='700px';
	o.style.backgroundColor='#F00';
	o.style.border='1px solid #000';

}

function floatFrame_calc(){
	calc_pressKey();


}


/*
from str to html format
*/
function htmlspecialchars(str){
	str=strReplace(str,'&','&amp;');
	str=strReplace(str,'"','&quot;');
	str=strReplace(str,'<','&lt;');
	str=strReplace(str,'>','&gt;');
	str=strReplace(str,'%20',' ');
	return str;
}
function includeScript(sFilename){
	var objHead = document.getElementsByTagName('head');
	var objScript = document.createElement('script');
	objScript.type = 'text/javascript';
	objScript.src = '../../../../../CORE/WWW/js/'+sFilename;
	objHead[0].appendChild(objScript);
}
function includeSkin(sFilename){
	var objHead = document.getElementsByTagName('head');
	var objScript = document.createElement('link');
	objScript.type = 'text/css';
	objScript.src = sFilename;
	objHead[0].appendChild(objScript);
}
createEl = function(t, a, y, x) {
	var e = document.createElement(t);
	if (a) {
		for (var k in a) {
			if (k=='class')e.className=a[k];
			else if (k=='id')e.id=a[k];
			else e.setAttribute(k,a[k]);
		}
	}
	if (y){for(var k in y)e.style[k]=y[k];}
	if (x){e.appendChild(document.createTextNode(x));}
	return e;
}
/*
get language charactor of page from <html lang='....'>
*/
function getPageLang(){
	var objHtml = document.getElementsByTagName('html');
	if(objHtml[0].lang.length>1)return objHtml[0].lang;
	return "default";
}
function close_hiddenLayer(){
	changeClass(findObj('hiddenLayer'),"hiddenLayer2Hidden");
}
function myFavorite(site,url){
	if(document.all){
		window.external.AddFavorite(url,site);
	}
	else{
		window.sidebar.addPanel(site, url, "");
	}
}
function addslashesSignal(str){
	newstr="";
	for(i=0;i<str.length;i++){
		c=str.codeAt(i);
		if(c=="'")newstr+="\\";
		newstr+=c;
	}
	return newstr;
}
/*
0:call from DomId
1:sql schema Id
2:return DomId,return DomId,.....etc 
*/
function popupSelect(){
	var pps=popupSelect.arguments;
	var subGet='';
	if(pps.length<3)return false;
	subGet=pps[0];if(!subGet.match(/^[a-zA-Z_0-9]+$/g))return false;
	subGet=pps[1];if(!subGet.match(/^[a-zA-Z_0-9]+$/g))return false;
	subGet=pps[2];if(!subGet.match(/^(,?[a-zA-Z_0-9]+)+$/g))return false;
	if(pps.length>5){
		for(i=5;i<pps.length;i++){
			subGet+='&x'+(i-5)+'="'+pps[i-4]+'"';
		}
	}
	subGet='DomId='+pps[0]+'&sqlId='+pps[1]+'&returnDomId='+pps[2]+''+subGet;
	x=findObj(pps[0]);
	switchPanel('hiddenLayer');
	o=popup_funcexec('This is a test game.','','<iframe src="cows_list.php?'+subGet+'" style="width:100%;height:100%;margin:0;"></iframe>',getposOffset(x,'left')-150,getposOffset(x,'top')+10);
	o.style.width=pps[3];
	o.style.height=pps[4];
}
function exec_popup_iframe(subject,contentAjaxLink,moveX,moveY){
	var popupContent='';
	popupLayerObj=findObj('hiddenLayer');
	//changeClass(popupLayerObj,'hiddenLayer2Display');
	popupLayerObj.style.left=moveX+'px';
	popupLayerObj.style.top=moveY+'px';
	popupLayerObj.style.width='400px';
	popupLayerObj.style.height='300px';
	popupLayerObj.style.backgroundColor='#F00';

	popupContent+='<div id="exec_block">';
	popupContent+='	<div class="block_top_left"><div class="block_top_center"><div class="block_top_right"></div></div></div>';
	popupContent+='	<div class="block_body_left"><div class="block_body_center"><div class="block_body_right">';
	popupContent+='		<table border="0" cellpadding="0" cellspacing="0" class="exec_table">';
	popupContent+='		<tr>';
	popupContent+='			<th class="exec_subject">';
	popupContent+=''+subject;
	popupContent+='			</th>';
	popupContent+='			<th class="exec_button"><div class="exec_close" onClick="exec_block_close();"></div></th>';
	popupContent+='		</tr>';
	popupContent+='		<tr>';
	popupContent+='			<td colspan="2">';
	popupContent+='';
	popupContent+='			</td>';
	popupContent+='		</tr>';
	popupContent+='		</table>';
	popupContent+='	</div></div></div>';
	popupContent+='	<div class="block_bottom_left"><div class="block_bottom_center"><div class="block_bottom_right"></div></div></div>';
	popupContent+='</div>';
	changeText('hiddenLayer',popupContent);
}
/*
popup_funcexec(string subject,string link of subject,string contents,int moveX,int moveY)
*/
function popup_funcexec(subject,subjectLink,contents,moveX,moveY){
	hiddenLayerContent='';
	o=findObj('hiddenLayer');
	changeClass(o,'hiddenLayer2Display');
	o.style.left=moveX+'px';
	o.style.top=moveY+'px';
	hiddenLayerContent ='<div id="hiddenLayerTable">';
	hiddenLayerContent+='	<div class="HeaderLeft"><div class="HeaderCenter"><div class="HeaderRight"></div></div></div>';
	hiddenLayerContent+='		<div class="BodyLeft"><div class="BodyCenter"><div class="BodyRight">';
	hiddenLayerContent+='			<table style="width:96%;height:96%;margin:2%;"><tr>';
	hiddenLayerContent+='				<td class="popupSubject">';
	if(subjectLink.length>0)hiddenLayerContent+='<a href="'+subjectLink+'">';
	hiddenLayerContent+='<div>'+subject+'</div>';
	if(subjectLink.length>0)hiddenLayerContent+='</a>';
	hiddenLayerContent+='				</td>';
	hiddenLayerContent+='<td class="popupFunc" width="20px;"><input type="button" onClick="switchPanel(\'hiddenLayer\')" class="a_button exec_close" /></td></tr>';
	hiddenLayerContent+='<tr><td id="Content" colspan="2"><div>'+contents+'</div></td></tr></table>';
	hiddenLayerContent+='		</div></div></div>';
	hiddenLayerContent+='	<div class="FooterLeft"><div class="FooterCenter"><div class="FooterRight"></div></div></div>';
	hiddenLayerContent+='</div>';
	changeText('hiddenLayer',hiddenLayerContent);
	return o;
}
function selectDisable(o){
	o.disabled=true;
}
function selectEnable(o){
	o.disabled=false;
}
function delSelectOption(selectObj){
	if(!selectObj)return;
	for(i=selectObj.length-1;i>=0;i--){
		selectObj.options[i]=null;
	}
}

function deleteFormTables(r,p){
	var x=0;
	r--;
	for(i=0;i<tableData[p][3].length;i++){
		if(tableData[p][3][i]==1)x++;
		if(i==r)break;
	}
	var tT=document.getElementById(tableData[p][0]+""+"Table");
	tT.deleteRow(i+1);
	tableData[p][3][r]=0;
}

function addFormTables(p){
	var tT=document.getElementById(tableData[p][0]+""+"Table");
	var newIndex=tableData[p][2];
	var tNewRow=tT.insertRow(tT.rows.length-1);
	for(i=0;i<tableData[p][1].length;i++){
		c=tNewRow.insertCell(i);
		str=tableData[p][1][i].replace(/\[SERIAL\]/g,newIndex+1);
		c.innerHTML=str;
	}
	tableData[p][3][newIndex]=1;
	tableData[p][2]++;
	var tN=document.getElementById(tableData[p][0]+""+"Number");
	tN.value=tableData[p][2];
}


function selectMaping(obj,globalVar){
	var isNum=false;
	if(!GLOBALS.Check(globalVar))return;
	select_obj_id=obj.id.replace(/^([a-zA-Z0-9_]+)_input$/,"$1");
	select_obj=findObj(select_obj_id);
	if(obj.value.length<1){
		delSelectOption(select_obj);
		select_obj.disabled=true;
		return;
	}
	else if(obj.value.match(/^[0-9]+$/))isNum=true;
	data_temp=GLOBALS.Get(globalVar);
	select_obj.disabled=false;
	delSelectOption(select_obj);
	for(i=0;i<data_temp.length;i+=2){
		x1=data_temp[i+1];
		if((isNum && data_temp[i]!=obj.value) || 
			(isNum==false && x1.indexOf(obj.value)<0))continue;
		select_obj.length=i+1;
		select_obj.options[i].text = data_temp[i+1];
		select_obj.options[i].value = data_temp[i];
	}
	if(select_obj.length<=0)select_obj.disabled=true;
}
function loadSelectList(selectObj,varsList){
	if(selectObj.length>1)return;
	var indexed=selectObj.value;
	delSelectOption(selectObj);
	var defaultIndex=0;
	for(i=0;i<varsList.length;i++){
		selectObj.length=i+1;
		selectObj.options[i].text = varsList[i][1];
		selectObj.options[i].value = varsList[i][0];
		if(varsList[i][0]==indexed)defaultIndex=i;
	}
	selectObj.selectedIndex=defaultIndex;
	return;
}
function global_var(){
	var keyIndex=new Array();
	var datum=new Array();
	this.AddItem=function(p,v,t){
		if(t=="array"){
			datum[p]=new Array();
			for(j=0;j<v.length;j++){
				datum[p][j]=v[j];
			}
		}
		else{
			datum[p]=v;
		}
	}
	this.Add=function(k,v,t){
		for(i=0;i<datum.length;i++){
			if(keyIndex[i]==k){
				datum[i]=v;
				return;
			}
		}
		keyIndex[datum.length]=k;
		datum[datum.length]=v;
	}
	this.Get=function(k){
		for(i=0;i<datum.length;i++){
			if(keyIndex[i]==k)
				return datum[i];
		}
		return false;
	}
	this.Check=function(k){
		for(i=0;i<datum.length;i++){
			if(keyIndex[i]==k)
				return true;
		}
		return false;
	}
	this.Key=function(k){
		t=new Array();
		for(i=0;i<datum.length;i++){
			if(datum.indexOf(k)>=0)
				t[t.length]=k;
		}
		if(t.length>0)
			return t;
		return false;
	}
	this.Val=function(v){
	
	}
	this.Set=function(k,v){
		this.add(k,v);
	}
}

/*

sDataType...........................................
int/integer:	integer		[\-\+]?[0-9]+
float/double:	float		[\-\+]?[0-9]+(\.[0-9]+)?
english:		en-string	[a-zA-Z]+
number:			plus integer		[0-9]+
zip:			zip code	[1-9]\d{1,10}
email:			Email		\w+([\-\.]\w+)*@\w+([\-\.]\w+)*\.\w{2,6}

*/

var GLOBALS=new global_var();