//Float div
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var busy = false;
var fading = false;

function floatDiv(id, sy)
{
	var fix_popup = false;
	if (arguments[2]){
		fix_popup = true;
	}
	var pageDimensions = getPageSize();
	var elem_W = elementById(id).offsetWidth;
	var sx = (pageDimensions[2]/2) - (elem_W/2);
	
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		
		if (fix_popup){ 
			this.cx += (pX + this.sx - this.cx);this.cy += (pY + this.sy - this.cy);
		} else {
			this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		}
		this.sP(this.cx, this.cy);
		//setTimeout(this.id + "_obj.floatIt()", 40);
	}
	return el;
}

/*function attach(id, funct){
	elementById(id).onkeyup = function(){eval(funct+"('"+id+"')");};
	elementById(id).onkeydown = function(){eval(funct+"('"+id+"')");};
	elementById(id).onblur = function(){eval(funct+"('"+id+"')");};
}*/

function attach(id, funct){
	elementById(id).onkeyup		= function(){eval(funct);};
	elementById(id).onkeydown	= function(){eval(funct);};
	elementById(id).onblur		= function(){eval(funct);};
}

function detach(id){
	elementById(id).onkeyup = null;
	elementById(id).onkeydown = null;
	elementById(id).onblur = null;
}	


function elementById(id){
	var ea;
	for( var i = 0; i < arguments.length; i++ ) {
		var e = arguments[i];
		if( typeof e == 'string' ) e = document.getElementById(e);
		if( arguments.length == 1 ) return e;
		if( !ea ) ea = new Array();
		ea[ea.length] = e;
	}
	return ea;
}


function trim(string) {
	while (string.substring(0,1) == ' '){
		string = string.substring(1, string.length);
	}
	while (string.substring(string.length-1, string.length) == ' '){
		string = string.substring(0,string.length-1);
	}
	return string;
}

/* innerHTML that works in IE without using textnode */
var myInnerHTML = function(text, id){
	if(trim(text) != ''){
		if(elementById(id+'_txt')){
			elementById(id+'_txt').innerHTML = text;
		} else {
			var newInnerHTML = document.createElement('div');
			newInnerHTML.innerHTML = text;
			newInnerHTML.setAttribute('id', id+'_txt');
			elementById(id).appendChild(newInnerHTML);
		}
	} else {
		try{
			elementById(id+'_txt').parentNode.removeChild(elementById(id+'_txt'));
		} catch(e){}
	}
}

/* date validation */
function febException(year){
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function validateDate(month, day, year){
	var february = febException(year);
	var daysInMonth = new Array(0 ,31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);	
	if(day > 0 && day <= daysInMonth[month] && year != 0){
		return true;
	}
	return false;
}


var checkLiteral = function(inputid){
	try{
		elementById(inputid).value = elementById(inputid).value.replace( /[^a-zA-Z ']/gi, '' );
		elementById(inputid).value = elementById(inputid).value.replace( /( {2,})/gi, ' ' );
	} catch(e){}
}


var checkNumerical = function(inputid){
	try{
		elementById(inputid).value = elementById(inputid).value.replace( /[^0-9]/gi, '' );
	} catch(e){}
}


function radioButtonChecker(formname, button){
	var r_sel = false;
	for (index=0; index<eval("document."+formname+"."+button+".length"); index++){
		if (eval("document."+formname+"."+button+"[index].checked"))
		r_sel = true; 
	}
	if (!r_sel){
		return false;	
	}
	return true;	
}

/* changes field CSS attributes to reflect error statuses */
function displayFieldError(input_id){
	elementById(input_id).className = elementById(input_id).className.replace( /field/gi, 'fld_err' ); 
}

function clearFieldError(input_id){
	elementById(input_id).className = elementById(input_id).className.replace( /fld_err/gi, 'field' );
}

function clearFieldErrors(fld){
	var count = fld.length;
	var fld_name = '';
	for (i=0 ; i<count ; i++ ){
		if(!elementById(fld[i])){
			continue;
		}
		elementById(fld[i]).className = elementById(fld[i]).className.replace( /fld_err/gi, 'field' );
		if (fld[i].match(/(ssn|phone)/g) && fld[i].match(/type/) == null){
			temp = fld[i].split('_');
			fld_name = temp[0];
		} else if (fld[i].match(/birth/)){
			fld_name = 'birth';
		} else {
			fld_name = fld[i];
		}
		myInnerHTML('', fld_name+'_err');
	}
}

function validateSSN(SSN1, SSN2, SSN3){
	checkNumerical(SSN1);
	checkNumerical(SSN2);
	checkNumerical(SSN3);		
	if(elementById(SSN1).value.length != 3 || elementById(SSN2).value.length != 2 || elementById(SSN3).value.length != 4){
		return false;
	}		
	return true;	
}
function validatePhoneNumber(no_1, no_2, no_3){
	checkNumerical(no_1);
	checkNumerical(no_2);
	checkNumerical(no_3);		
	if(elementById(no_1).value.length != 3 || elementById(no_2).value.length != 3 || elementById(no_3).value.length != 4){
		return false;
	}		
	return true;	
}

/* a JS replica of PHP in_array() function*/
Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return x;
	}
	return -1;
}

function getPageSize(){       
	var xScroll, yScroll; 
	
	if (window.innerHeight && window.scrollMaxY) {  
					xScroll = document.body.scrollWidth; 
					yScroll = window.innerHeight + window.scrollMaxY; 
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 
					xScroll = document.body.scrollWidth; 
					yScroll = document.body.scrollHeight; 
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 
					xScroll = document.body.offsetWidth; 
					yScroll = document.body.offsetHeight; 
	} 
	
	var windowWidth, windowHeight; 
	if (self.innerHeight) { // all except Explorer 
					windowWidth = self.innerWidth; 
					windowHeight = self.innerHeight; 
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode 
					windowWidth = document.documentElement.clientWidth; 
					windowHeight = document.documentElement.clientHeight; 
	} else if (document.body) { // other Explorers 
					windowWidth = document.body.clientWidth; 
					windowHeight = document.body.clientHeight; 
	}       
	
	// for small pages with total height less then height of the viewport 
	if(yScroll < windowHeight){ 
					pageHeight = windowHeight; 
	} else { 
					pageHeight = yScroll; 
	} 
	
	// for small pages with total width less then width of the viewport 
	if(xScroll < windowWidth){      
					pageWidth = windowWidth; 
	} else { 
					pageWidth = xScroll; 
	} 
	
	
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize; 
}

function get_y(obj) {
	if(obj.y) {
		y_coord = obj.y;
	}
	else if(obj.offsetParent){
		var y_coord = 0;
		while (obj.offsetParent) {
			y_coord += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}	
	return y_coord;
}

function get_x(obj) {
	if(obj.x) {
		x_coord = obj.x;
	}
	else if(obj.offsetParent){
		var x_coord = 0;
		while (obj.offsetParent) {
			x_coord += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	return x_coord;
}

function encodeForm(formname){
	var form = document.forms[formname];
	var codedString='';
	var ampersand='';
	var formLength=form.elements.length;
	for(index=0;index<formLength;index++){
		var element=form.elements[index];
		if(!element.name||element.name==''||element.name=='undefined'||!element.value||element.value==''){continue;}
		switch(element.type){
			case'text':
			case'hidden':
			case'password':
			case'textarea':
			case'select-one':
				codedString+=ampersand+element.name+'='+encodeURIComponent(trim(element.value));
				break;
			case'radio':
			case'checkbox':
				if(element.checked)
				codedString+=ampersand+element.name+'='+encodeURIComponent(element.value);
				break;
		}
		ampersand='&';
	}
	//alert(codedString);
	
	if (arguments[1]){
		return codedString + '&' + arguments[1];
	} else {
		return codedString;
	}
}

/* popup BOX */
	var popInfoBox = function(obj, content){
		if(elementById('pop_up_info_box')){
			removePopInfoBox();
			if(arguments[3]){
				return popInfoBox(obj, content,arguments[2],arguments[3]);
			} else if(arguments[2]) {
				return popInfoBox(obj, content,arguments[2]);
			} else {
				return popInfoBox(obj, content);				
			}
		}
		var title = '&nbsp;';
		if(arguments[2]){
			title = arguments[2];
		}
		var box_popup = document.createElement('div');
		box_popup.setAttribute('id', 'pop_up_info_box');		
		box_popup.innerHTML			= 	'<table border="0" cellspacing="0" cellpadding="0" style="width:100%">'
																+	  '<tr>'
																+		'<td class="NW"></td>'
																+		'<td class="N"></td>'
																+		'<td class="NE"></td>'
																+	  '</tr>'
																+	  '<tr>'
																+		'<td class="W"></td>'
																+		'<td class="core"><div class="header" onclick="closePopInfoBox()">'+title+'</div><div id="pop_up_info_box_core" style="padding:0 5px 5px 5px"></div></td>'
																+		'<td class="E"></td>'
																+	  '</tr>'
																+	  '<tr>'
																+		'<td class="SW"></td>'
																+		'<td class="S"></td>'
																+		'<td class="SE"></td>'
																+	  '</tr>'
																+	'</table>';
		box_popup.className = 'hidden';
		document.body.appendChild(box_popup);
		elementById('pop_up_info_box_core').innerHTML = content;
		var content_W = 400;
		if(arguments[3]){
			var content_W = arguments[3];
		}
		box_popup.style.width = content_W+'px';		
		positionPopUp(obj);
		return false;
	}
	
	var positionPopUp = function(obj){
		var pageDimensions = getPageSize();
		var x = get_x(obj);
		var y = get_y(obj);
		var popup_W = elementById('pop_up_info_box').offsetWidth;
		var popup_H = elementById('pop_up_info_box').offsetHeight;
			
		var tent_X = x + 15;
		var tent_Y = y - (popup_H/2);		
		
		if((tent_X + popup_W) > (pageDimensions[0]-15)){
			tent_X = (pageDimensions[0]-15) - popup_W;
		}
		
		if(tent_Y < 5){
			tent_Y = y;
		}
		else if((tent_Y + popup_H) > (pageDimensions[1]-5)){
			tent_Y = (pageDimensions[1]-5) - popup_H;
		}
		
		elementById('pop_up_info_box').style.top = tent_Y+'px';
		elementById('pop_up_info_box').style.left = tent_X+'px';
		elementById('pop_up_info_box').className = 'displayed';
	}	
	
	var closePopInfoBox = function(){
		if(fading==false && elementById('pop_up_info_box')){
			Effect.Fade('pop_up_info_box');
		}
		return false;
	}
	
	var removePopInfoBox = function(){
		try{
			document.body.removeChild(elementById('pop_up_info_box'));
		} catch(e){}
	}

function relation(obj){
	elementById('div_relation_other').className = obj.value == 'other' ? 'displayed' : 'hidden'; 
}

function address(answer){
	elementById('div_not_address').className = answer == 'Y' ? 'hidden' : 'displayed'; 
}

function addUserCmCheckbox(chk){
        elementById('div_not_cmuser').className = chk.checked == 1 ? 'displayed' : 'hidden';
}

function livedAddress(frm) {
	if (!radioButtonChecker(frm, 'lived_at_address') || elementById('laa_Y').checked){elementById('div_not_address').className = 'hidden'}
}

/*** XML error parser ***/

function processError(xml_string){
	//alert(xml_string);
	output_array = Array();

	
	try //Internet Explorer
	  {
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  xmlDoc.loadXML(xml_string);
	  }
	catch(e)
	  {
	  try //Firefox, Mozilla, Opera, etc.
	  {
	  parser=new DOMParser();
	  xmlDoc=parser.parseFromString(xml_string,"text/xml");
	  }
	  catch(e)
	  {
	  alert(e.message);
	  return;
	  }
	}
	var root = xmlDoc.documentElement;	
	node_length = root.childNodes.length;	
	for (i=0 ; i<node_length ; i++){
		
		id = root.childNodes[i].nodeName;
			
		var err_id = id+'_err';
		msg = root.childNodes[i].childNodes[0].nodeValue;
		if(elementById(id)){
			elementById(id).className = elementById(id).className.replace( /field/gi, 'fld_err' );	
		}
			
	
		if (id.match(/birth/)){
			err_id = 'birth_err';
		} else if (id.match(/ssn/)){
			err_id = 'ssn_err';
		} else if (id.match(/phone/) && id.match(/type/) == null){
			err_id = 'phone_err';
		} else if (id.match(/gender/)){
			err_id = 'gender_err';
		}

		if (document.getElementById(err_id)){
			myInnerHTML(msg, err_id);
		}
	}
}

function implode(arr){
	var str = '';
	for(i=0; i<arr.length; i++){
		if(arr[i] != null){
			if(str != ''){
				str += ',';
			}
			str += arr[i];			
		}		
	}
	return str;
}

function showPPoint(obj){
	obj.className = 'ppoint_s';
	var id = obj.getAttribute('id');
	elementById('expl_'+id).style.display = 'block';
	return false;
}

function hidePPoint(id){
	elementById(id).className = 'ppoint';
	elementById('expl_'+id).style.display = 'none';
	return false;
}

function navInit(){
	elementById('tab_how').onmouseover = function(){elementById('tab_how_sm').className='displayed';};
	elementById('tab_how').onmouseout = function(){elementById('tab_how_sm').className='hidden';};
			
	elementById('tab_facts').onmouseover = function(){elementById('tab_facts_sm').className='displayed';};
	elementById('tab_facts').onmouseout = function(){elementById('tab_facts_sm').className='hidden';};
	
	elementById('tab_resource').onmouseover = function(){elementById('tab_resource_sm').className='displayed';};
	elementById('tab_resource').onmouseout = function(){elementById('tab_resource_sm').className='hidden';};
    elementById('tid_blog').onmouseover = function(){elementById('tid_blog_sm').className='displayed';};
    elementById('tid_blog').onmouseout = function(){elementById('tid_blog_sm').className='hidden';};
}

function checkRadio(obj) {
	str = obj.name.split('_');
	elm = document.forms[0].elements;
	for (i=0; i<elm.length; i++) {
		if (elm[i].name.indexOf(str[0]) != -1 && elm[i].name != obj.name) {
			elm[i].checked = false;
		}
	}
}

function ufa(arg1, arg2){
	var a = new aJaX();
	a.modePOST('/ajax/fraudalert.php', 'arg1='+arg1+'&arg2='+arg2);
}

function activation(step){
	elementById('saving_status').className = 'displayed';
	elementById('continue_btn').style.display = 'none';
	var ajaxSave = new aJaX(activationStatus);
	var postContent = encodeForm('activation_'+step);
	ajaxSave.modePOST('/ajax/activation.php?type=' + step, postContent);		
	
	return false;
}

function forgotPassword_status(aJaX_obj, AJX_response){
	//alert(AJX_response);
	elementById('saving_status').className = 'hidden';
	elementById('continue_btn').style.display = '';
	AJX_response = eval(AJX_response);
	
	var type = AJX_response[0];	
	AJX_response.splice(0, 1);
	switch (type){
		case 'step1':
			switch(AJX_response[0]){
				case 'ERR':
					elementById('record_not_match').className = 'hidden';
					printErrors(AJX_response);
					elementById('record_not_match_locked').className = 'hidden';
					elementById('record_not_match').className = 'hidden';
					break;
				case 'OK':
					myInnerHTML('','ssn_last4_err');
					clearFieldError('ssn_last4');
					myInnerHTML('','birth_err');
					clearFieldError('birth_day');
					clearFieldError('birth_month');
					clearFieldError('birth_year');
					elementById('record_not_match').className = 'hidden';
					if (elementById('email')){
						myInnerHTML('','email_err');
						clearFieldError('email');
					}
					elementById('record_not_match_locked').className = 'hidden';
					elementById('record_not_match').className = 'hidden';
					window.location = '/myfico/forgotpassword.php';
				break;

				case 'NM':
					myInnerHTML('','ssn_last4_err');
					clearFieldError('ssn_last4');
					myInnerHTML('','birth_err');
					clearFieldError('birth_day');
					clearFieldError('birth_month');
					clearFieldError('birth_year');
					elementById('record_not_match').className = 'hidden';
					elementById('record_not_match_locked').className = 'displayed';
					if (elementById('email')){
						myInnerHTML('','email_err');
						clearFieldError('email');
					}
				break;

				case 'LM':
					myInnerHTML('','ssn_last4_err');
					clearFieldError('ssn_last4');
					myInnerHTML('','birth_err');
					clearFieldError('birth_day');
					clearFieldError('birth_month');
					clearFieldError('birth_year');
					elementById('record_not_match_locked').className = 'hidden';
					elementById('record_not_match').className = 'displayed';
					if (elementById('email')){
						myInnerHTML('','email_err');
						clearFieldError('email');
					}
				break;
			}
		break;
		
		case 'step2': 
			switch(AJX_response[0]){
				case 'ERR':
					printErrors(AJX_response);
					break;
				case 'OK':
					myInnerHTML('','new_password_err');
					clearFieldError('new_password');
					myInnerHTML('','confirm_password_err');
					clearFieldError('confirm_password');
					window.location = '/myfico/forgotpassword.php';
				break;
			}
		break;
	}
}

function forgotPassword(step){
	elementById('saving_status').className = 'displayed';
	elementById('continue_btn').style.display = 'none';
	var ajaxSave = new aJaX(forgotPassword_status);
	var postContent = encodeForm('forgotpassword_'+step);
	ajaxSave.modePOST('/ajax/forgotpassword.php?type=' + step, postContent);		
	
	return false;
}


function preForgotPassword_status(aJaX_obj, AJX_response){
	//alert(AJX_response);
	elementById('saving_status').className = 'hidden';
	elementById('continue_btn').style.display = '';
	AJX_response = eval(AJX_response);
	
	switch(AJX_response[0]){
		case 'ERR':
			elementById('record_not_match').className = 'hidden';
			printErrors(AJX_response);
			break;
		
		case 'OK':
			elementById('record_not_match').className = 'hidden';
			myInnerHTML('','email_err');
			clearFieldError('email');
			
			window.location = '/myfico/forgotpassword.php?confirm';
		break;

		case 'NM':
			elementById('record_not_match').className = 'displayed';
			myInnerHTML('','email_err');
			clearFieldError('email');
		break;
	}
}

function preForgotPassword(){
	elementById('saving_status').className = 'displayed';
	elementById('continue_btn').style.display = 'none';
	var ajaxSave = new aJaX(preForgotPassword_status);
	var postContent = encodeForm('forgotpassword');
	ajaxSave.modePOST('/ajax/preForgotPassword.php', postContent);		
	
	return false;
}

function printErrors(errorArray) {
	var arrayLength = errorArray.length;
	for(i=1; i<arrayLength; i+=2){
		myInnerHTML(errorArray[i],errorArray[i+1]+'_err');
		if(errorArray[i] == ''){
			try{
				clearFieldError(errorArray[i+1]);
			} catch(e){}
		} else {
			try{
				displayFieldError(errorArray[i+1]);
			} catch(e){}
		}
	}
}

//Thia function will toggle the email address box display for credit monitoring
function setclassforcm(form) {
    for (var i=0; i<form.add_user_to_cm.length; i++) {
        if (form.add_user_to_cm[i].checked)
	{
        	if(form.add_user_to_cm[i].value == 'skip')         	
        	{
        		if(form.form_action) {
        			form.form_action.value = 'skip';
        		}
        	}
        	else
        	{        		
        		var show_element = document.getElementById(form.add_user_to_cm[i].value);
            	if(show_element) {
            		show_element.style.display = 'block';
            	}
	            //form.email_secondary_unused[i].name = "email_secondary";
	            if(form.form_action) {
	            	form.form_action.value = '';
	            }
        	}
	}
        else 
	{
        	var hide_element = document.getElementById(form.add_user_to_cm[i].value);
        	if(hide_element) {
        		hide_element.style.display = 'none';
        	}
	}
    }
}
