function createSetup(){ 
	foreignTog();
	secTog();
	switchReg();
	moveRegToContact();
	regForTog();
}

function regForTog(){
	var regForRadio = $('input[name=regFor]:radio');
	var regForValue = '';
	var regForOther = $('#reg_for_other');
	var regForSelf = $('#reg_for_self');
	var submitBlock = $('#submit');
	var formNameField = $('#create_form_name');
	for (var i = 0;i<regForRadio.length;i++){
		if (regForRadio[i].checked){
			regForValue = regForRadio[i].value;
			break;
		}
	}
	switch (regForValue){
		case 'other':
			regForOther.show();
			regForSelf.hide();
			submitBlock.show();
			formNameField[0].value = 'sugCreateUpdate';
		break;
		case 'yourself':
			regForOther.hide();
			regForSelf.show();
			submitBlock.show();
			formNameField[0].value = 'registryCreateUpdate';
		break;
		default:
		regForOther.hide();
		regForSelf.hide();
		submitBlock.hide();
		formNameField[0].value = '';
		break;
	}
}

function foreignTog(){
	var foreignInput,prependInput;
	var foreignA = $('#foreign_a');
	var citizenB = $('#foreign_b');
	var foreignValue = -1
	foreignInput = $('input[name=foreignNat]:radio');
	// loop the radio button stop when value found
	for (var i = 0;i<foreignInput.length;i++){
		if (foreignInput[i].checked){
			foreignValue = foreignInput[i].value;
		}
	}
	switch(foreignValue){
		case '0':
		foreignA.show();
		citizenB.hide();
		break;
		case '1':
		citizenB.show();
		break;
	}
} 


function secTog(){
	var sec1 = $('input[name=secFinra1]:radio');
	var sec2 = $('input[name=secFinra2]:radio');
	var secA = $('#sec_a');
	var showSEC = false;
	for (var i = 0;i<sec1.length;i++){
		if (sec1[i].checked && sec1[i].value == "yes"){
			showSEC = true;
			break;
		}
	}
	for (var i = 0;i<sec2.length;i++){
		if (sec2[i].checked && sec2[i].value == "yes"){
			showSEC = true;
			break;
		}
	}
	if (showSEC){
		secA.show();
	}
	else {
		secA.hide();
	}
}


function switchReg(){
	// get the custodial div, and the other registrant div
	var reg_input = $('input[name=regTypeID]:radio');
	var cust_div = $('.cust');
	var other_reg_div = $('.oreg');
	var reg_type_id = '';
	var label_switch_p = $('.label_switch_p');
	var label_switch_s = $('.label_switch_s');
	var label_p = '';
	var label_s = '';
	var i,ii,iii = 0;
	// loop the registration types find selected value
	for (i = 0;i<reg_input.length;i++){
		if (reg_input[i].checked){
			reg_type_id = reg_input[i].value;
			break;
		}
	}
	// switch on reg type
	switch(reg_type_id){
		case '1':
			cust_div.hide();
			other_reg_div.hide();
			label_p = "Registrant's";
			label_s = "Registrant";
		break;
		case '3':
			cust_div.hide();
			other_reg_div.show();
			label_p = "First Registrant's";
			label_s = "First Registrant";
		break;
		case '4':
			cust_div.show();
			other_reg_div.hide();
			label_p = "Custodian's";
			label_s = "Custodian";
		break;
	}
	// switch all the plural labels
	for (ii = 0;ii<label_switch_p.length;ii++){
		// replace the label 
		label_switch_p[ii].replaceChild(document.createTextNode(label_p),label_switch_p[ii].firstChild);
	}
	// switch all the single labels 
	for (iii = 0;iii<label_switch_s.length;iii++){
		// replace the label 
		label_switch_s[iii].replaceChild(document.createTextNode(label_s),label_switch_s[iii].firstChild);
	}
	
}	


function inputMover(id,this_num,chars_move){
	var this_input = $('#'+id+this_num);
	var next_num = this_num + 1;
	var next_input = $('#'+id+next_num);
	if (this_input[0].value.length == chars_move){
		next_input[0].value = '';
		next_input[0].focus();
	}
}

// make an xml http request object
function makeXMLRequest(){
	// mozilla, safari
	if (window.XMLHttpRequest) {
		httpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		httpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		httpReq = 0;
	}
	return httpReq;
}

// function to resolve a zip code into a city state
function resolveZip(zipField,cityID,stateID) {
	var mode = 'prod';
	var cityField = document.getElementById(cityID);
	var stateSelect = document.getElementById(stateID);
	var httpReq = makeXMLRequest();
	var requestURL = '';
	if (mode == 'prod') {
		requestURL = 'http://10.10.10.12/sites/private';
	}
	else {
		requestURL = 'https://private.directinvesting.com';
	}
	if (httpReq != 0 && zipField.value.length == 5) {
		httpReq.open('POST',requestURL + '/index.cfm?x=ajax.zip&zip=' + zipField.value,true);
		httpReq.onreadystatechange = function() {
			if (httpReq.readyState ==4) {
				// compile json response into javascript 
				var cityState = eval('(' + httpReq.responseText + ')');
				cityField.value = cityState[0];
				stateSelect.value = cityState[1];
			}
		}
		httpReq.send('');
	}
}

function moveRegToContact(){
	// get the checkbox, see if its checked
	var contact_reg_input = $("input[name=contactIsReg]");
	var contact_name_input = $("input[name=contactName]"); 
	var contact_phone_input = $("input[name=contactPhone]");
	if (contact_reg_input[0].checked){
		// clear and disable both inputs 
		contact_name_input[0].value = '';
		contact_phone_input[0].value = '';
		contact_name_input[0].disabled = true;
		contact_phone_input[0].disabled = true;
	}
	else {
		contact_name_input[0].disabled = false;
		contact_phone_input[0].disabled = false;	
	}
	
}


function giftCardFill(){
	// get the from and message field 
	var from_field = $("textarea[name=giftFrom]");
	var message_field = $("textarea[name=giftMessage]");
	var from_fill = $("#js_gift_from");
	var message_fill = $("#js_gift_message");
	var from_error = $("#gift_from_error");
	var message_error = $("#gift_message_error");
	// if from is too long inform, if not hide 
	if (from_field[0].value.length > 50){
		from_error.show();	
	}
	else {
		from_error.hide();
	}
	if (from_field[0].value.length == 0){
		from_fill[0].replaceChild(document.createTextNode('Your Name Here'),from_fill[0].firstChild);
	}
	else {
		from_fill[0].replaceChild(document.createTextNode(from_field[0].value.substring(0,50)),from_fill[0].firstChild);
	}
	// if message is too long inform, if not hide 
	if (message_field[0].value.length > 165){
		message_error.show();
	}
	else {
		message_error.hide();
	}
	if (message_field[0].value.length == 0){
		message_fill[0].replaceChild(document.createTextNode('Your Message Here'),message_fill[0].firstChild);
	}
	else {
		message_fill[0].replaceChild(document.createTextNode(message_field[0].value.substring(0,165)),message_fill[0].firstChild);
	}
	
}


function giftOrderRoute(action){
	var action_field = $("input[name=action]");
	var order_form = $("#f_gift_order");
	action_field[0].value = action;
	order_form[0].submit();
}


function removeCompany(companyID){
	// fill the company id 
	var removeCompanyID = $('#remove_company_id');
	var removeForm = $('#remove_form');
	removeCompanyID[0].value = companyID;
	removeForm[0].submit();
}

// this function will move all the fields from the registrant info into the stock registration info 
function moveRegInfo() {
	$("input[name=fname]")[0].value = $("input[name=rFname]")[0].value;
	$("input[name=mname]")[0].value = '';
	$("input[name=lname]")[0].value = $("input[name=rLname]")[0].value;
	$("input[name=address]")[0].value = $("input[name=rAddress]")[0].value;
	$("input[name=city]")[0].value = $("input[name=rCity]")[0].value;
	$("select[name=state]")[0].value = $("select[name=rState]")[0].value;
	$("input[name=zip]")[0].value = $("input[name=rZip]")[0].value;
	$("select[name=country]")[0].value = $("select[name=rCountry]")[0].value;
	$("input[name=email1]")[0].value = $("input[name=rEmail1]")[0].value;
	$("input[name=pnumber1]")[0].value = $("input[name=rPnumber1]")[0].value;
}


function viewRegStock(r,c){
	var company_id_checks = $("input[name=companyIDs]");
	var company_ids = '';
	var mode = 'prod'
	var root = '';
	var userIDInput = $("input[name=userID]");
	// make a list of all checked company ids 
	for (var i = 0;i < company_id_checks.length;i++){
		if (company_id_checks[i].checked){
			company_ids = company_ids + company_id_checks[i].value + ',';
		}
	}
	// send the user to the page to view prospectus
	if (mode == 'dev'){
		root = 'http://10.10.10.12/sites/giftsofstock_com_new/?page=stock_info&r=' + r + '&companyID=' + c + '&userID=' + userIDInput[0].value + '&companyIDs=';
	}
	else {
		root = 'http://www.giftsofstock.com/?page=stock_info&r=' + r + '&companyID=' + c + '&userID=' + userIDInput[0].value + '&companyIDs=';
	}
	window.location = root + company_ids;
}



function moveAdder(){
	$("input[name=symbol]")[0].value = '';
	$("input[name=symbol]")[0].focus();
}

function cleanAdder() {
	$("input[name=symbol]")[0].value = '';
}

function removeFromCart(companyID) {
	$("input[name=rCompanyID]")[0].value = companyID;
	$("#cart_remove").submit();
}

