
//
// keytype(): Insert zhuyinfuhao (bopomofo) into the "zhuyin"
//           text box.
//
function keytype(s){
	
	document.getElementById('zhuyin').value += s;
	
}

//
// pkeytype(): Insert punctuation keys into the 
//            "example" text box.
//
function pkeytype(s){
	
	document.getElementById('example').value += s;
	
}

//
// function showCard
//
function showCard(tabId,cardId){
	
	// Hide the cards:
	document.getElementById('card1').style.display="none";
	document.getElementById('card2').style.display="none";
	document.getElementById('card3').style.display="none";
	// Inactive color on the tabs:
	document.getElementById('tab1').style.backgroundColor="#706e6a";
	document.getElementById('tab2').style.backgroundColor="#706e6a";
	document.getElementById('tab3').style.backgroundColor="#706e6a";
	
	
	// Now show the selected card:
	document.getElementById(cardId).style.display="block";
	document.getElementById(tabId).style.backgroundColor="#938d7f";
	
}


	//
	// tobpmf(): Convert from pinyin to zhuyinfuhao
	//
	function tobpmf(){
		
		s=document.getElementById("pinyin").value;
		// Tone mark replacements:
		s=s.replace( /1/g , "ˉ");
		s=s.replace( /2/g , "ˊ");
		s=s.replace( /3/g , "ˇ");
		s=s.replace( /4/g , "ˋ");
		s=s.replace( /5/g , "");
		// Four-letter replacements:		
		s=s.replace( /juan/g , "ㄐㄩㄢ" );
		s=s.replace( /quan/g , "ㄑㄩㄢ" );
		s=s.replace( /xuan/g , "ㄒㄩㄢ" );
		s=s.replace( /iong/g , "ㄩㄥ" );
		s=s.replace( /iang/g , "ㄧㄤ" );
		s=s.replace( /ying/g , "ㄧㄥ" );
		s=s.replace( /yuan/g , "ㄩㄢ" );
		s=s.replace( /yong/g , "ㄩㄥ" );
		s=s.replace( /uang/g , "ㄨㄤ" );
		s=s.replace( /yang/g , "ㄧㄤ" );
		// Three-letter replacements:
		s=s.replace( /ian/g , "ㄧㄢ" );
		s=s.replace( /iao/g , "ㄧㄠ" );
		s=s.replace( /ang/g , "ㄤ" );
		s=s.replace( /eng/g , "ㄥ" );
		s=s.replace( /ong/g , "ㄨㄥ" );
		s=s.replace( /ing/g , "ㄧㄥ" );
		s=s.replace( /yun/g , "ㄩㄣ" );
		s=s.replace( /yan/g , "ㄧㄢ" );
		s=s.replace( /you/g , "ㄧㄡ" );
		s=s=s.replace( /yao/g , "ㄧㄠ" );
		s=s.replace( /yai/g , "ㄧㄞ" );
		// Three-letter replacements:
		s=s.replace( /zhi/g , "ㄓ" );
		s=s.replace( /chi/g , "ㄔ" );
		s=s.replace( /shi/g , "ㄕ" );
		// Two letter replacements:
		s=s.replace( /an/g , "ㄢ" );
		s=s.replace( /en/g , "ㄣ" );
		s=s.replace( /ai/g , "ㄞ" );
		s=s.replace( /ei/g , "ㄟ" );
		s=s.replace( /ao/g , "ㄠ" );
		s=s.replace( /ou/g , "ㄡ" );
		s=s.replace( /ie/g , "ㄧㄝ" );
		s=s.replace( /ye/g , "ㄧㄝ" );
		s=s.replace( /yi/g , "ㄧ" );
		s=s.replace( /iu/g , "ㄧㄡ" );
		s=s.replace( /ui/g , "ㄨㄟ" );
		s=s.replace( /yu/g , "ㄩ" );
		s=s.replace( /ya/g , "ㄧㄚ" );
		s=s.replace( /yo/g , "ㄧㄛ" );
		s=s.replace( /ue/g , "ㄩㄝ" );
		s=s.replace( /un/g , "ㄨㄣ" );
		s=s.replace( /u:/g , "ㄩ" );
		s=s.replace( /er/g , "ㄦ" );
		s=s.replace( /ri/g , "ㄖ" );
		s=s.replace( /re/g , "ㄖㄜ" );
		s=s.replace( /wu/g , "ㄨ" );
		//
		s=s.replace( /ju/g , "ㄐㄩ" );
		s=s.replace( /qu/g , "ㄑㄩ" );
		s=s.replace( /xu/g , "ㄒㄩ" );
		//
		s=s.replace( /zh/g , "ㄓ" );
		s=s.replace( /ch/g , "ㄔ" );
		s=s.replace( /sh/g , "ㄕ" );
		//
		s=s.replace( /zi/g , "ㄗ" );
		s=s.replace( /ci/g , "ㄘ" );
		s=s.replace( /si/g , "ㄙ" );
		//
		s=s.replace( /b/g , "ㄅ" );
		s=s.replace( /p/g , "ㄆ" );
		s=s.replace( /m/g , "ㄇ" );
		s=s.replace( /f/g , "ㄈ" );
		s=s.replace( /d/g , "ㄉ" );
		s=s.replace( /t/g , "ㄊ" );
		s=s.replace( /n/g , "ㄋ" );
		s=s.replace( /l/g , "ㄌ" );
		s=s.replace( /g/g , "ㄍ" );
		s=s.replace( /k/g , "ㄎ" );
		s=s.replace( /h/g , "ㄏ" );
		s=s.replace( /j/g , "ㄐ" );
		s=s.replace( /q/g , "ㄑ" );
		s=s.replace( /x/g , "ㄒ" );
		s=s.replace( /r/g , "ㄖ" );
		s=s.replace( /z/g , "ㄗ" );
		s=s.replace( /c/g , "ㄘ" );
		s=s.replace( /s/g , "ㄙ" );
		s=s.replace( /w/g , "ㄨ" );
		// vowel replacements:
		s=s.replace( /a/g , "ㄚ" );
		s=s.replace( /o/g , "ㄛ" );
		s=s.replace( /e/g , "ㄜ" );
		s=s.replace( /u/g , "ㄨ" );
		s=s.replace( /u/g , "ㄩ" );
		s=s.replace( /i/g , "ㄧ" );
		//alert(s);
		document.getElementById("zhuyin").value=s;
	}
	
	//
	// topinyin(): Convert from zhuyinfuhao to pinyin
	//
	function topinyin(){
		s=document.getElementById("zhuyin").value;
		s=s.replace(/ㄅ/g,"b");
		s=s.replace(/ㄆ/g,"p");
		s=s.replace(/ㄇ/g,"m");
		s=s.replace(/ㄈ/g,"f");
		//
		s=s.replace(/ㄉ/g,"d");
		s=s.replace(/ㄊ/g,"t");
		s=s.replace(/ㄋ/g,"n");
		s=s.replace(/ㄌ/g,"l");
		//
		s=s.replace(/ㄍ/g,"g");
		s=s.replace(/ㄎ/g,"k");
		s=s.replace(/ㄏ/g,"h");
		//
		s=s.replace(/ㄐ/g,"j");
		s=s.replace(/ㄑ/g,"q");
		s=s.replace(/ㄒ/g,"x");
		//
		s=s.replace(/ㄓ/g,"zh");
		s=s.replace(/ㄔ/g,"ch");
		s=s.replace(/ㄕ/g,"sh");
		s=s.replace(/ㄖ/g,"r");
		//
		s=s.replace(/ㄗ/g,"z");
		s=s.replace(/ㄘ/g,"c");
		s=s.replace(/ㄙ/g,"s");
		//
		s=s.replace(/ㄚ/g,"a");
		s=s.replace(/ㄛ/g,"o");
		s=s.replace(/ㄜ/g,"e");
		s=s.replace(/ㄝ/g,"e");
		// Dipthongs:
		s=s.replace(/ㄞ/g,"ai");
		s=s.replace(/ㄟ/g,"ei");
		s=s.replace(/ㄠ/g,"ao");
		s=s.replace(/ㄡ/g,"ou");
		//
		s=s.replace(/ㄢ/g,"an");
		s=s.replace(/ㄣ/g,"en");
		s=s.replace(/ㄤ/g,"ang");
		s=s.replace(/ㄥ/g,"eng");
		//
		s=s.replace(/ㄦ/g,"er");
		s=s.replace(/ㄧ/g,"i");
		s=s.replace(/ㄨ/g,"u");
		s=s.replace(/ㄩ/g,"u");
		//
		s=s.replace(/ˉ/g,"1");
		s=s.replace(/ˊ/g,"2");
		s=s.replace(/ˇ/g,"3");
		s=s.replace(/ˋ/g,"4");
		document.getElementById("pinyin").value=s;
		
	}

//
// fillForm:
//
function fillForm(simplified,traditional,pinyin,zhuyin,definition,example,illustration){
	
	document.getElementById("jianti").value       = simplified;
	document.getElementById("fanti").value        = traditional;
	document.getElementById("pinyin").value       = pinyin;
	document.getElementById("zhuyin").value       = zhuyin;
	document.getElementById("definition").value   = definition;
	document.getElementById("example").value      = example;
	document.getElementById("illustration").value = illustration;
	
}

//
// fillWebForm(): transfer search result into the Baidu or Google
// search form
//
function fillWebForm(simplified,traditional){
	//
	// The choice of whether to put simplified or traditional
	// into the google box is definitely arbitrary.  If you
	// search using simplified, normally you will get more 
	// results because mainland China is a lot bigger than
	// Taiwan and HK combined.  But, you still might want
	// to search traditional from Google.
	//
	// On the other hand, Baidu will only take simplified
	// and 台灣教育部國語辭典 will only take traditional:
	//
	document.getElementById("baidu").value	     = simplified;
	//
	// Google will do either a simplified or a traditional search,
	// but you have to specify which one you want, so we plug in
	// both results, but only if they are not the same (since often
	// they are the same, but of course not always):
	//
	if(simplified == traditional){
		document.getElementById("google").value = simplified;
	}else{
		document.getElementById("google").value	= simplified + " " + traditional;
	}
	document.getElementById("guoyucidian").value = traditional;
	//
	// Here we also fill in our own search box, so that the user
	// can do a refined search, for example on a single character
	// result:
	//
	document.getElementById("zhongwen").value    = simplified;
	
}

//
// convertString
//
function convertString(string,direction){
	   
	//
	// Get answer from the server:
	//
	var conversionWindow = window.open("","Convert","screenX=10, screenY=10, width=200, height=200,resizable=no");
	
	var OutputString = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0.1//EN\"\n";
	OutputString += "   \"http://www.w3.org/TR/html4/strict.dtd\">\n";
	OutputString += "<html>\n";
	OutputString += " <head>\n";
	OutputString += "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
	OutputString += "  <meta http-equiv=\"Content-Language\" content=\"en-us\">\n";
	OutputString += " </head>\n";
	OutputString += " <body>\n";
	OutputString += "  <h2>Searching ... please wait...</h2>\n";
	OutputString += "  <form enctype=\"multipart/form-data\" charset=\"UTF-8\" id=\"conversionForm\" method=\"post\" action=\"hanyu.php\">\n";
	OutputString += "   <input type=\"text\" name=\"convertString\" value=\"" + string + "\">\n";
	OutputString += "   <input type=\"hidden\" name=\"direction\" value=\"" + direction + "\">\n";
	OutputString += "   <input type=\"submit\" name=\"convert\" value=\"Convert\">\n";
	OutputString += "  </form>\n  </body>\n</html>\n";
	
	//
	// Create the document:
	//
	conversionWindow.document.write(OutputString);
	conversionWindow.document.close();
	//
	// Submit the form:
	//
	conversionWindow.document.getElementById("conversionForm").submit();
	
}

//
// function putQueryResultsIntoParentWindow(): called by the result window's <body> "onload" event:
//
function putQueryResultsIntoParentWindow(){
	
	if(window.opener){
		window.opener.document.getElementById("jianti").value   = document.getElementById("jianti").value;
		window.opener.document.getElementById("fanti").value    = document.getElementById("fanti").value;
	}
	
}

