/******************************************************************************
 * http://ajas.us/
 * The way I want to do socials (Apr 11, 2007) rev. (Jun 14, 2007)
 * If you find an error, or improvement, let me know: dev at ajas dot us
 * This file is free for use
 * 
 * ajas.ui.magicSocial()           - call this onblur, scrubs and validates
 * ajas.validator.social.validate()- call for true/false validation
 * ajas.validator.social.parse()   - don't call this
 *
 * The function cancelBubble() is found in ajas.event.js
 *
 * highgroup codes found monthly at http://www.ssa.gov/employer/highgroup.txt
 * SSA details: http://www.ssa.gov/employer/ssnweb.htm
 *****************************************************************************/

if(!ajas)var ajas={};
if(!ajas.ui)ajas.ui={};
if(!ajas.validator)ajas.validator={};
if(!ajas.validator.social)ajas.validator.social={};

// use this function for form feedback
ajas.ui.magicSocial=function(oInput, bStrict, bDashed) {
	// Set preferred defaults here.
	// bStrict=true will filter currently known area & group codes
	// bStrict=false will filter 000 area & group codes
	if(arguments.length < 2) var bStrict=true;
	// bDashed=true will add dashes
	// bDashed=false will return nine digits without dashes
	if(arguments.length < 3) var bDashed=true;
	
	var sLabel = oInput.id + 'Msg';
	oInput.className = oInput.className.replace(/ajas_social_error/g, '');
	try {
		if (oInput.value == '') {
			document.getElementById(sLabel).innerHTML = '###-##-####';
			return;
		}
		var sSocial = ajas.validator.social.parse(oInput.value, bStrict);
		// Human readable SSN
		document.getElementById(sLabel).innerHTML = sSocial.substr(0,3)+'-'+sSocial.substr(3,2)+'-'+sSocial.substr(5,4);
		oInput.value=bDashed?sSocial.substr(0,3)+'-'+sSocial.substr(3,2)+'-'+sSocial.substr(5,4):sSocial;
		return true;
	} catch (e) {
		oInput.className += ' ajas_social_error';
		var message = e.message;
		// Fix for IE6 bug
		if (message.indexOf('is null or not an object') > -1) {
			message = 'Invalid SSN';
		}
		try{document.getElementById(sLabel).innerHTML=message;}catch(E){}
		return false;
	}

}

//use this function to get a quiet true/false response
ajas.validator.social.validate=function(sSocial) {
	try {
		ajas.validator.social.parse(sSocial, true);
	} catch (e) {
		return false;
	}
	return true;
}

ajas.validator.social.parse=function(sSocial, bStrict) {
	if(arguments.length < 2) var bStrict=true;

	if (aBits = /[a-z]/i.exec(sSocial)) {
		throw new Error("Invalid SSN");
	}

	sSocial = sSocial.replace(/[^0-9]/ig, '');

	if (sSocial.length != 9) {
		throw new Error("Invalid SSN length");
	}
	if (sSocial.substr(0,3) == '000') {
		throw new Error("Invalid SSN Area");
	}
	if (sSocial.substr(3,2) == '00') {
		throw new Error("Invalid SSN Group");
	}
	if (sSocial.substr(5,4) == '0000') {
		throw new Error("Invalid SSN Serial");
	}
	if (bStrict) {
		//check for unallocated areas (leading 3 digits)
		if (!ajas.validator.social.aHighGroups[sSocial.substr(0,3)]) {
			throw new Error("Invalid SSN Area");
		}
		//check for unallocated groups (middle 2 digits)
		var iHG=ajas.validator.social.aHighGroups[sSocial.substr(0,3)];
		var iSG=sSocial.substr(3,2);
		for(var i=0;i<ajas.validator.social.aGroupOrder.length;i++) {
			if (ajas.validator.social.aGroupOrder[i]==iSG) break;
			if (ajas.validator.social.aGroupOrder[i]==iHG) {
				throw new Error("Invalid SSN Group");
			}
		}
		
		//check for known invalid numbers/partials
		for(var i in ajas.validator.social.aAdvertSSN) {
			if (sSocial==ajas.validator.social.aAdvertSSN[i]) {
				throw new Error("Known Bad SSN");
			}
		}
		for(var i in ajas.validator.social.aBadCombo) {
			if (sSocial.substr(0,5)==ajas.validator.social.aBadCombo[i]) {
				throw new Error("Invalid SSN Area/Group");
			}
		}
	}

	return sSocial;
}

// "Within each area, the group number (middle two (2) digits) 
//  range from 01 to 99 but are not assigned in consecutive 
//  order. For administrative reasons, group numbers issued 
//  first consist of the ODD numbers from 01 through 09 and 
//  then EVEN numbers from 10 through 98, within each area 
//  number allocated to a State. After all numbers in group 98 
//  of a particular area have been issued, the EVEN Groups 02 
//  through 08 are used, followed by ODD Groups 11 through 99."
//  ODD - 01, 03, 05, 07, 09 
//  EVEN - 10 to 98
//  EVEN - 02, 04, 06, 08 
//  ODD - 11 to 99
ajas.validator.social.aGroupOrder= [
	'01','03','05','07','09','10','12','14','16','18','20','22',
	'24','26','28','30','32','34','36','38','40','42','44','46',
	'48','50','52','54','56','58','60','62','64','66','68','70',
	'72','74','76','78','80','82','84','86','88','90','92','94',
	'96','98','02','04','06','08','11','13','15','17','19','21',
	'23','25','27','29','31','33','35','37','39','41','43','45',
	'47','49','51','53','55','57','59','61','63','65','67','69',
	'71','73','75','77','79','81','83','85','87','89','91','93',
	'95','97','99'];
ajas.validator.social.aAdvertSSN = [
	'042103580','062360749','078051120','095073645','128036045',
	'135016629','141186941','165167999','165187999','165207999','165227999',
	'165247999','189092294','212097694','212099999','306302348','308125070',
	'468288779','549241889','987654320','987654321','987654322','987654323',
	'987654324','987654325','987654326','987654327','987654328','987654329'];
ajas.validator.social.aBadCombo = [
	'55019','58619','58629','58659','58679','58680','58681',
	'58682','58683','58684','58685','58686','58687','58688','58689','58690',
	'58691','58692','58693','58694','58695','58696','58697','58698','58699'];
	
//This function is useful for converting `highgroup.txt` to a JS object
ajas.validator.social.highGroupParse=function(s) {
	s="ajas.validator.social.aHighGroups={\n\t'"+s
		.substring(s.indexOf('001'))
		.replace(/\t/g,'  ')
		.replace(/\*/g,' ')
		.replace(/   /g,'  ')
		.replace(/ $/g,'').replace(/ $/g,'')
		.replace(/(\d) (\d)/g,"$1':'$2")
		.replace(/\r\n/g,'|').replace(/\r/g,'|').replace(/\n/g,'|')
		.replace(/\s+\|/g,'|')
		.replace(/\s+/g,"','")
		.replace(/\|+/g,"',\n\t'")
	;
	return s.substr(0,s.length-4)+"};";
}	
ajas.validator.social.aHighGroups={
	'001':'06','002':'04','003':'04','004':'08','005':'08','006':'06',
	'007':'06','008':'90','009':'90','010':'90','011':'90','012':'90',
	'013':'90','014':'90','015':'90','016':'90','017':'90','018':'90',
	'019':'90','020':'90','021':'90','022':'90','023':'90','024':'90',
	'025':'88','026':'88','027':'88','028':'88','029':'88','030':'88',
	'031':'88','032':'88','033':'88','034':'88','035':'72','036':'72',
	'037':'72','038':'70','039':'70','040':'11','041':'11','042':'11',
	'043':'11','044':'11','045':'11','046':'11','047':'08','048':'08',
	'049':'08','050':'96','051':'96','052':'96','053':'96','054':'96',
	'055':'96','056':'96','057':'96','058':'96','059':'96','060':'96',
	'061':'96','062':'96','063':'96','064':'96','065':'96','066':'96',
	'067':'96','068':'96','069':'96','070':'96','071':'96','072':'96',
	'073':'96','074':'96','075':'96','076':'96','077':'96','078':'96',
	'079':'96','080':'96','081':'96','082':'96','083':'96','084':'96',
	'085':'96','086':'96','087':'96','088':'96','089':'96','090':'96',
	'091':'96','092':'96','093':'96','094':'96','095':'96','096':'96',
	'097':'96','098':'96','099':'96','100':'96','101':'96','102':'96',
	'103':'94','104':'94','105':'94','106':'94','107':'94','108':'94',
	'109':'94','110':'94','111':'94','112':'94','113':'94','114':'94',
	'115':'94','116':'94','117':'94','118':'94','119':'94','120':'94',
	'121':'94','122':'94','123':'94','124':'94','125':'94','126':'94',
	'127':'94','128':'94','129':'94','130':'94','131':'94','132':'94',
	'133':'94','134':'94','135':'19','136':'19','137':'19','138':'19',
	'139':'17','140':'17','141':'17','142':'17','143':'17','144':'17',
	'145':'17','146':'17','147':'17','148':'17','149':'17','150':'17',
	'151':'17','152':'17','153':'17','154':'17','155':'17','156':'17',
	'157':'17','158':'17','159':'84','160':'84','161':'84','162':'84',
	'163':'84','164':'84','165':'84','166':'84','167':'84','168':'84',
	'169':'84','170':'84','171':'84','172':'84','173':'84','174':'84',
	'175':'82','176':'82','177':'82','178':'82','179':'82','180':'82',
	'181':'82','182':'82','183':'82','184':'82','185':'82','186':'82',
	'187':'82','188':'82','189':'82','190':'82','191':'82','192':'82',
	'193':'82','194':'82','195':'82','196':'82','197':'82','198':'82',
	'199':'82','200':'82','201':'82','202':'82','203':'82','204':'82',
	'205':'82','206':'82','207':'82','208':'82','209':'82','210':'82',
	'211':'82','212':'79','213':'79','214':'77','215':'77','216':'77',
	'217':'77','218':'77','219':'77','220':'77','221':'06','222':'04',
	'223':'99','224':'99','225':'99','226':'99','227':'99','228':'99',
	'229':'99','230':'99','231':'99','232':'53','233':'53','234':'53',
	'235':'53','236':'51','237':'99','238':'99','239':'99','240':'99',
	'241':'99','242':'99','243':'99','244':'99','245':'99','246':'99',
	'247':'99','248':'99','249':'99','250':'99','251':'99','252':'99',
	'253':'99','254':'99','255':'99','256':'99','257':'99','258':'99',
	'259':'99','260':'99','261':'99','262':'99','263':'99','264':'99',
	'265':'99','266':'99','267':'99','268':'13','269':'13','270':'13',
	'271':'13','272':'13','273':'13','274':'13','275':'13','276':'13',
	'277':'13','278':'13','279':'13','280':'13','281':'11','282':'11',
	'283':'11','284':'11','285':'11','286':'11','287':'11','288':'11',
	'289':'11','290':'11','291':'11','292':'11','293':'11','294':'11',
	'295':'11','296':'11','297':'11','298':'11','299':'11','300':'11',
	'301':'11','302':'11','303':'31','304':'31','305':'31','306':'31',
	'307':'31','308':'31','309':'31','310':'31','311':'31','312':'31',
	'313':'31','314':'31','315':'31','316':'29','317':'29','318':'06',
	'319':'06','320':'06','321':'06','322':'06','323':'06','324':'06',
	'325':'06','326':'06','327':'06','328':'06','329':'06','330':'06',
	'331':'06','332':'06','333':'06','334':'06','335':'06','336':'06',
	'337':'06','338':'06','339':'06','340':'06','341':'06','342':'06',
	'343':'04','344':'04','345':'04','346':'04','347':'04','348':'04',
	'349':'04','350':'04','351':'04','352':'04','353':'04','354':'04',
	'355':'04','356':'04','357':'04','358':'04','359':'04','360':'04',
	'361':'04','362':'33','363':'33','364':'33','365':'33','366':'33',
	'367':'33','368':'33','369':'33','370':'33','371':'33','372':'33',
	'373':'33','374':'33','375':'33','376':'33','377':'33','378':'33',
	'379':'33','380':'33','381':'33','382':'33','383':'33','384':'33',
	'385':'33','386':'31','387':'29','388':'29','389':'29','390':'29',
	'391':'27','392':'27','393':'27','394':'27','395':'27','396':'27',
	'397':'27','398':'27','399':'27','400':'67','401':'67','402':'67',
	'403':'67','404':'67','405':'65','406':'65','407':'65','408':'99',
	'409':'99','410':'99','411':'99','412':'99','413':'99','414':'99',
	'415':'99','416':'61','417':'61','418':'61','419':'61','420':'61',
	'421':'61','422':'61','423':'59','424':'59','425':'99','426':'99',
	'427':'99','428':'99','429':'99','430':'99','431':'99','432':'99',
	'433':'99','434':'99','435':'99','436':'99','437':'99','438':'99',
	'439':'99','440':'23','441':'23','442':'23','443':'23','444':'23',
	'445':'21','446':'21','447':'21','448':'21','449':'99','450':'99',
	'451':'99','452':'99','453':'99','454':'99','455':'99','456':'99',
	'457':'99','458':'99','459':'99','460':'99','461':'99','462':'99',
	'463':'99','464':'99','465':'99','466':'99','467':'99','468':'49',
	'469':'49','470':'49','471':'49','472':'49','473':'49','474':'49',
	'475':'49','476':'49','477':'49','478':'37','479':'37','480':'37',
	'481':'37','482':'37','483':'37','484':'35','485':'35','486':'25',
	'487':'25','488':'25','489':'25','490':'25','491':'25','492':'25',
	'493':'25','494':'23','495':'23','496':'23','497':'23','498':'23',
	'499':'23','500':'23','501':'33','502':'31','503':'39','504':'39',
	'505':'51','506':'51','507':'51','508':'51','509':'27','510':'27',
	'511':'27','512':'27','513':'27','514':'25','515':'25','516':'43',
	'517':'43','518':'77','519':'75','520':'53','521':'99','522':'99',
	'523':'99','524':'99','525':'99','526':'99','527':'99','528':'99',
	'529':'99','530':'99','531':'61','532':'61','533':'61','534':'61',
	'535':'61','536':'61','537':'61','538':'59','539':'59','540':'73',
	'541':'73','542':'73','543':'71','544':'71','545':'99','546':'99',
	'547':'99','548':'99','549':'99','550':'99','551':'99','552':'99',
	'553':'99','554':'99','555':'99','556':'99','557':'99','558':'99',
	'559':'99','560':'99','561':'99','562':'99','563':'99','564':'99',
	'565':'99','566':'99','567':'99','568':'99','569':'99','570':'99',
	'571':'99','572':'99','573':'99','574':'49','575':'99','576':'99',
	'577':'45','578':'43','579':'43','580':'37','581':'99','582':'99',
	'583':'99','584':'99','585':'99','586':'61','587':'99','588':'01',
	'589':'99','590':'99','591':'99','592':'99','593':'99','594':'99',
	'595':'99','596':'84','597':'84','598':'82','599':'82','600':'99',
	'601':'99','602':'63','603':'63','604':'63','605':'63','606':'63',
	'607':'63','608':'63','609':'63','610':'63','611':'63','612':'63',
	'613':'63','614':'63','615':'63','616':'63','617':'63','618':'61',
	'619':'61','620':'61','621':'61','622':'61','623':'61','624':'61',
	'625':'61','626':'61','627':'08','628':'08','629':'08','630':'08',
	'631':'08','632':'08','633':'08','634':'08','635':'08','636':'08',
	'637':'08','638':'08','639':'08','640':'08','641':'08','642':'06',
	'643':'06','644':'06','645':'06','646':'94','647':'92','648':'44',
	'649':'42','650':'44','651':'44','652':'44','653':'42','654':'26',
	'655':'26','656':'24','657':'24','658':'24','659':'14','660':'14',
	'661':'14','662':'14','663':'14','664':'14','665':'14','667':'34',
	'668':'34','669':'34','670':'32','671':'32','672':'32','673':'32',
	'674':'32','675':'32','676':'12','677':'12','678':'12','679':'12',
	'680':'86','681':'12','682':'12','683':'12','684':'12','685':'12',
	'686':'12','687':'12','688':'10','689':'10','690':'10','691':'07',
	'692':'07','693':'07','694':'07','695':'07','696':'05','697':'05',
	'698':'05','699':'05','700':'18','701':'18','702':'18','703':'18',
	'704':'18','705':'18','706':'18','707':'18','708':'18','709':'18',
	'710':'18','711':'18','712':'18','713':'18','714':'18','715':'18',
	'716':'18','717':'18','718':'18','719':'18','720':'18','721':'18',
	'722':'18','723':'18','724':'28','725':'18','726':'18','727':'10',
	'728':'14','729':'10','730':'09','731':'09','732':'09','733':'09',
	'750':'09','751':'07','752':'01','753':'01','756':'05','757':'05',
	'758':'03','759':'03','760':'03','761':'03','762':'03','763':'03',
	'764':'78','765':'76','766':'60','767':'60','768':'60','769':'60',
	'770':'60','771':'60','772':'60'};
