/*
		iAmMap object holds the key/value pairs iAm/an Array object holds the list of IWantTo entries.
		Any number of iWanto entries can be added to the object 
*/

var iAmMap =  { 
	
// US Citizen holds an array[list] of I Want To entries.
    "USCitizen" 	: 	[{ "iWantTo" : "To get a certificate of citizenship.",
                     	   "url"     : "/us-citizenship/Certificate-of-Citizenship-Form-N-600.jsp" },
			 
			 { "iWantTo" : "To help my child become a U.S. citizen or get proof of U.S. citizenship.",
                     	   "url"     : "/us-citizenship/Certificate-of-Citizenship-Form-N-600.jsp" },

			 { "iWantTo" : "To replace a lost, stolen or destroyed naturalization certificate/certificate of citizenship.",
                     	   "url"     : "/us-citizenship/Naturalization-Certificate-Citizenship-Form-N-565.jsp" }],

    "Fiance_k1"	:	[{ "iWantTo" : "To correct an error on my permanent resident card (green card).",
                           "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp" },

             { "iWantTo" : "To get a re-entry permit.",
                           "url"     : "/uscis-forms/Form-I-131-Application-Travel-Document.jsp" },

	{ "iWantTo" : "To renew my Permanent Resident Card (green card).",
                           "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp" },


             { "iWantTo" : "To replace my lost/damaged permanent resident card (green card).",
                           "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp" }],


"conditionresident" : [{ "iWantTo" : "To correct an error on my permanent resident card (green card).",
                           "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp"},

{ "iWantTo" : "To replace my lost/damaged permanent resident card (green card).",
                           "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp"}],

"PermanentResident_GreencardHolder"  :

			[{ "iWantTo" : "To apply for U.S. Citizenship.",
                           "url"     : "/us-citizenship/U-S-Citizenship-application-Form-N-400.jsp" },

      
             { "iWantTo" : "To find out about wartime naturalization.",
                           "url"     : "/us-citizenship/U-S-Citizenship-application-Form-N-400.jsp" },

             { "iWantTo" : "To replace a lost, stolen or destroyed naturalization certificate/certificate of citizenship.",
                           "url"     : "/us-citizenship/Naturalization-Certificate-Citizenship-Form-N-565.jsp" }],


    "imigrantforms" :

		       [{ "iWantTo" : "Form I-90, Application to Replace or Renew Green card.",
                          "url"     : "/greencard/renewal-green-card-Form-I-90-replace.jsp" },

			{ "iWantTo" : "Form N-400, Application for Citizenship (Naturalization)",
                          "url"     : "/us-citizenship/U-S-Citizenship-application-Form-N-400.jsp" },

			{ "iWantTo" : "Form N-565, Application for Replacement Citizenship Document",
                          "url"     : "/us-citizenship/Naturalization-Certificate-Citizenship-Form-N-565.jsp" },

			{ "iWantTo" : "Form N-600, Application for Certificate of Citizenship",
                          "url"     : "/us-citizenship/Certificate-of-Citizenship-Form-N-600.jsp" },
                        { "iWantTo" : "I-131, Application for Travel Document",
                          "url"     : "/uscis-forms/Form-I-131-Application-Travel-Document.jsp" }],


"refugeeasylee"    	     : [{ "iWantTo" : "To get a refugee travel document.",
                          "url"     : "/uscis-forms/Form-I-131-Application-Travel-Document.jsp" }]

}

var iWanToSize = 0;	// used for refreshing the iWantTo list
var noSelected = 0;	// for providing default select box functionality
var defaultText = "Select One";		// default select box text
var defaultFrmAction = "none";		// initial form action value before selecting iWantTo

// Shows the iAm List
function showIAmList(){	
	noSelected = noSelected + 1;
	if(document.getElementById('iWantToList') != null || document.getElementById('iWantToList') != undefined){
		if(document.getElementById('iWantToList').style.display == "block"){
			hideTheBlock(document.getElementById('iWantToList'));
		}
	}
	if(noSelected == 1){
		showTheBlock(document.getElementById('iAmList'));
		hideTheBlock(document.getElementById('iWantToBlk'));
		//document.getElementById('iAm').style.borderBottom = "solid #fff 2px";
		document.getElementById('iAmList').style.marginTop = '-8px'

	}else{
		showTheBlock(document.getElementById('iWantToBlk'));
		hideTheBlock(document.getElementById('iAmList'));
		noSelected = 0;
	}
}

//gets iWantTo list according to user selection
function getIWantToList(SEL_iAmObj){
	var SELECTED_iAmObj = document.getElementById(SEL_iAmObj);
	noSelected = 0;
	refreshIWanToList();
	refreshErrorMsg();
	var iAmObj = document.getElementById('iAm');
	var iAm = SELECTED_iAmObj.innerHTML;	
	iAm = wrapText(iAm);
	iAmObj.innerHTML = iAm;
	iAm_Current = iAm;
	for(var i = 0; i < iAmMap[SELECTED_iAmObj.id].length; i++){
		var nextIwantTo = iAmMap[SELECTED_iAmObj.id][i].iWantTo;
		var nextURL = iAmMap[SELECTED_iAmObj.id][i].url;
		appendIWantToValues(nextIwantTo, nextURL, "list"+i);
		iWanToSize++;
	}	
	hideTheBlock(document.getElementById('iAmList'));
	showTheBlock(document.getElementById('iWantToBlk'));
}

// appends all the iWantTo values to the Select Box
function appendIWantToValues(iWantTo, url, uniqListId){
		
	var iWantToULObj = document.getElementById('selected-menu');
	// if List contains more than five elements apply scroll
	if(parseInt(uniqListId.substring(4,5)) >=5)
	{
		iWantToULObj.setAttribute("class","second-menu-scroll") || iWantToULObj.setAttribute("className","second-menu-scroll");
	}
	// append div Tag
	var dynaDIV = document.createElement("div");
	dynaDIV.id = uniqListId;
	dynaDIV.setAttribute("class","second-menu-list") || dynaDIV.setAttribute("className","second-menu-list");
	dynaDIV.onmouseover = function(){dynaDIV.style.background="#EBEBEB";}
	dynaDIV.onmouseout = function(){dynaDIV.style.background="";}
	iWantToULObj.appendChild(dynaDIV);
   	// append UL Tag
   	var dynaUL = document.createElement("ul.menulist");
	dynaDIV.appendChild(dynaUL);
    	// append LI Tag
    	var dynaLI = document.createElement("li.options");   	 
    	dynaUL.appendChild(dynaLI); 
	// append A Tag
	var hrefObj = document.createElement("a");
	hrefObj.href = "javascript:getIWantToURL('"+iWantTo+"','"+url+"')";
	hrefObj.innerHTML = iWantTo;
 	dynaLI.appendChild(hrefObj);
}

// refreshes previously displayed iWantTo List
function refreshIWanToList(){
	var iWantToListObj = document.getElementById('selected-menu');
	if(iWanToSize > 0){
		for(var i = 0; i < iWanToSize; i++){
			var childTag = document.getElementById('list'+i);
			iWantToListObj.removeChild(childTag);
		}
	}
	iWantToListObj.setAttribute("class","") || iWantToListObj.setAttribute("className","");
	iWanToSize = 0;
	defaultFrmAction = "none";
	document.getElementById('iWantTo').innerHTML = defaultText;
}

// displays the iWantTo List
function showIWantToList(){	
	noSelected = noSelected + 1;
	if(noSelected == 1){
		showTheBlock(document.getElementById('iWantToList'));
		//document.getElementById('iWantTo').style.borderBottom = "solid #fff 2px";
		document.getElementById('iWantToList').style.marginTop = '-8px';		
	}else{
		hideTheBlock(document.getElementById('iWantToList'));
		noSelected = 0;
	}
}

//sets the URL for selected iWantTo and sets the selected text into iWantTo
function getIWantToURL(SELECTED_iWantTo, selUrl){
	noSelected = 0;
	var iWantToObj = document.getElementById('iWantTo');
	iWantToObj.innerHTML = wrapText(SELECTED_iWantTo);
	iWantTo_Current = SELECTED_iWantTo;
	hideTheBlock(document.getElementById('iWantToList'));
	defaultFrmAction = selUrl;
	
}

// If select One is selected form action is cleared
function selOne(){
	document.getElementById('iWantTo').innerHTML = defaultText;
	hideTheBlock(document.getElementById('iWantToList'));
	defaultFrmAction = "none";
	noSelected = 0;
}

// Redirects page to the corresponding URL
function goToPage(){

	refreshErrorMsg();
	// creates form element with the selected URL as action
	if(defaultFrmAction != "none")
	{		
		var where_To_Div = document.getElementById('where-to-start');
		var where_To_form = document.createElement("form");
		where_To_form.id = "where_To_form";
		where_To_form.action = defaultFrmAction;
		where_To_form.method = "post";
		where_To_Div.appendChild(where_To_form);
		where_To_form.submit();
	}else{	
		// Error Message block if select box is not selected	
		var Where_To_Begin_Txt = document.getElementById('Where_To_Begin_Txt');
		var dynaP = document.createElement("p");
		dynaP.id = "ErrorMsg";
		dynaP.setAttribute("class","page-errors") || dynaP.setAttribute("className","page-errors");
		dynaP.innerHTML = "Please select your option to proceed";
    		Where_To_Begin_Txt.appendChild(dynaP);
	}
}

// refreshes the error message and form element
function refreshErrorMsg(){
	if(document.getElementById('ErrorMsg') != null || document.getElementById('ErrorMsg') != undefined){
		var Where_To_Begin_Txt = document.getElementById('Where_To_Begin_Txt');
		Where_To_Begin_Txt.removeChild(document.getElementById('ErrorMsg'));
	}
	if(document.getElementById('where_To_form') != null || document.getElementById('where_To_form') != undefined){
		var where_To_Div = document.getElementById('where-to-start');
		where_To_Div.removeChild(document.getElementById('where_To_form'));
	}
}
// common functionalities

function wrapText(text){
	// Wrap the selected text if the size exceeds the option box
	if(text.length > 25){
		text = text.substring(0,23)+"..";
	}
	return text;
}

function showTheBlock(htmlObj){
	htmlObj.style.display = 'block';
}

function hideTheBlock(htmlObj){
	htmlObj.style.display = 'none';
}

// Event Driven functionalities
// closing dropdown by click out of it
// Register event 'click' and associated call back.

  
function respondToClick(event) { 
  var a = Event.element(event);
  if(a != '' || a != null || a != undefined){
	if(a.id != ''){
		if(a.id == 'iAmBlock' || a.id == 'iAmList' || a.id == 'iAm' || a.id == 'iWantToBlk' || a.id == 'iWantTo' || a.id == 'iWantToList'){
			return false;
		}
	}
  }
	noSelected = 0;
	hideTheBlock(document.getElementById('iAmList'));
	hideTheBlock(document.getElementById('iWantToList'));
	showTheBlock(document.getElementById('iWantToBlk'));
}

Event.observe(document, 'click', respondToClick);








