//   Description        : Js file containig common functions
function res_populateCountryCity(formCountryId, formCityId)
{
   var countryEle = document.getElementById(formCountryId);
   var cityEle = document.getElementById(formCityId);
   var selI = countryEle.selectedIndex;

   // Remove existing elements from city
   cityEle.length = 0;

   if(countryEle[selI].value!="select")
   {
	var sp1 = countryEle[selI].value.split("|X|");
        var sp2 = sp1[1].split(",");
        var sp4=sp1[0];
   }
   var optSelect = new Option();
   optSelect.text = "-- Select --";
   optSelect.value = "select";
   cityEle[0] = optSelect;
   
   if(sp2)
   {
	   for (var i = 1; i <= sp2.length; ++i)
	   {
		sp3=sp2[i-1].split("#");
	       var opt = new Option();
	       opt.text = sp3[1];
	       opt.value = sp4+"."+sp3[0]+"#"+sp3[2];
	       cityEle[i] = opt;
	   }
   }
}

function isEmail(stremail)
{
 	if (stremail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
		return false;
	else
		return true;
}

function isDigit (c)
{
		 return ((c >= "0") && (c <= "9"))
}

function isInteger(iNumber)
{
	var i;
	
	for (i=0;i<iNumber.length;i++)
	{
		var c = iNumber.charAt(i);
	
		if (!isDigit(c))
		{
			return false;
		}
	}
	
  	return true;
}

// This function is used to left and right trim the string which is passed to it.
function nkr_trim(inputString) 
{
	if (typeof inputString != "string") { return inputString; }
	
	var temp_str = '';
	temp_str = inputString.replace(/[\s]+/g,"");
	if(temp_str == '')
        	return "";	

	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") 
	{
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) 
	{
	      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
	}
	return retValue;
}

//	Description	:	set the ISD values in the given ISD text fields
function nkr_fillISD(id_country,id_landisd,id_faxisd,id_mobisd)
{	
	var countryEle = document.getElementById(id_country);
	var splitArray = countryEle.value.split("|X|");
	var countryValue = splitArray[0];
	
	var codeArray = {1:213, 2:973, 3:20, 4:964, 5:962, 6:965, 7:961, 8:218, 9:212, 10:968, 11:92, 12: 970, 13:974, 14:966, 15:963, 16:216, 17:971, 80:91};
	
	var code;
	if ((code=codeArray[countryValue]) == null)
	{
		document.getElementById(id_landisd).value = '';
		document.getElementById(id_mobisd).value = '';
		if(id_faxisd != '')
			document.getElementById(id_faxisd).value = '';
	}
	else
	{
		document.getElementById(id_landisd).value = code;
		document.getElementById(id_mobisd).value = code;
		if(id_faxisd != '')
			document.getElementById(id_faxisd).value = code;
	}
}

//	Description	:	Return false if input string has any space else returns true
function nkr_findSpace(strInput)
{
	var strTemp;
	strTemp = strInput.replace(/[\s]/g,"");

	if(eval(strTemp.length) == eval(strInput.length))
		return true;
	
	return false;
}

//	Description	:	Validates the input date
function nkr_checkDate(day, month, year)
{
	// since jan equals one and not zero, hence thirteen elements in the array.  
	var no_of_days_in_month = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)

	if (month >= 1 && month <= 12 && day >=  1 && day <= 31 && year >= 0)
	{ 
	
		//Handling february, special case. 
		if (month == 2)
		{
			if ( (year%4==0 && year%100 != 0) || year%400 == 0 )
				no_of_days_in_month[month]=29
		}

		if (day >= 1 && day <= no_of_days_in_month[month])
			return true;
		else
			return false;
	}
	else
		return false;
}

//	Description	:	Returns true if input number is a float/double & false otherwise
function nkr_isFloat(iNumber)
{
	var i;
	
	for (i=0;i<iNumber.length;i++)
	{
		var c = iNumber.charAt(i);
	
		if (!isDigit(c) && c != '.')
		{
			return false;
		}
	}
	
  	return true;
}

function nkr_display(id, flag)
{
	if(ele = document.getElementById(id))
		ele.style.display = flag;		
}

function nkr_isPhoneNumber(ph)
{
	return isInteger(ph.replace(/[+-]+/g, ""));
}