/*
	Copyright Infinite Solutions Co.,Ltd 2007;
	index.php v 1.0 2007.07.30;
	Infinite Solutions JS Main File
*/
function toconvert(otoutf,oelement)
{
	var sourceareastr = oelement.value;
	var strlen = sourceareastr.length;
	//document.board.destarea.value="";
	var str="";
	//alert(sourceareastr+oelement.id);
	if (otoutf=='wintoutf') { //convert from win1251 to unicode
		 for (i = 0; i < strlen;  i++) { 
		  	var codestr = sourceareastr.charCodeAt(i);
			//alert(codestr);
		   	switch(codestr) {
				case 184: codestr=1105;break; //eo
				case 168: codestr=1025;break; // capital EO
			
				case 175: codestr=1198; break; //capital UE
				case 191: codestr=1199; break; //ue
				
				case 170: codestr=1256; break; // capital OE
				case 186: codestr=1257; break; //oe
		   }
		   if (256>codestr && codestr>191) 
		   		codestr=codestr+848;
		   switch(codestr) {
				case 1111: codestr=1199;break;   //u
				case 1031: codestr= 1198; break; //capital U
				
				case 1108: codestr=1257; break; //ou
				case 1028: codestr=1256; break; //capital OU
		   }
			   str=str+String.fromCharCode(codestr);
		} //end for   
		
		oelement.value=str;
	   
	   
	 
	} 
	else 
		if (otoutf=='utftowin') { //convert from unicode to win1251
			   for (i = 0; i < strlen;  i++) { 
					var codestr = sourceareastr.charCodeAt(i);
					//alert(codestr);
					switch(codestr) {
						case 1025: codestr=168;break; // capital EO
						case 1105: codestr=184;break; //eo
							
						case 1198: codestr=175; break; //capital Ue
						case 1199: codestr=191; break; //ue
						
						case 1256: codestr=170; break; // capital OE
						case 1257: codestr=186; break; //oe
						
						case 415: codestr=170; break; // capital OE for Lucida sans Unicode
						case 629: codestr=186; break; // oe for Lucida sans Unicode
					}
					if (1280>codestr && codestr>1023) 
							codestr=codestr-848; 
					str=str+String.fromCharCode(codestr);
		   		}
	   			
				oelement.value=str;
	
	   		
        }
		
}

var editorMY=[];

	function checkFormConvert(oCheckformId)
	{
		var Checkform=document.getElementById(oCheckformId);
		if(Checkform)
		{
			inputInd=0;		
			while(Checkform.getElementsByTagName('input')[inputInd])
			{
				
				var testinput=Checkform.getElementsByTagName('input')[inputInd];
				if(testinput.type!='file'){
					toconvert('wintoutf',testinput);			
				}
				inputInd++;
			}
			
			textareaInd=0;
			while(Checkform.getElementsByTagName('textarea')[textareaInd])
			{			
				var testarea=Checkform.getElementsByTagName('textarea')[textareaInd];						
				toconvert('wintoutf',testarea);	
				
				
				if(editorMY && editorMY[testarea.id])
					editorMY[testarea.id].loadContentFromField();
				textareaInd++;
			}		
		}
		return true;
	}
	
	
function testErrOutput(objErrors,oElement)
{
	var message = objErrors.generalError + '<br />';
	//alert(message+oElement);
	if (objErrors.fieldErrors) 
	{
		for (var ii = 0; ii < objErrors.fieldErrors.length; ii++)
			message += (ii + 1) + ': Field "' + objErrors.fieldErrors[ii].field.title + '" ' + objErrors.fieldErrors[ii].errorMessage + "<br />";
		
		
		var outputDiv = document.getElementById(oElement+'_error');
	
		if(outputDiv != null)
		{
			outputDiv.innerHTML = message;
			outputDiv.style.display = "block";
		}
	}
}


var loader=[];
var loadstart=null;
var loaderst=0;

function indicatorCREATE(ocontainer){
	
	if(document.getElementById(ocontainer) ){
		if(loader[ocontainer]){
		
			//alert(ocontainer+'start'+loader[ocontainer].active);
			if(!loader[ocontainer].active){
		
				loader[ocontainer] = new Zapatec.Indicator({
						container: ocontainer
				});			
				loader[ocontainer].start('loading');
			}
		}
		else		
		{	
			//alert(ocontainer+'create');
			loader[ocontainer] = null;
			//loader[ocontainer].start('loading');
			if(loaderst==0){
				setTimeout(function(){loadstart = new Zapatec.Indicator({container: ocontainer});
				loadstart.start('loading');
				setTimeout("loadstart.stop();", 10);},1);
			}
			else{				
				loader[ocontainer] = new Zapatec.Indicator({container: ocontainer});
				loader[ocontainer].start('loading');
			}
		}
		
		
		loaderst=1;	
	}
	
}

function formvalidfunc(oform,ocontainer){
		
		indicatorCREATE(ocontainer);
		checkFormConvert(oform.id);
	
}

function myOnSuccess() 
{
	var outputDiv = document.getElementById("errOutput");

	if(outputDiv != null)
	{
		outputDiv.innerHTML = '';
		outputDiv.style.display = "none";
	}
	
	alert('Success!');
};

loadnumber=0;
var debugFILE='';


function myDebug(message,oElement,ourl,ospea,otitle,ocontent,omet)
{
	var debugEl = document.getElementById(oElement);
	if(debugEl)
	{		
		//alert('FORM'+message);
		if(loader[oElement])
			loader[oElement].stop();
		Zapatec.Transport.setInnerHtml({
			  html: message,
			  container: oElement
		});
				
		dhtmlHistory.add(historyLocationName(ourl.split(httphost)[1],omet,oElement,escape(ocontent),oElement,'true'), message); 
	}
}

function myDebugnohis(message,oElement,ourl,ospea,otitle,ocontent,omet)
{
	var debugEl = document.getElementById(oElement);
	if(debugEl)
	{		
		//alert('FORM'+message);
		if(loader[oElement])
			loader[oElement].stop();
		Zapatec.Transport.setInnerHtml({
			  html: message,
			  container: oElement
		});
	}
}



function historyLocSplit(olocation,omethod){
	
	
	if(omethod=='vsend')
		return olocation;
	var oRegex = new RegExp( '[\?&]' + omethod + '=([^&]+)', 'i' ) ;
		
	var oMatch = oRegex.exec( olocation ) ;
	
	if ( oMatch && oMatch.length > 1 )
		return oMatch[1] 
	else
		return '' ;
		
}

function historyLocationName(ourl,omethod,oelement,osend,obusyelement,ohistory,ofunc){
	
	var expFilename;
	var ofiletype;
	
	
	var strREP;
	var strREP2;
	strREP=ourl.split('.');
	filenameLen=strREP.length*1-1;
	ofiletype='.'+strREP[filenameLen];
	expFilename=ourl.split(ofiletype);
	filenameLen2=strREP.length*1-2;
	var strAS;
	var newLocNameCrea;
	strAS=ourl.split('?');
	strREP2=strAS[0].split('.');
	filenameLen3=strREP2.length*1-1;
	
	//alert(osend);
	if(omethod=='POST'){
		if(osend!='' && osend!=' ')
			osend='&'+osend;
		else
			osend='';
	}
	else{
		if(strAS[1]!='' && strAS[1]!=' ')
			osend='&'+strAS[1];
		else
			osend='';
	}
	
	newLocNameCrea='#vload=true&vhis='+ohistory+'&vpid='+oelement+'&vurl='+expFilename[0]+'&vtype='+omethod+'&vftype='+strREP2[filenameLen3]+'&vbusy='+obusyelement+osend+'&vend=true';
	
	
	return newLocNameCrea;
}

function removescript(oelement){
	
	var fcontainer=document.getElementById(oelement);
	jsind=0;
	var mainbody=document.getElementsByTagName('body')[0];
	while(fcontainer.getElementsByTagName('script')[jsind]){
		var oldscript=fcontainer.getElementsByTagName('script')[jsind];
		if(document.getElementById(oelement+'_'+jsind)){
			var removejs=document.getElementById(oelement+'_'+jsind)
			mainbody.removeChild(removejs);
		}
		jsind++;
	}
	
}

function loadscript(oelement){
	
	var fcontainer=document.getElementById(oelement);
	jsind=0;
	var mainbody=document.getElementsByTagName('body')[0];
	while(fcontainer.getElementsByTagName('script')[jsind]){
		var oldscript=fcontainer.getElementsByTagName('script')[jsind];
		
		var newscript=document.createElement('scr'+'ipt');
		newscript.text=oldscript.text;
		newscript.id=oelement+'_'+jsind;
		newscript.type="text/javascript";
		if(oldscript.src)
			newscript.src=oldscript.src;
		

		mainbody.appendChild(newscript);
		
		jsind++;
		
	}
	
}

function setHEMLloader(ohtml,ocontainer){
	Zapatec.Transport.setInnerHtml({
          html: ohtml,
          container: ocontainer
        });
}


function simplepage7(ourl,omethod,oelement,osend) 
{
	
	if(omethod=='GET' && osend!='' ){
		var strAS;
		
		strAS=ourl.split('?');
		if(strAS[1])
			ourl=ourl+'&'+osend;
		else
			ourl=ourl+'?'+osend;
		osend='';
		
	}
	
	Zapatec.Transport.fetch({
    
    url: ourl,    
    method: omethod,
	content: osend,
    onLoad: function(objRequest) {
			var strHtml = objRequest.responseText;
		//alert(oelement);
			setHEMLloader(strHtml,oelement);
    }
  
  });
	
}


function simplepage(ourl,omethod,oelement,osend,obusyelement,ohistory,ofunc) 
{
	indicatorCREATE(oelement);
	if(ohistory=='true'  ){
		//window.scrollTo(0,0);
	}
	if(omethod=='GET' && osend!='' ){
		var strAS;
		
		strAS=ourl.split('?');
		if(strAS[1])
			ourl=ourl+'&'+osend;
		else
			ourl=ourl+'?'+osend;
		osend='';
		
	}
	//document.getElementById(oelement).innerHTML='';
	
	Zapatec.Transport.fetch({
		url: ourl,    
		method: omethod,
		content: osend,
		onLoad: function(objRequest) {
				if(loader[oelement])
					loader[oelement].stop();
		
				var strHtml = objRequest.responseText;
			
				setHEMLloader(strHtml,oelement);
				
				if(ohistory=='true'  ){
					dhtmlHistory.add(historyLocationName(ourl,omethod,oelement,osend,obusyelement,ohistory), objRequest.responseText); 
				}
				if(typeof(ofunc)=="function")				
										ofunc();  
		}//,
	   //busyContainer: obusyelement
  });
	
}


function simplepageRefresh(ourl,omethod,oelement,osend,obusyelement,ohistory,ocurrent) 
{
	indicatorCREATE(oelement);

	if(omethod=='GET' && osend!='' ){
		var strAS;
		strAS=ourl.split('?');
		if(strAS[1])
			ourl=ourl+'&'+osend;
		else
			ourl=ourl+'?'+osend;
		osend='';
	}
		
		
	Zapatec.Transport.fetch({
		
		url: ourl,    
		method: omethod,
		content: osend,
		onLoad: function(objRequest) {			
			if(loader[oelement])
				loader[oelement].stop();
			var strHtml = objRequest.responseText;
			Zapatec.Transport.setInnerHtml({
				  html: strHtml,
				  container: oelement
			});
			dhtmlHistory.add('#'+ocurrent, objRequest.responseText); 
		}
	  });
}
