/*
FunctionName: convertDMEIDtoHex
Purpose     : To Convert of 18 Digit Decimal Number to 14 Digit HEX Number 
Argument    : 18 Digit Decimal Number
Return		: 14 Digit HexaDecimal Number
*/
	function convertDMEIDtoHex(meID)
	{
		var invalid_char="Z"
		var HexValue = meID;
		//alert("HexValue=" + HexValue);
		var HexLength = HexValue.length;
		var DecFirst10digit = HexValue.substr(0,10);
		var DecLast8digit = HexValue.substr(10);
		//alert("HexVal=" + HexValue + "  First 10 Dec=" + DecFirst10digit + "  Last 8 Dec=" + DecLast8digit);
		if (((DecFirst10digit < 2684354560) || (DecFirst10digit > 4294967295)) && ((DecLast8digit < 0) || (DecLast8digit > 16777215)) ){
		//	alert('Inside Function Invalid Decimal Digit');
			return invalid_char;
		}
		//Deleting the left padded zero
		while(DecFirst10digit.charAt(0)=="0" && DecFirst10digit.length > 1){
			DecFirst10digit = DecFirst10digit.substr(1);
		}//while
		while(DecLast8digit.charAt(0)=="0" && DecLast8digit.length > 1){
			DecLast8digit = DecLast8digit.substr(1);
		}//while
		//alert("DecVal111=" + HexValue + "  First 10 Dec=" + DecFirst10digit + "  Last 8 Dec=" + DecLast8digit);

		var HexFirst8digit = new dec2hex(DecFirst10digit).toHex();
		var HexLast6digit = new dec2hex(DecLast8digit).toHex();
		//alert("HexFirst8digit " + HexFirst8digit + "HexLast6digit " + HexLast6digit);
		if (HexFirst8digit.length>8 || HexLast6digit.length>6){
			return invalid_char;
		}//if
		//For numbers less than 8 and 6 digits respectively, left padding zero
		while(HexFirst8digit.length < 8){
			HexFirst8digit = "0" + HexFirst8digit;
			//HexFirst8digit = eval(HexFirst8digit);
		}//while
		while(HexLast6digit.length < 6){
			HexLast6digit = "0" + HexLast6digit;
			//HexLast6digit = eval(HexLast6digit);
		}//while
		//concatenate the two Hex parts
		var FinalHex14Digit = HexFirst8digit + HexLast6digit
		//alert("FinalHex14Digit=" + FinalHex14Digit);
		return FinalHex14Digit;
	}

/*
FunctionName: getchecksum
Purpose     : To Calculate the CheckSum of the MeID
Argument    : 14 or 15 Digit Hexa-Decimal Number
Return		: CheckSum Digit
*/
	function getchecksum(meid)
	{
		var total = 0;
		var valAtIdx = -1;
		var doubleVal = -1;
		for (loop=0; loop<14; loop++)
		{
		  valAtIdx = parseInt(meid.substring(loop,loop+1),16);
	      if((eval(loop % 2))!=0)
	       {
			doubleVal = eval(valAtIdx * 2);
			total = total + Math.floor(eval(doubleVal/16));
			total = total + Math.round(eval(doubleVal % 16));
	       }	
	      else
			total = eval(total + eval(valAtIdx));
		}
		var checkSumExpected = eval(16 - Math.floor(eval(total%16)));
		if (checkSumExpected == 16){
	      checkSumExpected = 0;
		}
		var checksum = new dec2hex(checkSumExpected).toHex();
		return (checksum.toUpperCase());
	}

/*
FunctionName: validatemeid
Purpose     : To Get the Valid Meid for the Given Hex Number
Argument    : Valid HexaDecimal Number
Return		: If the Argument is correct, a Valid 15 Digit Hex MEID else value "Z" is returned.
*/

	function validatemeid(vmeid)
	{
		//alert('Inside ValidateMeid Function '+ vmeid);
		var check0 = parseInt(vmeid.substring(0,1),16);
		var check1 = parseInt(vmeid.substring(1,2),16);
	    if(check0<10 && check1<10)
  	    {
		//alert("MeID Number doesn't have a valid Prefix");
		  return "Z";
		}
		var checksum = getchecksum(vmeid);
	    var Hex14Digit = vmeid.substring(0,14)
/*		if(vmeid.length!=14)
		{
		  if(!(checksum == vmeid.charAt(14)))
           {
		   //alert("Invalid checksum value for MEID: " + vmeid + " Correct checksum:" + checksum);
		        return "Z";
		   }
		  return vmeid;
		}
*/
		return Hex14Digit + checksum;
	 }

