
function writeError(str)
{
	var obj = document.getElementById("errmsg");
	if (obj) obj.innerHTML = str;  //I hate stupid Firefox!! innerText does not work!
}

function checkPhrase()
{
	var obj = document.getElementById("phrase");
	if (!obj) return "";
	str = obj.value;
	str = str.replace(/^\s*|\s*$/g,"")
	obj.value = str;
	return  str;
} 

function goGoogle()
{
	var str = checkPhrase();
	if (str != "")
		top.location='http://www.ecallchina.com/search/googlesearch.aspx?phrase=' + str;
	else
		writeError("please input your search phrase, for example, cheap phone card call China.");
}

function enterPress(e)
{
	if (!e) e = window.event;
	var kc = e.keyCode;
	if (kc==13 && checkPhrase() != "") //enter pressed
		goGoogle();
	else
		{if (kc!=32) writeError("");} //remove error message
}

var npa, nxx, country;
var discount = "0.50";
var referid = "eccsuggest";

function findcard()
{
	if (checkPhone() && checkCountry())
		top.location = "http://www.ecallchina.com/map/g"
			+ "_a" + npa 
			+ "_d" + ((discount) ? discount : "")
			+ "_m"
			+ "_i"
			+ "_n"
			+ "_o"
			+ "_p" + npa
			+ "_q" + nxx 
			+ "_r" + ((referid) ? referid : "")
			+ "_s"
			+ "_t" + country
			+ ".aspx";
}

function checkCountry()
{
	country = getinputValue('country');
	return (country.length>=3) ? true : false;
}

function checkPhone()
{
	var npanxx = getinputValue('npanxx');
	if (npanxx.length < 7) 
	{
		writeError("incorrect phone number");
		return false;
	}
	npa = npanxx.substring(0,3);
	nxx = npanxx.substring(4);
	if (parseInt(npa) <= 200 || parseInt(nxx) <0 || npa==false || nxx == false || isNaN(npa) || isNaN(nxx))
	{
		writeError("You input a wrong phone number");
		return false;
	}
	else
		return true;
}

function getinputValue(domid)
{
	var obj = document.getElementById(domid);
	return (obj) ? obj.value : false; 
}

function fillinValue(domid, str)
{
	var obj = document.getElementById(domid);
	if (obj) obj.value = str; 
}

function mask(e,str,textbox,loc,delim){

	//clear error message
	if(!e) e = window.event;
	var keycode = e.keyCode;
	//if (keycode!=32) writeError("the first 6 digits of your phone number starting with area code");
	//var sinput = String.fromCharCode(keycode);  //does not work for numeric keyborard
	var sinput = str.substring(str.length-1);
	if (!IsNumeric(sinput)) 
	{
		textbox.value = (str.substring(0,str.length-1));
		return false;
	}

	var locs = loc.split(',');

	for (var i = 0; i <= locs.length; i++){
		for (var k = 0; k <= str.length; k++){
		 if (k == locs[i]){
		  if (str.substring(k, k+1) != delim){
		    if (e.keyCode != 8){ //backspace
			str = str.substring(0,k) + delim + str.substring(k,str.length)
		    }
		  }
		 }
	}
	}
	textbox.value = str
	if(str.length==7) document.getElementById('country').focus();
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
	         IsNumber = false;
         }
      }
   return IsNumber;
}

function CountryNameKeyUp(obj,e,str)
{
	if(!e) e = window.event;
	var sinput = str.substring(str.length-1);
	if (IsNumeric(sinput)) 
	{
		obj.value = (str.substring(0,str.length-1));
		return false;
	}
	else
		return SendQuery(e,str);
}

function CountryBoxonFocus(obj)
{
	if (obj.value == "Country Name") {
		// this is the default value
		obj.value = "";
	} else {
		// this is the state with user query input
		obj.select();
	}
	writeError('type country name ... then choose it from our list');
}