/**
	ET-TelConvert-1998 
	(for personal and non-profit use)

	(c) Copyright 2005, Dr. Bedilu Habte.
	bediluh@telecom.net.et

	All rights reserved.

	Permission is granted to use this software in personal and non-profit
	applications ONLY, subject to the following restrictions:

	The origin of this software must not be misrepresented.  If you use this
	software in a product, an acknowledgment in the product is required.

	The copyright notice must not be modified or removed from any of the
	source code.

	YOU MAY MODIFY OR REDISTRIBUTE THE SOFTWARE.

	THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL 
	THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT OR INDIRECT DAMAGES ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.

	For commercial use please contact: bediluh@telecom.net.et

	JavaScript code remain the intellectual property of Dr. Bedilu Habte.
	(c) Copyright 2005.  All rights reserved.
*/

// variable for old and new telephon numbers
var oldTel, newTel;

// variable for the number of digits in the old telephon 
// (length of 2, 3, 6 or 7 are possible)
var digs = 0;

// variable for old and new area codes
var oldCode, newCode;

// variable for number to be added in front
var add;

// message as a final output
var msg;

/**
Addis Ababa Normal Fixed Line Numbers
*/

// North Addis
var AAf1 = new Array(11,12,14,22,23,24,25,27,31,35,55,56,57,58,87,88);

// West Addis
var AAf2 = new Array(13,36,37,38,59,70,75,76,77,78,79,83,84);

// South West Addis
var AAf3 = new Array(20,21,30,41,48,49,71,72,73,74);

// South Addis
var AAf4 = new Array(16,19,32,33,34,39,40,42,43,65,66,67,68);

// Central Addis
var AAf5 = new Array(15,17,44,50,51,52,53,54);

// East Addis
var AAf6 = new Array(18,26,29,45,46,47,60,61,62,63,80,81,85);

// All normal fixed
var AAfall = new Array(AAf1,AAf2,AAf3,AAf4,AAf5,AAf6);

// array of starting of special old numbers in Addis
AASold = new Array(315,316,318,883,599,820,835,825,306,308,380,389,415,387,640,648,865,860,875);

// array of starting of special new numbers in Addis ( to be changed to)
AASnew = new Array(1320,1330,1340,1860,2580,2820,2850,2860,3310,3320,3380,3390,3420,3870,6640,6650,6860,6870,6880);

/**
Mobile Numbers
*/
// group 1 add in front "1"
var AAm1 = new Array(10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
40,41,42,43,44,45,46,47,48,49,50,60,61,62,63,64,65,66,67,68,69,
84,86,87,88,89);

// group 2 add in front "4"
var AAm2 = new Array(30,31,70,71,72);

// group 3 add in front "5"
var AAm3 = new Array(32,33,73,74,75);

// group 4 add in front "6"
var AAm4 = new Array(58,82,83);

// group 5 add in front "7"
var AAm5 = new Array(55,57,80,81);

// group 6 add in front "8"
var AAm6 = new Array(34,35,76,77);

// All normal fixed
var AAmall = new Array(AAm1,AAm2,AAm3,AAm4,AAm5,AAm6);

// Array of special (service) numbers
var service = new Array(97,98,99,900,901,902,903,904,905,906,907,908,
			911,912,913,915,916,918,919,929,
			950,951,952,953,954,955,956,957,958,
			959,960,961,962,963,964,965,966,967,969);

// check whether numbers are typed and 
// length is 2, 3, 6 or 7 
function checkNum(input) {
	var onlyThese ="0123456789";
	digs = input.length;
	
	if(digs != 2 && digs != 3 && digs != 6 && digs != 7) 
		return false;
		
	for (var i = 0; i < digs.length; i++)
		if (onlyThese.indexOf(input.charAt(i)) < 0 ) return false;
	
	return true;
}

// Addis and environs
function convertTelephon(code, number){

    oldCode = code;
    oldTel = number;
    
    newTel = "";

	msg = "Please check your input, and try again.<p>";
    
    // first check numbers are input
    if(checkNum(oldTel)){
    
		// first call function to convert the area code
		convertCode(code);
    
    	//if(digs == 2 || digs == 3) convertService(oldTel);
    	if(code == "01"){ // for fixed normal/chordless lines (code = 01)
			if (digs == 7) convertWireless(oldTel);
			else if(digs == 6) convertAddis(oldTel);
			else if(digs == 2 || digs == 3) convertService(oldTel);
    	} else if(code == "09"){ // for mobile or handy (code = 09)
        	if(digs == 6) convertMobile(oldTel);
        } else if(code == "091"){ // for VSAT Service
        	if(digs == 6) convertVsat(oldTel);		
    	} else {   // for regional fixed telephone numbers (code 02 - 08)
			if(digs == 6) convertRegional(oldTel);
			else if(digs == 2 || digs == 3) convertService(oldTel);
    	}
    	
		// output converted value
		if (newTel != "") outMsg();
		else outErr(msg); 
	} else {		
		outErr(msg);
	}
}

// to convert area codes
function convertCode(c){
	newCode = "";
	switch(c) {
 		case "01":
 			newCode = "011";
 			break;
 		case "02":
 			newCode = "022";
 			break;
 		case "03":
 			newCode = "033";
 			break;
 		case "04":
 			newCode = "034";
 			break;
  		case "05":
 			newCode = "025";
 			break;
 		case "06":
 			newCode = "046";
 			break;
 		case "071": // this region has two cases!!
 			newCode = "047";
 			oldCode = "07";
 			break;
 		case "072":
 			newCode = "057";
 			oldCode = "07";
 			break;
 		case "08":
 			newCode = "058";
 			break;
 		case "09":
 			newCode = "091";
 			break;
 		 case "091":
 			newCode = "098";
 			break;
 	}
}

// to convert telephone numbers in Addis Ababa and Environs
function convertAddis(n){
	
	newTel = "";
    // get the first two digits from the telephon number
    var first2 = n.substring(0,2);

    // get the first three digits from the telephon number
    var first3 = n.substring(0,3);

	// check for special numbers with irregular change
	for(i=0;i<AASold.length;i++){
		if(first3 == AASold[i]){
			// to convert special fixed telephone numbers in Addis Abeba 
			//   (irregular changes)
			newTel = AASnew[i]+""+n.substring(3);
		}
	}

    // get the number of digits in a telephon number
    var regions = n.length;
    
    // for fixed normal/chordless lines (code = 01)	
	if (newTel == ""){
		for(i=0;i<AAfall.length;i++){
			for(j=0;j<AAfall[i].length;j++){
				if(first2 == AAfall[i][j])newTel = ""+(i+1)+""+n;
			}
		}
	}
}


// to convert all mobile telephone numbers
function convertMobile(n){

	// get the first two digits from the telephon number
    var first2 = n.substring(0,2);
    
	for(i=0;i<AAmall.length;i++){
		for(j=0;j<AAmall[i].length;j++){
			if(first2 == AAmall[i][j]){
				if(i == 0) add = (i+1);
					else add = (i+3);
				newTel = ""+add+""+n;
			}
		}
	}        
}

// to convert Wireless telephone numbers (7 digits)
function convertWireless(n){
	newTel = "";
	// get the first three digits from the telephon number
    var first3 = n.substring(0,3);
    
    if(first3 == "251" ||
       first3 == "252" ||
       first3 == "253" ||
       first3 == "254" ||
       first3 == "255") newTel = "6" + n.substring(1);
    else {
    	msg = "Wrong number, try another.<p>";
    }
}

// to convert service numbers of the telecom
function convertService(n){
	newTel = "";
	// get the length of available service numbers
    var len = service.length;
    
    // 
	for(i=0;i<len;i++){
		if(n == service[i]){
			newTel = n;
			if(digs == 2) newTel = "9" + n;
		}
	}
}

// to convert VSAT numbers 
function convertVsat(n){
	newTel = "";
	// get the first digit from the telephon number
    var first2 = n.substring(0,2);
    
    // faraway == 11 or dialup == 19
	if(first2 == "11" || first2 == "19") newTel = "1"+n;
}

// to convert regional telephone numbers (outside Addis Ababa)
function convertRegional(n){
	newTel = "";
	// get the first digit from the telephon number
    var first1 = n.substring(0,1);
    
    if(first1 > 0 && first1 < 9) newTel = first1 + "" + n;
}

// for error messages
function outErr(n){
	alert("ERROR in the number.\nPlease input numbers only, without a space!");
	parent.frames[1].document.write(n);
}

// to output the final message
function outMsg(){
	var prev = ("Old Area Code: ".bold() + oldCode.fontsize(5) +
	            "\nOld Phone Number: ".bold() + 
	            oldTel.fontsize(5)).fontcolor("#ff0000");
	var curr = ("New Area Code: ".bold() + newCode.fontsize(5) +
	            "\nNew Phone Number: ".bold() + 
	            newTel.fontsize(5)).fontcolor("#006400");

var callusa=("To call from USA and Canada: (011) 251 ".fontsize(5) + newCode.substring(1).fontsize(5) + "  ".fontsize(5) + newTel.substring(0,3).fontsize(5)+ "-".fontsize(5)+ newTel.substring(3).fontsize(5)).fontcolor("#000000");

var calleu=("To call from Europe: (00) 251 ".fontsize(5) + newCode.substring(1).fontsize(5) + "  ".fontsize(5) + newTel.substring(0,3).fontsize(5)+ "-".fontsize(5)+ newTel.substring(3).fontsize(5)).fontcolor("#000000");

var callkenya=("To call from Kenya: (000) 251 ".fontsize(5) + newCode.substring(1).fontsize(5) + "  ".fontsize(5) + newTel.substring(0,3).fontsize(5)+ "-".fontsize(5)+ newTel.substring(3).fontsize(5)).fontcolor("#000000");

var accessurl=("<a href=\"http://www2.bt.com/btPortal/application?origin=leftnav_links.jsp&event=bea.portal.framework.internal.refresh&pageid=edq_icg&com.bea.event.type=linkclick&oLDesc=ICG++link+from+left+nav&oSiteArea=edq&oPJsp=leftnav_links.jsp&oPt=EDQNavPortlet&oLName=link.international&oOJsp=&oPg=edq&siteArea=edq\" target=\"_blank\">" + " Access Code </a>");

var callother=("To call from all other: (XXX) 251 ".fontsize(5) + newCode.substring(1).fontsize(5) + "  ".fontsize(5) + newTel.substring(0,3).fontsize(5)+ "-".fontsize(5)+ newTel.substring(3).fontsize(5) + (" where (XXX) is the international")+ accessurl ).fontsize(5).fontcolor("#000000");


	msg = (prev.small()).strike() + "  " + curr + "<br>" + callusa +  "<br/>" + calleu + "<br>" + callkenya + "<br>" + callother + "<p>" ;		

	parent.frames[1].document.write(msg);
}
