/*(c) 2006 - 2008 AllenPort Co. All Rights Reserved.
All versions of this code, including the source and executable versions, 
constitute the intellectual property of AllenPort Co., which expressly reserves 
any and all U.S. and foreign rights and benefits to the code under copyright, 
trade secret and any other intellectual property law or international treaty 
whatsoever. Use of the code is subject to the terms and conditions of a separate 
written license agreement, and the code shall not be reproduced, modified, distributed 
or otherwise used in any form or manner whatsoever without obtaining the prior written 
permission of AllenPort Co. Any unauthorized reproduction or distribution of the code, 
or any portion of it, may result in civil and criminal penalties and be prosecuted 
to the fullest extent of the law.*/

var xmlHttp;

function get_current_page_name()
{
	return document.getElementById("page_id").value;
}


function iBack()
{
	$("#target").val						("page.php?id=acc_register");
	
	document.business_registration.submit	();
}

function set_default_screen_name(){
	var name = document.getElementById("user_name").value;
	document.getElementById("screen_name").value = name.substring(0,15);
}

function checkEmailAvailable(str)
{
	xmlHttp = GetXmlHttpObject();
	
	var url = "core/check_email_available.php";
	params 	= "email=" + str;
	xmlHttp.open("POST", url, true);
	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	blockUI	();
	
	xmlHttp.onreadystatechange=function()
	{
	    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	    {
			
			unblockUI();
	        
	        var status 	= getXmlTag(xmlHttp.responseXML, "status");
	        var message = getXmlTag(xmlHttp.responseXML, "message");
	
	        if(status == interface_ok)
	        {
				alert("You have entered an email address that is already in use. Please enter a different email address if you want to create a new account. If you have forgotten the password for an existing account, click the \"Forgot your password?\" link on the login window.");
				document.getElementById("login_email").select();
				document.getElementById("login_email").focus();
	        }
	        else 
	        	if(status == interface_user_does_not_exist_error)
	        	{
	        		//Show bill_info page. Updates readonly fields on bill_info page with correct data.
	                on_user_info_page_submitted();
	            }
	            else
	            {
					if(status == interface_network_error)
						alert("There has been a temporary interruption in the AllenPort service. We apologize for this inconvenience. Please call AllenPort Support, 609 951-3700, for assistance.\n\nThank you for your patience.");					  	
					else
	                    alert("Error: " + message + "\nCode: " + status);
	            }        
	    }
	}
	xmlHttp.send(params);
}

//For error types list see validation_errors_messages.js
function show_error_message(field_id, error_type)
{
	// set the message on top content
	$('#ValidationErrorCaption').html	(generic_error_message);
	
	var error_object_id = field_id + "_error";
	var object = document.getElementById(error_object_id);
	if(object != null)
	{
		$('#'+error_object_id).find('p').html				(validation_messages[field_id][error_type]);
		
		$('#'+error_object_id).css							("display", "inline");
		$('#'+get_current_page_name() + '_error_hint').css	("display", "inline");

	   	try
	   	{  			
	   		document.getElementById(field_id).select();
	   		document.getElementById(field_id).focus();
	   	}
	   	catch(e)
	   	{
	   	}
	}
}

//Clears all fields on the registration form by clicking on Cancel button.
function clear_form(page_name)
{
	
	hide_errors(page_name);
	
	if(page_name == 'user_info')
	{
		
		document.getElementById("company_name").value = "";
		document.getElementById("company_phone").value = "";
		
		//Administrator's information
		document.getElementById("login_email").value = "";  
		document.getElementById("confirm_email").value = ""; 
		document.getElementById("user_name").value = "";           				         
		document.getElementById("screen_name").value = "";                 
		    
		//Security information
		document.getElementById("user_password").value = "";	
		document.getElementById("confirm_password").value = "";	
		document.getElementById("security_question").value = "";     
		document.getElementById("security_answer").value = ""; 
		   
		//Billing address		 
		document.getElementById("address_1").value = "";			
		document.getElementById("address_2").value = "";			
		document.getElementById("city").value = "";				  		
		document.getElementById("zip").value = "";	
		document.getElementById("country").value = "";			
	}
	if(page_name == 'bill_info') 
	{ 
		//Credit card information
		document.getElementById("card_type").value = "";
		document.getElementById("card_name").value = "";
		document.getElementById("card_number").value = "";
		document.getElementById("card_security_code").value = "";
		document.getElementById("expiration_month").value = "";	
		document.getElementById("expiration_year").value = "";	  
		document.getElementById("turing").value = "";
		
		return;
	}
	
	
	if(page_name == 'close_account') 
	{
		document.getElementById("user_password").value = "";
		document.getElementById("reason").value = "";
		return;
	}
	
	if (page_name == 'update_billing_info')	
	{
		//Billing address		 
		document.getElementById("address_1").value = "";			
		document.getElementById("address_2").value = "";			
		document.getElementById("city").value = "";				  		
		document.getElementById("zip").value = "";	
		document.getElementById("country").value = "";				
		
		//Credit card information
		document.getElementById("card_type").value = "";
		document.getElementById("card_name").value = "";
		document.getElementById("card_number").value = "";
		document.getElementById("card_security_code").value = "";
		document.getElementById("expiration_month").value = "";	
		document.getElementById("expiration_year").value = "";	
		return;
	}
}

//Must be called before [encrypt]
function get_registration_data(page)
{	
	var admin_info 						= init_admin_info_array();
	
	admin_info.login_email 				= document.getElementById("login_email").value;  		//submit_email              
    admin_info.screen_name   			= document.getElementById("screen_name").value;         //submit_s_name             
    admin_info.user_name  				= document.getElementById("user_name").value;                
    admin_info.company_phone      		= document.getElementById("company_phone").value;		//submit_a_phone         
    admin_info.security_question   	 	= document.getElementById("security_question").value;   //submit_sec_question                
    admin_info.company_name				= document.getElementById("company_name").value;		//submit_c_name
    
    admin_info.seats_number				= document.getElementById("seats_number").value;
    admin_info.base_storage				= document.getElementById("base_storage").value;
    
    var unit_size 						= parseInt(document.getElementById("unit_size").value);
    var inc_storage 					= parseInt(document.getElementById("incremental_storage").value);
    
	//Conver inc. storage from gbs to units.
    admin_info.incremental_storage		= inc_storage/unit_size;
    admin_info.ref_code					= document.getElementById("ref_code").value;
	
	admin_info.zip 						= document.getElementById("zip").value;	
	admin_info.address_1 				= document.getElementById("address_1").value;			//submit_adr1
	admin_info.address_2 				= document.getElementById("address_2").value;			//submit_adr2
	admin_info.city 					= document.getElementById("city").value;				//submit_city
	admin_info.country 					= document.getElementById("country").value;				//submit_country


	if(page == 'bill_info')
	{
	    admin_info.expiration_month 		= document.getElementById("expiration_month")? document.getElementById("expiration_month").value: '';	 //submit_month
	    admin_info.expiration_year 			= document.getElementById("expiration_year")? document.getElementById("expiration_year").value: '';		 //submit_year
	    admin_info.card_number				= document.getElementById("card_number")?document.getElementById("card_number").value: '';	  			 //submit_card_num
		admin_info.card_security_code 		= document.getElementById("card_security_code")?document.getElementById("card_security_code").value: ''; //submit_cvv2
		admin_info.card_name 				= document.getElementById("card_name")?document.getElementById("card_name").value: '';					 //submit_name_card
		admin_info.card_type				= document.getElementById("card_type")?document.getElementById("card_type").value: '';					 //submit_card_type
		admin_info.monthly_billing 			= document.getElementById("monthly_billing").value;														 //submit_amount
		
		admin_info.turing 					= document.getElementById("turing").value;				
	}
	return admin_info;	
}

function submit_registration_first_step()
{
	if(!validate_user_info_page())
		return;

	// check if account specifications are calculated
	if (document.getElementById("monthly_amount").value == "") {
		alert	('Please enter the Account Specification information and click the Calculate button to see the monthly billing amount');
		return;
	}
	
	// check if any account specification was changed but was not recalculated
	if (accountSpecificationsNeedRecalculate) {
		alert	('Please use the Calculate button to display the monthly billing amount');
		return;
	}

	document.getElementById("incremental_storage").value 	= spinCtrlIncrementalStorage.GetCurrentValue();

    var login_email 										= document.getElementById("login_email").value;
    checkEmailAvailable										(login_email);
}


//Maybe must calculate this values if GetQuote button is not clicked.
function on_user_info_page_submitted()
{
    document.business_registration.submit();    
}


function submit_registration_second_step()
{	
	//In case for trial/demo or internal account credit card info doesn't need.
	if(document.getElementById("card_type") != null)
		if(!validate_bill_info_page())
			return;
	
	// block UI from here because there is time consuming encryption task
	blockUI	();

	var admin_info = get_registration_data('bill_info');
	
	var pw = document.getElementById("user_password").value;
	var sa = document.getElementById("security_answer").value;
	
	encrypt	(pw, sa, admin_info);	
}

function update_captcha()
{
	document.getElementById('turing').value = '';
	document.getElementById('captcha').src = document.getElementById('captcha').src + '?' + (new Date()).getMilliseconds();
}

function on_back_button_clicked()
{
	update_captcha();
	iBack();
}

// Hide error functionality
function hide_errors(page)
{
	if (page ==	'user_info')
	{
		$("#user_info_error_hint").css		("display","none");
		$("#company_name_error").css		("display","none");
		$("#company_phone_error").css		("display","none");
		$("#login_email_error").css			("display","none");
		$("#confirm_email_error").css		("display","none");
		$("#user_name_error").css			("display","none");
		$("#screen_name_error").css			("display","none");
		$("#user_password_error").css		("display","none");
		$("#confirm_password_error").css	("display","none");
		$("#security_question_error").css	("display","none");
		$("#security_answer_error").css		("display","none");
		$("#address_1_error").css			("display","none");
		$("#city_error").css				("display","none");
		$("#zip_error").css					("display","none");
		$("#seats_number_error").css		("display","none");
		$("#country_error").css				("display","none");
		
		return;
	}
	
	if (page == 'bill_info')
	{
		$("#bill_info_error_hint").css		("display","none");
		$("#card_type_error").css			("display","none");
		$("#card_name_error").css			("display","none");
		$("#card_number_error").css			("display","none");
		$("#card_security_code_error").css	("display","none");
		$("#expiration_month_error").css	("display","none");
		$("#expiration_year_error").css		("display","none");
		$("#captcha_error").css				("display","none");	
		
		return;
	}
	
	if (page == 'activate_account')
	{
		$("#activate_account_error_hint").css	("display","none");
	
		$("#address_1_error").css				("display","none");
		$("#city_error").css					("display","none");
		$("#zip_error").css						("display","none");
		$("#country_error").css					("display","none");
	
		$("#card_type_error").css				("display","none");
		$("#card_name_error").css				("display","none");
		$("#card_number_error").css				("display","none");
		$("#card_security_code_error").css		("display","none");
		$("#expiration_month_error").css		("display","none");
		$("#expiration_year_error").css			("display","none");
		
		$("#seats_number_error").css			("display","none");
		
		return;
	}
	
	if(page == 'close_account') 
	{
		$("#close_account_error_hint").css		("display","none");
		$("#user_password_error").css			("display","none");
		return;
	}	
	
	
	if (page == 'update_billing_info')
	{
		$("#update_billing_info_error_hint").css	("display","none");
	
		$("#address_1_error").css					("display","none");
		$("#city_error").css						("display","none");
		$("#zip_error").css							("display","none");
		$("#country_error").css						("display","none");
	
		$("#card_type_error").css					("display","none");
		$("#card_name_error").css					("display","none");
		$("#card_number_error").css					("display","none");
		$("#card_security_code_error").css			("display","none");
		$("#expiration_month_error").css			("display","none");
		$("#expiration_year_error").css				("display","none");
		
		return;
	}
}


////////////////////////////////////////////////
// Validation
////////////////////////////////////////////////

function validate_user_info_page()
{
	hide_errors('user_info');
	
	var admin_info = get_registration_data('user_info');
	
	if(admin_info.company_name == '')
	{	
	   show_error_message('company_name', 'empty_error');	   
	   return false;
	}
	if(admin_info.company_phone == '')
	{	
	   show_error_message('company_phone', 'empty_error');	   
	   return false;
	}
	if(!checkPhone(admin_info.company_phone))
	{
	   show_error_message('company_phone', 'incorrect_error');	   
	   return false;
	}
	if(admin_info.login_email == '')
	{	
	   show_error_message('login_email', 'empty_error');	   
	   return false;
	}
	if(!checkMailId(admin_info.login_email))
	{
     	show_error_message('login_email', 'incorrect_error');	   
	   	return false;
	}
	var confirm_email = document.getElementById("confirm_email").value;
	if(confirm_email == '')
	{	
	   show_error_message('confirm_email', 'empty_error');	   
	   return false;
	}
	if(!(admin_info.login_email == confirm_email))
	{
	  	show_error_message('confirm_email', 'not_match_error');	   
	  	return false;
	}
	if(admin_info.user_name == '')
	{	
	   show_error_message('user_name', 'empty_error');	   
	   return false;
	}
	if(admin_info.screen_name == '')
	{	
	   show_error_message('screen_name', 'empty_error');	   
	   return false;
	}
	var user_password = document.getElementById("user_password").value;
	if(user_password == '')
	{	
	   show_error_message('user_password', 'empty_error');	   
	   return false;
	}
	if(!(checkpasswd(user_password)))
	{ 
	   show_error_message('user_password', 'incorrect_error');	   
	   return false;
	}
	var confirm_password = document.getElementById("confirm_password").value;
	if(confirm_password == '')
	{	
	   show_error_message('confirm_password', 'empty_error');	   
	   return false;
	}
	if(!(user_password == confirm_password))
	{
		show_error_message('confirm_password', 'not_match_error');	   
	   	return false;
	}
	var security_question = document.getElementById("security_question").value;
	if(security_question == '')
	{	
	   show_error_message('security_question', 'empty_error');	   
	   return false;
	}
	var security_answer = document.getElementById("security_answer").value;
	if(security_answer == '')
	{	
	   show_error_message('security_answer', 'empty_error');	   
	   return false;
	}
	if(!is_ascii(security_answer))
	{
	   show_error_message('security_answer', 'incorrect_error');	   
	   return false;
	}
	if(admin_info.address_1 == '')
	{	
	   show_error_message('address_1', 'empty_error');	   
	   return false;
	}
	if(admin_info.city == '')
	{	
	   show_error_message('city', 'empty_error');	   
	   return false;
	}
	if(!check_zip(admin_info.zip))
	   return false;
	
	if(admin_info.country == '')
	{	
	   show_error_message('country', 'empty_error');	   
	   return false;
	}
	//TODO: lost focus and incorrect
	if(admin_info.seats_number == '')
	{	
	   show_error_message('seats_number', 'empty_error');	   
	   return false;
	}
	
	return true;
}

function validate_bill_info_page()
{
	hide_errors('bill_info');
	
	var admin_info = get_registration_data('bill_info');
	
	if(admin_info.card_type == '')
	{	
	   show_error_message('card_type', 'empty_error');	   
	   return false;
	}
	if(admin_info.card_name == '')
	{	
	   show_error_message('card_name', 'empty_error');	   
	   return false;
	}
	if(admin_info.card_number == '')
	{	
	   show_error_message('card_number', 'empty_error');	   
	   return false;
	}
	
	if (!checkCard(admin_info.card_number))
	{
		show_error_message('card_number', 'incorrect_error');
		return false;
	}
	
	if(admin_info.card_security_code == '')
	{	
	   show_error_message('card_security_code', 'empty_error');	   
	   return false;
	}
	if(admin_info.expiration_month == '')
	{	
	   show_error_message('expiration_month', 'empty_error');	   
	   return false;
	}
	if(admin_info.expiration_year == '')
	{	
	   show_error_message('expiration_year', 'empty_error');	   
	   return false;
	}
	
	return true;
}



////////////////////////////////////////////////
// END Validation
////////////////////////////////////////////////


function check_zip(zip)
{
	// remove spaces
	var value = zip.split(' ').join('');
	
	if (value == '') 
		show_error_message('zip', 'empty_error');
	else if (!checknumeric (value))
		show_error_message('zip', 'numeric_error');
	else if (value.length > 9) 	
		show_error_message('zip', 'incorrect_max_length');
	else // everything is ok 
		return true;
	
	return false;
}



function check_seats_number(page)
{
	hide_errors(page);
	
	var seats_number = document.getElementById("seats_number").value;
	var min_seats_value = 2;
	
	if(seats_number == '')
	{	
	   show_error_message('seats_number', 'empty_error');	   
	   return false;
	}
	
	if(!checknumeric(seats_number) || (seats_number < min_seats_value))
	{
		show_error_message('seats_number', 'incorrect_error');	   
	   	return false;
	}
	return true;
}

function on_seats_number_lost_focus()
{	
	if(!check_seats_number())
	{
		show_error_message('seats_number', 'incorrect_lost_focus_error');	   
	}
}

function get_quote(page)
{
	if(check_seats_number(page))
	{
		var incremental_storage = spinCtrlIncrementalStorage.GetCurrentValue();
		if(incremental_storage != '' && !checknumeric(incremental_storage))
		{
			show_error_message('incremental_storage', 'incorrect_error');	   
			return;
		}
		
		// process the calculation of the account parameter
		
	}
}

function checkMailId(mailids)
{
   var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(mailids))
		return true;
	else
		return false;
}

function checkURL(url)
{
	if(url.length == 0)
		return true;
    var filter = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.|[\w]+){1}([\w]+)(.[\w]+){1,2}$/;
	if (filter.test(url))
		return true;
	else
		return false;
}

function is_ascii(str)
{
	for(var j=0; j<str.length; j++)
	{
		var alphaa = str.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(hh > 127 || hh < 32)
		{
			return false;
		}
	}

	if(str.length == 0)
		return true;

	str = str.replace(/ /g, '');
	if(str.length == 0)
		return false;
	
	return true;
}

function checkpasswd(password)
{
	var numaric = password;
	var count_alpha = 0;
	var count_numeric = 0;

	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(hh >= 47 && hh <= 57) 
		{
			count_numeric++;
		}
		else if((hh >= 65 && hh <= 90) || (hh >= 97 && hh <= 122)){
			count_alpha++;
		}
		else if(hh > 127 || hh < 32)
		{
			return false;
		}
	}
	if(count_numeric == 0 || count_alpha == 0 || (count_numeric + count_alpha) < 6){
		return false;
	}

	return true;
}

function checkPhone(phone)
{
   var strng = phone;
   var stripped = strng.replace(/[\(\)\.\-\ \[\]]/g, '');

   var value = true;
   for(var j=0; j < stripped.length; j++)
   {
	  var alphaa = stripped.charAt(j);
	  var hh = alphaa.charCodeAt(0);
	  if(hh < 47 || hh > 57) 
	  {
		value = false;
		break;
	  }
   }
	if(stripped.length < 10)
	{
		value = false;
	}
   //strip out acceptable non-numeric characters
   /*if (isNaN(parseInt(stripped))) {
      //alert("The phone number contains illegal characters.");
      value = false;
   }
   else if (stripped.length < 10) {
 	//alert("The phone number is the wrong length.\nMake sure you included the area code.\n");
	value = false;
   }
   if(value == false){
	return false;
   }
   document.getElementById("phone").value = stripped;*/
   return value;
}

function checknumeric(input)
{
	var numaric = input;
	var count_numeric = 0;
	var value = true;
	
	for(var j=0; j<numaric.length; j++)
	{
		var alphaa = numaric.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if(hh < 48 || hh > 57)
		{
		     value = false;
		}
	}
	//if((numaric % 10) != 0) value = false;
	return value;
}

function checkCard(card)
{
	var strng = card;
	var numaric = strng.replace(/[\[\(\)\.\-\ \]]/g, '');
	
	//strip out acceptable non-numeric characters
	for(var j=0; j<numaric.length; j++)
	{
      var alphaa = numaric.charAt(j);
      var hh = alphaa.charCodeAt(0);
      if(hh < 48 || hh > 57)
      {
      	return false;
      }
	}
	
	// check the length (19 is max length)
	if (numaric.length > 19) 	return false;
	if (numaric.length < 4) 	return false;
	
	document.getElementById("card_number").value = numaric;
	return true;
}

var postState = '';
var postCountry = '';

var country_arr = '\
AF:Afghanistan|\
AL:Albania|\
DZ:Algeria|\
AS:American Samoa|\
AD:Andorra|\
AO:Angola|\
AI:Anguilla|\
AQ:Antarctica|\
AG:Antigua and Barbuda|\
AR:Argentina|\
AM:Armenia|\
AW:Aruba|\
AU:Australia|\
AT:Austria|\
AZ:Azerbaijan|\
AP:Azores|\
BS:Bahamas|\
BH:Bahrain|\
BD:Bangladesh|\
BB:Barbados|\
BY:Belarus|\
BE:Belgium|\
BZ:Belize|\
BJ:Benin|\
BM:Bermuda|\
BT:Bhutan|\
BO:Bolivia|\
BA:Bosnia And Herzegowina|\
XB:Bosnia-Herzegovina|\
BW:Botswana|\
BV:Bouvet Island|\
BR:Brazil|\
IO:British Indian Ocean Territory|\
VG:British Virgin Islands|\
BN:Brunei Darussalam|\
BG:Bulgaria|\
BF:Burkina Faso|\
BI:Burundi|\
KH:Cambodia|\
CM:Cameroon|\
CA:Canada|\
CV:Cape Verde|\
KY:Cayman Islands|\
CF:Central African Republic|\
TD:Chad|\
CL:Chile|\
CN:China|\
CX:Christmas Island|\
CC:Cocos (Keeling) Islands|\
CO:Colombia|\
KM:Comoros|\
CG:Congo|\
CD:Congo, The Democratic Republic O|\
CK:Cook Islands|\
XE:Corsica|\
CR:Costa Rica|\
CI:Cote d` Ivoire (Ivory Coast)|\
HR:Croatia|\
CU:Cuba|\
CY:Cyprus|\
CZ:Czech Republic|\
DK:Denmark|\
DJ:Djibouti|\
DM:Dominica|\
DO:Dominican Republic|\
TP:East Timor|\
EC:Ecuador|\
EG:Egypt|\
SV:El Salvador|\
GQ:Equatorial Guinea|\
ER:Eritrea|\
EE:Estonia|\
ET:Ethiopia|\
FK:Falkland Islands (Malvinas)|\
FO:Faroe Islands|\
FJ:Fiji|\
FI:Finland|\
FR:France (Includes Monaco)|\
FX:France, Metropolitan|\
GF:French Guiana|\
PF:French Polynesia|\
TA:French Polynesia (Tahiti)|\
TF:French Southern Territories|\
GA:Gabon|\
GM:Gambia|\
GE:Georgia|\
DE:Germany|\
GH:Ghana|\
GI:Gibraltar|\
GR:Greece|\
GL:Greenland|\
GD:Grenada|\
GP:Guadeloupe|\
GU:Guam|\
GT:Guatemala|\
GN:Guinea|\
GW:Guinea-Bissau|\
GY:Guyana|\
HT:Haiti|\
HM:Heard And Mc Donald Islands|\
VA:Holy See (Vatican City State)|\
HN:Honduras|\
HK:Hong Kong|\
HU:Hungary|\
IS:Iceland|\
IN:India|\
ID:Indonesia|\
IR:Iran|\
IQ:Iraq|\
IE:Ireland|\
EI:Ireland (Eire)|\
IL:Israel|\
IT:Italy|\
JM:Jamaica|\
JP:Japan|\
JO:Jordan|\
KZ:Kazakhstan|\
KE:Kenya|\
KI:Kiribati|\
KP:Korea, Democratic People\'S Repub|\
KW:Kuwait|\
KG:Kyrgyzstan|\
LA:Laos|\
LV:Latvia|\
LB:Lebanon|\
LS:Lesotho|\
LR:Liberia|\
LY:Libya|\
LI:Liechtenstein|\
LT:Lithuania|\
LU:Luxembourg|\
MO:Macao|\
MK:Macedonia|\
MG:Madagascar|\
ME:Madeira Islands|\
MW:Malawi|\
MY:Malaysia|\
MV:Maldives|\
ML:Mali|\
MT:Malta|\
MH:Marshall Islands|\
MQ:Martinique|\
MR:Mauritania|\
MU:Mauritius|\
YT:Mayotte|\
MX:Mexico|\
FM:Micronesia, Federated States Of|\
MD:Moldova, Republic Of|\
MC:Monaco|\
MN:Mongolia|\
MS:Montserrat|\
MA:Morocco|\
MZ:Mozambique|\
MM:Myanmar (Burma)|\
NA:Namibia|\
NR:Nauru|\
NP:Nepal|\
NL:Netherlands|\
AN:Netherlands Antilles|\
NC:New Caledonia|\
NZ:New Zealand|\
NI:Nicaragua|\
NE:Niger|\
NG:Nigeria|\
NU:Niue|\
NF:Norfolk Island|\
MP:Northern Mariana Islands|\
NO:Norway|\
OM:Oman|\
PK:Pakistan|\
PW:Palau|\
PS:Palestinian Territory, Occupied|\
PA:Panama|\
PG:Papua New Guinea|\
PY:Paraguay|\
PE:Peru|\
PH:Philippines|\
PN:Pitcairn|\
PL:Poland|\
PT:Portugal|\
PR:Puerto Rico|\
QA:Qatar|\
RE:Reunion|\
RO:Romania|\
RU:Russian Federation|\
RW:Rwanda|\
KN:Saint Kitts And Nevis|\
SM:San Marino|\
ST:Sao Tome and Principe|\
SA:Saudi Arabia|\
SN:Senegal|\
XS:Serbia-Montenegro|\
SC:Seychelles|\
SL:Sierra Leone|\
SG:Singapore|\
SK:Slovak Republic|\
SI:Slovenia|\
SB:Solomon Islands|\
SO:Somalia|\
ZA:South Africa|\
GS:South Georgia And The South Sand|\
KR:South Korea|\
ES:Spain|\
LK:Sri Lanka|\
NV:St. Christopher and Nevis|\
SH:St. Helena|\
LC:St. Lucia|\
PM:St. Pierre and Miquelon|\
VC:St. Vincent and the Grenadines|\
SD:Sudan|\
SR:Suriname|\
SJ:Svalbard And Jan Mayen Islands|\
SZ:Swaziland|\
SE:Sweden|\
CH:Switzerland|\
SY:Syrian Arab Republic|\
TW:Taiwan|\
TJ:Tajikistan|\
TZ:Tanzania|\
TH:Thailand|\
TG:Togo|\
TK:Tokelau|\
TO:Tonga|\
TT:Trinidad and Tobago|\
XU:Tristan da Cunha|\
TN:Tunisia|\
TR:Turkey|\
TM:Turkmenistan|\
TC:Turks and Caicos Islands|\
TV:Tuvalu|\
UG:Uganda|\
UA:Ukraine|\
AE:United Arab Emirates|\
UK:United Kingdom|\
GB:Great Britain|\
US:United States|\
UM:United States Minor Outlying Isl|\
UY:Uruguay|\
UZ:Uzbekistan|\
VU:Vanuatu|\
XV:Vatican City|\
VE:Venezuela|\
VN:Vietnam|\
VI:Virgin Islands (U.S.)|\
WF:Wallis and Furuna Islands|\
EH:Western Sahara|\
WS:Western Samoa|\
YE:Yemen|\
YU:Yugoslavia|\
ZR:Zaire|\
ZM:Zambia|\
ZW:Zimbabwe';

function init_country(country)
{
	var selectObject = document.getElementById("country");
	populate_country(country, selectObject);
}

function TrimString(sInString)
{
	if (sInString)
	{
		sInString = sInString.replace( /^\s+/g, "" );// strip leading
		return sInString.replace( /\s+$/g, "" );// strip trailing
	}
}

function populate_seq_question(question)
{
	var selectObject = document.getElementById("security_question");
	
	selectObject.options[0] = new Option('','');
	selectObject.selectedIndex = 0;
	
	var questions = new Array();
	questions[0] = "Your favorite person?";
	questions[1] = "Your pet's name?";
	questions[2] = "Your favorite movie?";
	questions[3] = "Your mother's maiden name?";
	questions[4] = "Your favorite sports team?";
	questions[5] = "Your first company?";
	
	for (var loop = 0; loop < questions.length; loop++)
	{
		var current_question = questions[loop];
		
		selectObject.options[loop + 1] = new Option(current_question, current_question);
		if (current_question == question)
		{
			selectObject.selectedIndex = loop + 1;
		}
	}
}

function populate_country(defaultCountry, selectObject)
{
	if (postCountry != '')
	{
		defaultCountry = postCountry;
	}
	
	var countryLineArray = country_arr.split('|');  // Split into lines
	
	selectObject.options[0] = new Option('Select Country','');
	selectObject.selectedIndex = 0;
	
	for (var loop = 0; loop < countryLineArray.length; loop++)
	{
		lineArray = countryLineArray[loop].split(':');
		countryCode  = TrimString(lineArray[0]);
		countryName  = TrimString(lineArray[1]);
		if (countryCode != '')
		{
			selectObject.options[loop + 1] = new Option(countryName, countryCode);
		}
		if (defaultCountry == countryCode)
		{
			selectObject.selectedIndex = loop + 1;
		}
	}
}

/************************************************************************************
* Resubmit scripts beginning.
*************************************************************************************/

function get_resubmit_info_array()
{
	var resubmit_info = {
        card_type:null,
        card_number:null,			  
    	card_security_code:null,	  
    	card_name:null,
    	expiration_month:null,
    	expiration_year:null
    };
    
    resubmit_info.card_type				= document.getElementById("card_type").value;			
	resubmit_info.card_number			= document.getElementById("card_number").value;	  		
	resubmit_info.card_security_code 	= document.getElementById("card_security_code").value;	
	resubmit_info.card_name 			= document.getElementById("card_name").value;			
	
	resubmit_info.expiration_month 		= document.getElementById("expiration_month").value;	
	resubmit_info.expiration_year 		= document.getElementById("expiration_year").value;		
    
    return resubmit_info;
}

function show_resubmit_page()
{
	document.business_registration.action = "page.php?id=reg_resubmit";
	document.business_registration.submit();	
}

function resubmitform()
{
	if(!validate_resubmit_page())
		return;
			
	var account_id = document.getElementById("account_id").value;
	
	var ri = get_resubmit_info_array();
	g_adminInfo = init_admin_info_array();
	
	g_adminInfo.address_1 = document.getElementById("address_1").value;
    g_adminInfo.address_2 = document.getElementById("address_2").value;
    g_adminInfo.city = document.getElementById("city").value;
    g_adminInfo.country = document.getElementById("country").value;
    g_adminInfo.zip = document.getElementById("zip").value;
    g_adminInfo.turing = document.getElementById("turing").value;
    g_adminInfo.login_email = document.getElementById("login_email").value;
    
	g_adminInfo.card_type = ri.card_type;
	g_adminInfo.card_number = ri.card_number;
	g_adminInfo.card_security_code = ri.card_security_code;
	g_adminInfo.card_name = ri.card_name;
	g_adminInfo.expiration_month = ri.expiration_month;
	g_adminInfo.expiration_year = ri.expiration_year;
	
	create_sale_transaction(account_id, g_adminInfo);
}

function hide_resubmit_page_errors()
{
	document.getElementById("resubmit_info_error_hint").style.display = "none";
	document.getElementById("card_type_error").style.display = "none";
	document.getElementById("card_name_error").style.display = "none";
	document.getElementById("card_number_error").style.display = "none";
	document.getElementById("card_security_code_error").style.display = "none";
	document.getElementById("expiration_month_error").style.display = "none";
	document.getElementById("expiration_year_error").style.display = "none";
}

function validate_resubmit_page()
{
	hide_resubmit_page_errors();
	
	var ri = get_resubmit_info_array();
		
	if(ri.card_type == '')
	{	
	   show_error_message('card_type', 'empty_error');	   
	   return false;
	}
	if(ri.card_name == '')
	{	
	   show_error_message('card_name', 'empty_error');	   
	   return false;
	}
	if(ri.card_number == '')
	{	
	   show_error_message('card_number', 'empty_error');	   
	   return false;
	}
	if(!checkCard(ri.card_number))
	{
	   show_error_message('card_number', 'incorrect_error');	   
	   return false;
	}
	if(ri.card_security_code == '')
	{	
	   show_error_message('card_security_code', 'empty_error');	   
	   return false;
	}
	if(ri.expiration_month == '')
	{	
	   show_error_message('expiration_month', 'empty_error');	   
	   return false;
	}
	if(ri.expiration_year == '')
	{	
	   show_error_message('expiration_year', 'empty_error');	   
	   return false;
	}
	return true;
}

function cancel_resubmit()
{
	document.business_registration.action = "page.php?id=reg_cancel_resubmit";
	document.business_registration.submit();
}

function leave_resubmit()
{
	window.location = 'page.php?id=download_allenport';
}

function cancel_end_resubmit()
{
	document.business_registration.action = "page.php?id=reg_resubmit";
	document.business_registration.submit();
}




