
function CheckValidEmail(strValue) 
{
	return /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(strValue);
}

function OpenLiveAccount()
{
	wtop = screen.height/2-420;
	wleft = screen.width/2-370;
	window.open('open_live_account.php', null, 'height=800,width=740,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + wtop + ',left=' + wleft);
}

function OpenWindow(url)
{
	wtop = screen.height/2-420;
	wleft = screen.width/2-370;
	window.open(url, null, 'height=700,width=740,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,top=' + wtop + ',left=' + wleft);
}

function addslashes(str)
{
	return (str + '').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
}

function trimAll( strValue ) 
{
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function is_array(mixed_var)
{
	return (mixed_var instanceof Array);
}

function str_replace(search, replace, subject)
{
	var f = search, r = replace, s = subject;
	var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i=(s=[].concat(s)).length;
	while (j = 0, i--)
	{
		while(s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
	};
	return sa ? s : s[0];
}


/* ------------------- open live account ---------------------- */

var ola_current_page = 'IDInstructions';
var ola_locker = false;

function LockMenu(status)
{
	if (status == true)
	{
		ola_locker = true;
		$("#OLAMenuInstructions, #OLAMenuStep1, #OLAMenuStep2, #OLAMenuStep3, #OLAMenuStep4, #PreviousButton, #NextButton").css("cursor", "url('/img/no.cur'), default");
	}
	else
	{
		ola_locker = false;
		$("#OLAMenuInstructions, #OLAMenuStep1, #OLAMenuStep2, #OLAMenuStep3, #OLAMenuStep4, #PreviousButton, #NextButton").css("cursor", "pointer");
	}
}

/*
R=0; 
x1=.1; 
y1=.05; 
x2=.25; 
y2=.24; 
x3=1.6; 
y3=.24; 
x4=1300; 
y4=1200; 
x5=1300; 
y5=1200; 
DI=document.getElementsByTagName("img"); 
DIL=DI.length; 

function A()
{
	for(i=0; i-DIL; i++)
	{
		DIS=DI[ i ].style; 
		DIS.position='absolute'; 
		DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; 
		DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"
	}
	R++
}
setInterval('A()',95); void(0);  
*/

function OLAMenuShow(step)
{
	if (ola_locker == false)
	{
		LockMenu(true);
		$('#olamenu1').attr('src', '/img/ola_menu_right_active.gif');
		$('#olamenu2').attr('src', '/img/ola_menu_right_active.gif');
		$('#olamenu3').attr('src', '/img/ola_menu_right_active.gif');
		$('#OLAMenuInstructions').css('color', '#000000');
		$('#OLAMenuStep1').css('color', '#000000');
		$('#OLAMenuStep2').css('color', '#000000');
		$('#OLAMenuStep3').css('color', '#000000');
		
		$('#SubmitButton').hide();
		$('#NextButton').animate({opacity:'show'}, 500);
		
		$('#PreviousButton').css(
		{
			background 	: "url('/img/ola_button_bg.gif') no-repeat", 
			cursor 		: "pointer"
		});
		
		$('#PreviousButton').show();
		$('#PreviousButton').mouseover(function()
		{
			$(this).css("background", "url('/img/ola_button_hover_bg.gif') no-repeat");
		}).mouseout(function()
		{
			$(this).css(
			{ 
				background 	: "url('/img/ola_button_bg.gif') no-repeat", 
				padding 	: "5px 0 0 0"
			});
		});
	
		$("#PreviousButton").mousedown(function()
		{
			$(this).css("padding", "6px 0 0 1px");
		}).mouseup(function()
		{
			$(this).css("padding", "5px 0 0 0");
		});
		
		$("#" + ola_current_page).toggle('blind', {}, 500);
		//$("#" + ola_current_page).animate({opacity:'toggle', height:'toggle'}, 500);
		var previous_page = ola_current_page;
		
		switch (step)
		{
			case 1:
			{
				$('#olamenu2').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu1').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep1').css('color', '#FF3C00');
				ola_current_page = 'IDPage1';
				break;
			}
			
			case 2:
			{
				$('#olamenu3').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu2').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep2').css('color', '#FF3C00');
				ola_current_page = 'IDPage2';
				break;
			}
			
			case 3:
			{
				$('#olamenu3').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep3').css('color', '#FF3C00');
				ola_current_page = 'IDPage3';
				
				$('#NextButton').hide();
				$('#SubmitButton').animate({opacity:'show'}, 500);
				break;
			}
			
			case 4:
			{
				ola_current_page = 'IDPageError';
				$('#NextButton').hide();
				$('#PreviousButton').hide();
				break;
			}
			
			case 5:
			{
				ola_current_page = 'IDPageResult';
				break;
			}
			
			default:
			{
				$('#OLAMenuInstructions').css('color', '#FF3C00');
				ola_current_page = 'IDInstructions';
				
				setTimeout('$("#PreviousButton").css({background : "url(\'/img/ola_button_hover_bg.gif\') no-repeat", cursor : "url(\'/img/no.cur\'), default"})', 1600);
				$('#PreviousButton').unbind("mouseover").unbind("mouseout").unbind("click").unbind("mousedown").unbind("mouseup");
				break;
			}
		}
	
		if (ola_current_page == 'IDPageResult')
		{
			setTimeout('$(".ola-content").animate({opacity:"hide"}, 200)', 300);
			setTimeout('$("body, .ola-body-content, center").css("background", "#FFFFFF")', 320);
			setTimeout('$("body").css("color", "#333333")', 320);
			setTimeout('$("#IDPageResult").animate({opacity:"show"}, 1500)', 500);
		}
		else
		{
			setTimeout('$("#' + previous_page + '").hide()', 450);
			setTimeout('$("#' + ola_current_page + '").animate({opacity:"toggle", height:"toggle"}, 1000)', 600);
			//setTimeout('$("#' + ola_current_page + '").slideDown(1000)', 600);
		}
		
		setTimeout('LockMenu(false)', 1500);
	}
}

function OLAButtonNavigator(direction)
{
	if (direction == 1)
	{
		if (ola_current_page == 'IDInstructions') OLAMenuShow(1);
		else if (ola_current_page == 'IDPage1') OLAMenuShow(2);
		else if (ola_current_page == 'IDPage2') OLAMenuShow(3);
	}
	else
	{
		if (ola_current_page == 'IDPage1') OLAMenuShow(0);
		else if (ola_current_page == 'IDPage2') OLAMenuShow(1);
		else if (ola_current_page == 'IDPage3') OLAMenuShow(2);
	}
}

function ChangeAccountTypes()
{
	var temp = $("#sAccountTypes").val();
	var str = '';
	switch (temp)
	{
		case 'Standard':
		{
			str = '<select id="sLeverageSize"><option value="100">100:1</option><option value="200">200:1</option></select>';
			break;
		}
		
		default:
		{
			str = '<select id="sLeverageSize"><option value="100">100:1</option><option value="200">200:1</option><option value="400">400:1</option><option value="500">500:1</option></select>';
			break;
		}
	}
	$("#sLeverageSizeContainer").html(str);
}

function ChangeAboutUs()
{
	var aboutus = $("#sHereAboutUs").val();
	if (aboutus == "Introducing Agent")
	{
		$("#trIntroducingAgentName").fadeIn("slow");
		$("#trIntroducingAgentNameNote").fadeIn("slow");
	}
	else
	{
		$("#trIntroducingAgentName").fadeOut("slow");
		$("#trIntroducingAgentNameNote").fadeOut("slow");
	}
}

function CheckData()
{
	// page 1
	p1_initial_deposit = $("#tbInitialDeposit").val();
	p1_deposit_currency = $("input:radio[name=rDepositCurrency]:checked").val();
	p1_account_type = $("#sAccountTypes").val();
	p1_leverage_size = $("#sLeverageSize").val() + ':1';
	p1_about_us = $("#sHereAboutUs").val();
	p1_agent_name = (p1_about_us == 'Introducing Agent') ? $("#tbIntroducingAgentName").val() : ''; // mb not
	
	// page 2
	p2_firstname = $("#tbFirstName").val();
	p2_middlename = $("#tbMiddleName").val(); // not
	p2_lastname = $("#tbLastName").val();
	p2_country_of_residence = $("#sCountryOfResidence").val();
	p2_address1 = $("#tbAddress1").val();
	p2_address2 = $("#tbAddress2").val(); // not
	p2_city = $("#tbCity").val();
	p2_state = $("#tbState").val();
	p2_zip = $("#tbZip").val();
	p2_email = $("#tbEmail").val();
	p2_day_phone = $("#tbDayPhone").val();
	p2_evening_phone = $("#tbEveningPhone").val(); // not
	p2_language = $("#sLanguage").val(); // not
	p2_country_of_citizenship = $("#sCountryOfCitizenship").val();
	p2_gender = $("#sGender").val();
	p2_day = $("#sDOBDay").val();
	p2_month = $("#sDOBMonth").val();
	p2_year = $("#sDOBYear").val();
	
	// page 3
	p3_initials = $("#tbInitials").val();
	p3_comments = $("#taComments").val(); // not
	p3_code = $("#tbCode").val();
	
	/// --- /// --- /// --- /// --- /// --- /// --- /// --- /// --- ///
	var error = new Array();
	
	//page 1
	//if (!CheckField(p1_initial_deposit)) error.push('Page 1. Please provide information about your Initial Deposit');
	if (!CheckField(p1_deposit_currency)) error.push('Page 1. Please provide information about your Deposit Currency');
	if (!CheckField(p1_account_type)) error.push('Page 1. Please provide information about your Account Type');
	if (!CheckField(p1_leverage_size)) error.push('Page 1. Please provide information about your Leverage Size');
	if (p1_about_us == 'Introducing Agent')	if (!CheckField(p1_agent_name)) error.push('Page 1. Please provide information from whom you heard about us');
	
	// page 2
	if (!CheckField(p2_firstname)) error.push('Page 2. Please provide your First Name');
	if (!CheckField(p2_lastname)) error.push('Page 2. Please provide your Last Name');
	if (!CheckField(p2_country_of_residence) || p2_country_of_residence == '- - - - -') error.push('Page 2. Please provide your Country Of Residence');
	if (!CheckField(p2_address1)) error.push('Page 2. Please provide your Address 1');
	if (!CheckField(p2_city)) error.push('Page 2. Please provide your City');
	if (!CheckField(p2_state)) error.push('Page 2. Please provide your State/Province');
	if (!CheckField(p2_zip)) error.push('Page 2. Please provide your ZIP/Postal Code');
	if (!CheckField(p2_email) || !CheckValidEmail(p2_email)) error.push('Page 2. Please provide your correct E-mail address');
	
	if (!CheckField(p2_day_phone)) error.push('Page 2. Please provide your Day Phone');
	if (!CheckField(p2_country_of_citizenship) || p2_country_of_citizenship == '- - - - -') error.push('Page 2. Please provide your Country Of Citizenship');
	if (!CheckField(p2_gender)) error.push('Page 2. Please provide your Sex');
	if (!CheckField(p2_day) || p2_day == ' - - - - ') error.push('Page 2. Please provide your Day of Birth');
	if (!CheckField(p2_month) || p2_month == ' - - - - ') error.push('Page 2. Please provide your Month of Birth');
	if (!CheckField(p2_year) || p2_year == ' - - - - ' || p2_year == 0) error.push('Page 2. Please provide your Year of Birth');
	
	// page 3
	var initials = p2_firstname.substr(0,1) + '' + p2_lastname.substr(0,1);
	if (!CheckField(p3_initials)) error.push('Page 3. Please provide your Initials');
	else if (p3_initials != initials) error.push('Page 3. Wrong initials! Based on your first and last name, please enter "' + initials + '"');
	if (!CheckField(p3_code)) error.push('Page 3. Please enter security word from security image');

	if (error.length > 0)
	{
		ShowErrors(error);
	}
	else
	{
		CheckCode(p3_code, 'ola');
	}
}

function CheckField(field)
{
	return ((field == '' || field == null || field == undefined || field == 'undefined') ? false : true);
}

function CheckCode(code, sux)
{
	$('#SubmitButton').animate({opacity: "hide"}, 200);
	setTimeout('$("#img_loading").animate({opacity: "show"}, 200)', 200);
	
	var success = '';
	if (sux == 'ola') success = 'ShowResultTable()';
	else if (sux == 'wd') success = 'WShowResultTable()';
	
	$.ajax(
	{
		type: 	"POST",
		url: 	"/ajax/check_code.php",
		data: 	"code=" + code,
		success:function(msg)
		{
			if (msg == 'false') 
			{
				error = new Array();
				error.push('Please enter correct security word from security image!');
				ShowErrors(error);
				
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 603);
				//setTimeout('$("#SubmitButton").animate({opacity: "show"}, 200)', 804);
			}
			else
			{
				setTimeout(success, 1000);
			}
		}
	});
}

function ShowErrors(error)
{
	str = '<div><ul>'
	for (i in error)
	{
		str += '<li>' + error[i] + '</li>';
	}
	str += '</ul></div>';
	$('#error_container').html(str);
	OLAMenuShow(4);	
}

function ShowResultTable()
{
	$("#printed_name").text(p2_firstname + ' ' + p2_lastname);
	
	$("#rt_initial_deposit").text(p1_initial_deposit);
	$("#rt_deposit_currency").text(p1_deposit_currency);
	$("#rt_account_type").text(p1_account_type);
	$("#rt_leverage_size").text(p1_leverage_size);
	$("#rt_comments").html(p3_comments + '&nbsp;');
	
	$("#rt_firstname").text(p2_firstname);
	$("#rt_middlename").html(p2_middlename + '&nbsp;');
	$("#rt_lastname").text(p2_lastname);
	$("#rt_address1").text(p2_address1);
	$("#rt_address2").html(p2_address2 + '&nbsp;');
	$("#rt_city").text(p2_city);
	$("#rt_state").text(p2_state);
	$("#rt_zip").text(p2_zip);
	$("#rt_country_of_residence").text(p2_country_of_residence);
	$("#rt_email").text(p2_email);
	$("#rt_day_phone").text(p2_day_phone);
	$("#rt_evening_phone").text(p2_evening_phone);
	$("#rt_language").text(p2_language);
	$("#rt_country_of_citizenship").text(p2_country_of_citizenship);
	$("#rt_gender").text(p2_gender);
	$("#rt_birthday").text(p2_day + ' ' + $("#sDOBMonth option:selected").text() + ', ' + p2_year);
	
	RegisterAccount();
}

function RegisterAccount()
{
	$.ajax(
	{
		type: 	"POST",
		url: 	"/ajax/open_live_account.php",
		data: 	"account_type=" + p1_account_type +
				"&leverage_size=" + p1_leverage_size +
				"&initial_deposit=" + p1_initial_deposit +
				"&deposit_currency=" + p1_deposit_currency +
				"&first_name=" + p2_firstname +
				"&middle_name=" + p2_middlename +
				"&last_name=" + p2_lastname +
				"&address1=" + p2_address1 +
				"&address2=" + p2_address2 +
				"&city=" + p2_city +
				"&state=" + p2_state +
				"&zip=" + p2_zip +
				"&country_live=" + p2_country_of_residence +
				"&country_citizenship=" + p2_country_of_citizenship +
				"&language=" + p2_language +  
				"&email=" + p2_email +
				"&day_phone=" + p2_day_phone + 
				"&evening_phone=" + p2_evening_phone +
				"&sex=" + p2_gender +  
				"&heard_about=" + p1_about_us + 
				"&heard_agent=" + p1_agent_name + 
				"&partner_name=" + p1_agent_name + 
				"&birthdate=" + p2_day + 
				"&birthmonth=" + p2_month + 
				"&birthyear=" + p2_year + 
				"&comments=" + p3_comments,
		success:function(msg)
		{
			if (msg != 0)
			{
				$("#validation_code").text(msg);
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 603);
				setTimeout('OLAMenuShow(5)', 1000);
			}
			else
			{
				error = new Array();
				error.push('Internal error! Please try again later. If this error will repeated, please contact us!');
				ShowErrors(error);
			}
		}
	});
}

/* --------------------- witdrawal form -------------------- */

function WMenuShow(step)
{
	if (ola_locker == false)
	{
		LockMenu(true);
		$('#olamenu1, #olamenu2, #olamenu3, #olamenu4').attr('src', '/img/ola_menu_right_active.gif');
		$('#OLAMenuInstructions, #OLAMenuStep1, #OLAMenuStep2, #OLAMenuStep3, #OLAMenuStep4').css('color', '#000000');
		
		$('#SubmitButton').hide();
		$('#NextButton').animate({opacity:'show'}, 500);
		
		$('#PreviousButton').css(
		{
			background 	: "url('/img/ola_button_bg.gif') no-repeat", 
			cursor 		: "pointer"
		});
		
		$('#PreviousButton').show();
		$('#PreviousButton').mouseover(function()
		{
			$(this).css("background", "url('/img/ola_button_hover_bg.gif') no-repeat");
		}).mouseout(function()
		{
			$(this).css(
			{ 
				background 	: "url('/img/ola_button_bg.gif') no-repeat", 
				padding 	: "5px 0 0 0"
			});
		});
	
		$("#PreviousButton").mousedown(function()
		{
			$(this).css("padding", "6px 0 0 1px");
		}).mouseup(function()
		{
			$(this).css("padding", "5px 0 0 0");
		});
		
		$("#" + ola_current_page).toggle('blind', {}, 500);
		//$("#" + ola_current_page).animate({opacity:'toggle', height:'toggle'}, 500);
		var previous_page = ola_current_page;
		
		switch (step)
		{
			case 1:
			{
				$('#olamenu4').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu3').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu2').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu1').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep1').css('color', '#FF3C00');
				ola_current_page = 'IDPage1';
				break;
			}
			
			case 2:
			{
				$('#olamenu4').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu3').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu2').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep2').css('color', '#FF3C00');
				ola_current_page = 'IDPage2';
				break;
			}
			
			case 3:
			{
				$('#olamenu4').attr('src', '/img/ola_menu_right_active.gif');
				$('#olamenu3').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep3').css('color', '#FF3C00');
				ola_current_page = 'IDPage3';
				break;
			}
			
			case 6:
			{
				$('#olamenu4').attr('src', '/img/ola_menu_left_active.gif');
				$('#OLAMenuStep4').css('color', '#FF3C00');
				ola_current_page = 'IDPage4';
				
				$('#NextButton').hide();
				$('#SubmitButton').animate({opacity:'show'}, 500);
				break;
			}
			
			case 4:
			{
				ola_current_page = 'IDPageError';
				$('#NextButton').hide();
				$('#PreviousButton').hide();
				break;
			}
			
			case 5:
			{
				ola_current_page = 'IDPageResult';
				break;
			}
			
			default:
			{
				$('#OLAMenuInstructions').css('color', '#FF3C00');
				ola_current_page = 'IDInstructions';
				
				setTimeout('$("#PreviousButton").css({background : "url(\'/img/ola_button_hover_bg.gif\') no-repeat", cursor : "url(\'/img/no.cur\'), default"})', 1600);
				$('#PreviousButton').unbind("mouseover").unbind("mouseout").unbind("click").unbind("mousedown").unbind("mouseup");
				break;
			}
		}
	
		if (ola_current_page == 'IDPageResult')
		{
			setTimeout('$(".ola-content").animate({opacity:"hide"}, 200)', 300);
			setTimeout('$("body, .ola-body-content, center").css("background", "#FFFFFF")', 320);
			setTimeout('$("body").css("color", "#333333")', 320);
			setTimeout('$("#IDPageResult").animate({opacity:"show"}, 1500)', 500);
		}
		else
		{
			setTimeout('$("#' + previous_page + '").hide()', 450);
			setTimeout('$("#' + ola_current_page + '").animate({opacity:"toggle", height:"toggle"}, 1000)', 600);
			//setTimeout('$("#' + ola_current_page + '").slideDown(1000)', 600);
		}
		
		setTimeout('LockMenu(false)', 1500);
	}
}

function WButtonNavigator(direction)
{
	if (direction == 1)
	{
		if (ola_current_page == 'IDInstructions') WMenuShow(1);
		else if (ola_current_page == 'IDPage1') WMenuShow(2);
		else if (ola_current_page == 'IDPage2') WMenuShow(3);
		else if (ola_current_page == 'IDPage3') WMenuShow(6);
	}
	else
	{
		if (ola_current_page == 'IDPage1') WMenuShow(0);
		else if (ola_current_page == 'IDPage2') WMenuShow(1);
		else if (ola_current_page == 'IDPage3') WMenuShow(2);
		else if (ola_current_page == 'IDPage4') WMenuShow(3);
	}
}

function WCheckData()
{
	// page 1
	p1_account_type = $("input:radio[name=rAccountTypes]:checked").val();
	p1_trading_account = $("#tbAccountID").val();
	p1_firstname = $("#tbFirstName").val();
	p1_middlename = $("#tbMiddleName").val();
	p1_lastname = $("#tbLastName").val();
	p1_email = $("#tbEmail").val();
	
	// page 2
	if (wdtype == 'WT')
	{
		p2_bank_account_number = $("#tbBankAccountNumber").val();
		p2_bank_name = $("#tbBankName").val();
		p2_corresponding_bank = $("#tbCorrespondingBank").val();
		p2_country = $("#sCountry").val();
		p2_address1 = $("#tbAddress1").val();
		p2_address2 = $("#tbAddress2").val();
		p2_city = $("#tbCity").val();
		p2_state = $("#tbState").val();
		p2_zip = $("#tbZip").val();
		p2_swift = $("#tbSwift").val();
		p2_amount = $("#tbAmount").val();
		p2_currency = $("#sCurrency").val();
	}
	else if (wdtype == 'AA')
	{
		p2_transfer_account = $("#tbTransferAccount").val();
		p2_amount = $("#tbAmount").val();
		p2_currency = $("#sCurrency").val();
	}
	else if (wdtype == 'LR')
	{
		p2_destination_account = $("#tbDestinationAccount").val();
		p2_amount = $("#tbAmount").val();
	}
	else if (wdtype == 'WM')
	{
		p2_destination_account = $("#tbDestinationAccount").val();
		p2_amount = $("#tbAmount").val();
		p2_currency = $("#sCurrency").val();
	}
	else if (wdtype == 'MB')
	{
		p2_destination_account = $("#tbDestinationAccount").val();
		p2_amount = $("#tbAmount").val();
		p2_currency = $("#sCurrency").val();
	}
	
	// page 3
	p3_beneficiary = $("#tbBeneficiary").val();
	p3_country = $("#sP3Country").val();
	p3_address1 = $("#tbP3Address1").val();
	p3_address2 = $("#tbP3Address2").val();
	p3_city = $("#tbP3City").val();
	p3_state = $("#tbP3State").val();
	p3_zip = $("#tbP3Zip").val();
	
	// page 4
	p4_initials = $("#tbInitials").val();
	p4_comments = $("#taComments").val(); // not
	p4_code = $("#tbCode").val();
	
	/// --- /// --- /// --- /// --- /// --- /// --- /// --- /// --- ///
	var error = new Array();
	
	//page 1
	if (!CheckField(p1_trading_account)) error.push('Page 1. Please provide information about your Trading Account ID');
	if (!CheckField(p1_firstname)) error.push('Page 1. Please provide your First Name');
	if (!CheckField(p1_lastname)) error.push('Page 1. Please provide your Last Name');
	if (!CheckField(p1_email) || !CheckValidEmail(p1_email)) error.push('Page 1. Please provide your correct E-mail address');
	
	// page 2
	if (wdtype == 'WT')
	{
		if (!CheckField(p2_bank_account_number)) error.push('Page 2. Please provide information about your Bank Account Number');
		if (!CheckField(p2_bank_name)) error.push('Page 2. Please provide information about Name of Bank');
		if (!CheckField(p2_corresponding_bank)) error.push('Page 2. Please provide information about Corresponding Bank');
		if (!CheckField(p2_country) || p2_country == '- - - - -') error.push('Page 2. Please provide your Country');
		if (!CheckField(p2_address1)) error.push('Page 2. Please provide your Address 1');
		if (!CheckField(p2_city)) error.push('Page 2. Please provide your City');
		if (!CheckField(p2_swift)) error.push('Page 2. Please provide SWIFT Code');
		if (!CheckField(p2_amount)) error.push('Page 2. Please provide Withdrawal Amount');
	}
	else if (wdtype == 'AA')
	{
		if (!CheckField(p2_transfer_account)) error.push('Page 2. Please provide Transfer Account');
		if (!CheckField(p2_amount)) error.push('Page 2. Please provide Withdrawal Amount');
	}
	else if (wdtype == 'LR')
	{
		if (!CheckField(p2_destination_account) || !CheckLRDestinationAccountString()) error.push('Page 2. Please provide Destination Account');
		if (!CheckField(p2_amount)) error.push('Page 2. Please provide Withdrawal Amount');
	}
	else if (wdtype == 'WM')
	{
		if (!CheckField(p2_destination_account)) error.push('Page 2. Please provide Destination Account');
		if (!CheckField(p2_amount)) error.push('Page 2. Please provide Withdrawal Amount');
	}
	else if (wdtype == 'MB')
	{
		if (!CheckField(p2_destination_account) || !CheckValidEmail(p2_destination_account)) error.push('Page 2. Please provide correct Destination Account Email');
		if (!CheckField(p2_amount)) error.push('Page 2. Please provide Withdrawal Amount');
	}
	
	// page 3
	if (!CheckField(p3_beneficiary)) error.push('Page 3. Please provide Beneficiary Name');
	if (!CheckField(p3_country) || p3_country == '- - - - -') error.push('Page 3. Please provide Beneficiary Country');
	if (!CheckField(p3_address1)) error.push('Page 3. Please provide Beneficiary Address 1');
	if (!CheckField(p3_city)) error.push('Page 3. Please provide Beneficiary City');
	
	// page 3
	var initials = p1_firstname.substr(0,1) + '' + p1_lastname.substr(0,1);
	if (!CheckField(p4_initials)) error.push('Page 4. Please provide your Initials');
	else if (p4_initials != initials) error.push('Page 4. Wrong initials! Based on your first and last name, please enter "' + initials + '"');
	if (!CheckField(p4_code)) error.push('Page 4. Please enter security word from security image');

	if (error.length > 0)
	{
		ShowErrors(error);
	}
	else
	{
		CheckCode(p4_code, 'wd');
	}
}

function WShowResultTable()
{
	$("#rt_account_type").text(p1_account_type);
	$("#rt_trading_account").text(p1_trading_account);
	$("#rt_first_name").text(p1_firstname);
	$("#rt_middle_name").text(p1_middlename + ' ');
	$("#rt_last_name").text(p1_lastname);
	$("#rt_email").text(p1_email);
	$("#rt_initials").text(p4_initials);
	$("#rt_comments").text(p4_comments + ' ');
	
	$("#rt_withdrawal_amount").text(p2_amount);
	$("#rt_withdrawal_currency").text(p2_currency);
	$("#rt_withdrawal_method").text(p2_withdrawal_method);
	$("#rt_ben_name").text(p3_beneficiary);
	$("#rt_ben_country").text(p3_country);
	$("#rt_ben_address1").text(p3_address1);
	$("#rt_ben_address2").text(p3_address2 + ' ');
	$("#rt_ben_city").text(p3_city);
	$("#rt_ben_state").text(p3_state + ' ');
	$("#rt_ben_zip").text(p3_zip + ' ');
	
	$("#rt_wire_part").hide();
	
	if (wdtype == 'WT')
	{
		$("#rt_destination").text(p2_bank_account_number);
		$("#rt_bank_name").text(p2_bank_name);
		$("#rt_bank_country").text(p2_country);
		$("#rt_bank_address1").text(p2_address1);
		$("#rt_bank_address2").text(p2_address2 + ' ');
		$("#rt_bank_city").text(p2_city);
		$("#rt_bank_state").text(p2_state + ' ');
		$("#rt_bank_zip").text(p2_zip + ' ');
		$("#rt_bank_swift").text(p2_swift);
		$("#rt_wire_part").show();
	}
	else if (wdtype == 'AA')
	{
		$("#rt_destination").text(p2_transfer_account);
	}
	else if (wdtype == 'LR')
	{
		$("#rt_destination").text(p2_destination_account);
	}
	else if (wdtype == 'WM')
	{
		$("#rt_destination").text(p2_destination_account);
	}
	else if (wdtype == 'MB')
	{
		$("#rt_destination").text(p2_destination_account);
	}
	
	$("#printed_name").text(p1_firstname + ' ' + p1_lastname);
	
	WithdrawalSend();
}

function WithdrawalSend()
{
	var param = '';
	if (wdtype == 'WT')
	{
		param = "&p1_account_type=" + p1_account_type + "&p1_trading_account=" + p1_trading_account + "&p1_firstname=" + p1_firstname;
		param += "&p1_middlename=" + p1_middlename + "&p1_lastname=" + p1_lastname + "&p1_email=" + p1_email + "&p2_bank_account_number=" + p2_bank_account_number;
		param += "&p2_bank_name=" + p2_bank_name + "&p2_corresponding_bank=" + p2_corresponding_bank + "&p2_country=" + p2_country + "&p2_address1=" + p2_address1;
		param += "&p2_address2=" + p2_address2 + "&p2_city=" + p2_city + "&p2_state=" + p2_state + "&p2_zip=" + p2_zip + "&p2_swift=" + p2_swift;
		param += "&p2_amount=" + p2_amount + "&p2_currency=" + p2_currency + "&p3_beneficiary=" + p3_beneficiary + "&p3_country=" + p3_country;
		param += "&p3_address1=" + p3_address1 + "&p3_address2=" + p3_address2 + "&p3_city=" + p3_city + "&p3_state=" + p3_state + "&p3_zip=" + p3_zip;
		param += "&p4_initials=" + p4_initials + "&p4_comments=" + p4_comments;
	}
	else if (wdtype == 'AA')
	{
		param = "&p1_account_type=" + p1_account_type + "&p1_trading_account=" + p1_trading_account + "&p1_firstname=" + p1_firstname;
		param += "&p1_middlename=" + p1_middlename + "&p1_lastname=" + p1_lastname + "&p1_email=" + p1_email + "&p2_transfer_account=" + p2_transfer_account;
		param += "&p2_amount=" + p2_amount + "&p2_currency=" + p2_currency + "&p3_beneficiary=" + p3_beneficiary + "&p3_country=" + p3_country;
		param += "&p3_address1=" + p3_address1 + "&p3_address2=" + p3_address2 + "&p3_city=" + p3_city + "&p3_state=" + p3_state + "&p3_zip=" + p3_zip;
		param += "&p4_initials=" + p4_initials + "&p4_comments=" + p4_comments;
	}
	else if (wdtype == 'LR')
	{
		param = "&p1_account_type=" + p1_account_type + "&p1_trading_account=" + p1_trading_account + "&p1_firstname=" + p1_firstname;
		param += "&p1_middlename=" + p1_middlename + "&p1_lastname=" + p1_lastname + "&p1_email=" + p1_email + "&p2_destination_account=" + p2_destination_account;
		param += "&p2_amount=" + p2_amount + "&p3_beneficiary=" + p3_beneficiary + "&p3_country=" + p3_country;
		param += "&p3_address1=" + p3_address1 + "&p3_address2=" + p3_address2 + "&p3_city=" + p3_city + "&p3_state=" + p3_state + "&p3_zip=" + p3_zip;
		param += "&p4_initials=" + p4_initials + "&p4_comments=" + p4_comments;
	}
	else if (wdtype == 'WM')
	{
		param = "&p1_account_type=" + p1_account_type + "&p1_trading_account=" + p1_trading_account + "&p1_firstname=" + p1_firstname;
		param += "&p1_middlename=" + p1_middlename + "&p1_lastname=" + p1_lastname + "&p1_email=" + p1_email + "&p2_destination_account=" + p2_destination_account;
		param += "&p2_amount=" + p2_amount + "&p2_currency=" + p2_currency + "&p3_beneficiary=" + p3_beneficiary + "&p3_country=" + p3_country;
		param += "&p3_address1=" + p3_address1 + "&p3_address2=" + p3_address2 + "&p3_city=" + p3_city + "&p3_state=" + p3_state + "&p3_zip=" + p3_zip;
		param += "&p4_initials=" + p4_initials + "&p4_comments=" + p4_comments;
	}
	else if (wdtype == 'MB')
	{
		param = "&p1_account_type=" + p1_account_type + "&p1_trading_account=" + p1_trading_account + "&p1_firstname=" + p1_firstname;
		param += "&p1_middlename=" + p1_middlename + "&p1_lastname=" + p1_lastname + "&p1_email=" + p1_email + "&p2_destination_account=" + p2_destination_account;
		param += "&p2_amount=" + p2_amount + "&p2_currency=" + p2_currency + "&p3_beneficiary=" + p3_beneficiary + "&p3_country=" + p3_country;
		param += "&p3_address1=" + p3_address1 + "&p3_address2=" + p3_address2 + "&p3_city=" + p3_city + "&p3_state=" + p3_state + "&p3_zip=" + p3_zip;
		param += "&p4_initials=" + p4_initials + "&p4_comments=" + p4_comments;
	}
	
	$.ajax(
	{
		type: 	"POST",
		url: 	"/ajax/withdrawal.php",
		data: 	"wdtype=" + wdtype + param,
		success:function(msg)
		{
			if (msg != 0)
			{
				$("#validation_code").text(msg);
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 603);
				setTimeout('WMenuShow(5)', 1000);
			}
			else
			{
				error = new Array();
				error.push('Internal error! Please try again later. If this error will repeated, please contact us!');
				ShowErrors(error);
			}
		}
	});
}

function CheckLRDestinationAccount(e)
{
	var str = $("#tbDestinationAccount").val();
	
	var keynum;
	var keychar;
	var numcheck;

    if (window.event) // IE
    {
		keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
		keynum = e.which;
    }

	if (keynum == 27) // Eescape
	{
		return false;
	}
	
	if (e.shiftKey) // Shift
	{
		return false;
	}

	if (keynum == 13) // Enter
	{
		return false;
	}

	keychar = String.fromCharCode(keynum);
	
	if (str.length > 0)
	{
		numcheck = /\d/;
		return numcheck.test(keychar)
				|| (keynum == null)
				|| (keynum == 8) // backspace
				|| (keynum == 46) // delete
				|| (keynum == 37) // left arrow
				|| (keynum == 39) // right arrow
				|| (keynum == 96) //numLock 0
				|| (keynum == 97) //numLock 1
				|| (keynum == 98) //numLock 2
				|| (keynum == 99) //numLock 3
				|| (keynum == 100) //numLock 4
				|| (keynum == 101) //numLock 5
				|| (keynum == 102) //numLock 6
				|| (keynum == 103) //numLock 7
				|| (keynum == 104) //numLock 8
				|| (keynum == 105) //numLock 9
				|| (keynum == 3) // numLock
				|| (keynum == 22) // numLock
				|| (keynum == 110) // numLock .
				|| (keynum == 9); // tab
	}
	else
	{
		//numcheck = /\D/;
		numcheck = /[a-zA-Z]/;
		if ((keynum > 95 && keynum < 112) || (keynum > 218 && keynum < 223) || (keynum > 187 && keynum < 193) || keynum == 59) return false;
		return numcheck.test(keychar)
				|| (keynum == 8) // backspace
				|| (keynum == 46) // delete
				|| (keynum == 37) // left arrow
				|| (keynum == 39) // right arrow
				|| (keynum == 9); // tab
	}
}

function CheckLRDestinationAccountString()
{
	var str = $("#tbDestinationAccount").val();
	var numcheck = /[a-zA-Z]{1}\d/;
	if (!numcheck.test(str)) $("#tbDestinationAccount").val('');
	if (str.length < 8) 
	{
		$("#tbDestinationAccount").css("background", "#FFBBBB");
		return false;
	}
	else $("#tbDestinationAccount").css("background", "#FFFFFF");
	return true;
}

/* ----------------------------------------- */

/* assign client to partner */
function AssignClientToPartner(clientid)
{
	var partnerid = $("#sPartners").val();
	var slogin = $("#slogin").val();
	var spassword = $("#spassword").val();
	
	if (partnerid != undefined && clientid != undefined && slogin != '' && spassword != '')
	{
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/assign_client_to_partner.php",
			data: 	"clientid=" + clientid + 
					"&partnerid=" + partnerid +
					"&slogin=" + slogin + 
					"&spassword=" + spassword,
			success:function(msg)
			{
				var answ = msg.split(':');
				AlertWin(answ[0], answ[1], null, null, 350, 175);
			}
		});
	}
	else
	{
		AlertWin("Введите параметры авторизации администратора", 'Ошибка', null, null, 350, 175);
	}
}

function ChangeCode()
{
	$("#security_code").animate({opacity: "hide"}, 300);

	$.ajax(
	{
		type: 	"POST",
		url: 	"/ajax/change_code.php",
		success:function(msg)
		{
			setTimeout('$("#security_code").html("' + msg + '")', 350);
			setTimeout('$("#security_code").animate({opacity: "show"}, 800)', 400);
		}
	});
}

function Send()
{
	var to = $('#sTo').val();
	var from = $('#tbFrom').val();
	var email = $('#tbEmail').val();
	var subject = $('#tbSubject').val();
	var question = $('#tbQuestion').val();
	var code = $('#tbCode').val();
	
	var error = '';
	
	if (code == '') error = ct_err_code;
	if (question == '') error = ct_err_question;
	if (subject == '') error = ct_err_subject;
	if (email == '' || !CheckValidEmail(email)) error = ct_err_email;
	if (from == '') error = ct_err_from;
	if (to == 0) error = ct_err_to;
	
	if (error == '')
	{
		$('#btSend').animate({opacity: "hide"}, 200);
		setTimeout('$("#img_loading").animate({opacity: "show"}, 200)', 200);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_code.php",
			data: 	"code=" + code,
			success:function(msg)
			{
				if (msg == 'false') ShowContactMessage(ct_err_code, '#CC0000');
				else
				{
					$('#contact_form').submit();
					return false;
				}
				
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 403);
				setTimeout('$("#btSend").animate({opacity: "show"}, 200)', 604);
			}
		});
	}
	else
	{
		ShowContactMessage(error, '#CC0000');
	}	
}

function ShowContactMessage(msg, color)
{
	$("#contact_error").html(msg);
	$("#contact_error").css('color', color);
	$("#contact_error").animate({opacity: "show"}, 500);
	setTimeout('$("#contact_error").animate({opacity: "hide"}, 1000)', 3000);
}

function RegisterDemo()
{
	var firstname = $('#tbFirstName').val();
	var lastname = $('#tbLastName').val();
	//var birthday = $('#tbBirthDate').val();
	var country = $('#sCountry').val();
	//var phone = $('#tbPhone').val();
	var email = $('#tbEmail').val();
	var currency = $('#sCurrency').val();
	var leverage = $('#sLeverage').val();
	var deposit = $('#tbDeposit').val();
	//var password = $('#password').val();
	//var confirm = $('#password_confirm').val();
	var code = $('#tbCode').val();
	
	var error = '';
	//var bd = birthday.split('.');
	
	if (code == '') error = oda_err_code;
	//if (password.length < 6 || password != confirm) error = oda_err_confirm;
	//if (password.length < 6) error = oda_err_password;
	if (deposit < 1) error = oda_err_deposit;
	//if (email == '' || !CheckValidEmail(email)) error = oda_err_email;
	//if (phone == '') error = oda_err_phone;
	//if (birthday == '' || bd.length != 3) error = oda_err_birthday;
	if (lastname == '') error = oda_err_lastname;
	if (firstname == '') error = oda_err_firstname;
	
	if (error == '')
	{
		$('#btRegister').animate({opacity: "hide"}, 200);
		setTimeout('$("#img_loading").animate({opacity: "show"}, 200)', 200);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_code.php",
			data: 	"code=" + code,
			success:function(msg)
			{
				if (msg == 'false') 
				{
					AlertWin(oda_err_code, p_msg, null, null, 350, 175);
					ShowContactMessage(oda_err_code, '#CC0000', null, null, 350, 175);
				}
				else
				{
					setTimeout("$('#open_demo_form').submit()", 1000);
					return false;
				}
				
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 403);
				setTimeout('$("#btRegister").animate({opacity: "show"}, 200)', 604);
			}
		});
	}
	else
	{
		AlertWin(error, p_msg, null, null, 350, 175);
		ShowContactMessage(error, '#CC0000');
	}
}

function RegisterWebReal()
{
	var firstname = $('#tbFirstName').val();
	var lastname = $('#tbLastName').val();
	if (oda_err_passportid != '') var passportid = $('#tbPassportID').val();
	var state = $('#tbState').val();
	var city = $('#tbCity').val();
	var address = $('#tbAddress1').val();
	var email = $('#tbEmail').val();
	var code = $('#tbCode').val();
	
	var error = '';
	
	if (code == '') error = oda_err_code;
	if (email == '' || !CheckValidEmail(email)) error = oda_err_email;
	if (address == '') error = oda_err_address;
	if (city == '') error = oda_err_city;
	if (state == '') error = oda_err_state;
	if (oda_err_passportid != '' && passportid == '') error = oda_err_passportid;
	if (lastname == '') error = oda_err_lastname;
	if (firstname == '') error = oda_err_firstname;
	
	if (error == '')
	{
		$('#btRegister').animate({opacity: "hide"}, 200);
		setTimeout('$("#img_loading").animate({opacity: "show"}, 200)', 200);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_code.php",
			data: 	"code=" + code,
			success:function(msg)
			{
				if (msg == 'false') 
				{
					AlertWin(oda_err_code, p_msg, null, null, 350, 175);
					ShowContactMessage(oda_err_code, '#CC0000');
				}
				else
				{
					setTimeout("$('#open_demo_form').submit()", 1000);
					return false;
				}
				
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 403);
				setTimeout('$("#btRegister").animate({opacity: "show"}, 200)', 604);
			}
		});
	}
	else
	{
		AlertWin(error, p_msg, null, null, 350, 175);
		ShowContactMessage(error, '#CC0000');
	}
}

function OnAgreeButton()
{
	if ($("#cbxAccept").attr("checked") == true) $("#btRegister").animate({"opacity" : "show"}, 200);
	else $("#btRegister").animate({"opacity" : "hide"}, 200);
}

/* partner form */

function BecomePartner()
{
	var firstname = $('#tbFirstName').val();
	var lastname = $('#tbLastName').val();
	var fathername = $('#tbFatherName').val();
	
	var password = $("#password").val();
	
	var birthday = $('#tbBirthDate').val();
	var address = $('#taAddress').val();
	var email = $('#tbEmail').val();
	var phone = $('#tbPhone').val();
	var organization = $('#taOrganization').val();
	var code = $('#tbCode').val();
	
	var error = '';
	var bd = birthday.split('.');
	
	if (code == '') error = bp_err_code;
	if (organization == '') error = bp_err_organization;
	if (phone == '') error = bp_err_phone;
	if (email == '' || !CheckValidEmail(email)) error = bp_err_email;
	if (address == '') error = bp_err_address;
	if (birthday == '' || bd.length != 3) error = bp_err_birthday;
	
	CheckPartnerLogin();
	if (password.length < 6) error = bp_err_password_short;
	else CheckPasswordConfirm();
	
	if (fathername == '') error = bp_err_fathername;
	if (lastname == '') error = bp_err_lastname;
	if (firstname == '') error = bp_err_firstname;
	
	if (error == '')
	{
		$('#btSend').animate({opacity: "hide"}, 200);
		setTimeout('$("#img_loading").animate({opacity: "show"}, 200)', 200);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_code.php",
			data: 	"code=" + code,
			success:function(msg)
			{
				if (msg == 'false') 
				{
					AlertWin(bp_err_code, p_msg, null, null, 350, 175);
					ShowContactMessage(bp_err_code, '#CC0000');
				}
				else
				{
					setTimeout("$('#become_partner_form').submit()", 1000);
					return false;
				}
				
				setTimeout('$("#img_loading").animate({opacity: "hide"}, 200)', 403);
				setTimeout('$("#btSend").animate({opacity: "show"}, 200)', 604);
			}
		});
	}
	else
	{
		AlertWin(error, p_msg, null, null, 350, 175);
		ShowContactMessage(error, '#CC0000');
	}
}

function CheckPartnerLogin()
{
	var login = $("#login").val();
	
	if (login != '' && login != undefined)
	{
		$("#login_msg").hide();
		$("#img_loading_login").animate({opacity:"show"}, 300);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_partner_login.php",
			data: 	"login=" + login,
			success:function(msg)
			{
				if (msg != '')
				{
					$("#img_loading_login").animate({opacity:"hide"}, 300);
					$("#login_msg").text(msg);
					$("#login_msg").css("color", "#FF5555");
					$("#login").css("border", "2px solid #FF5555");
					setTimeout('$("#login_msg").animate({opacity:"show"}, 500)', 600);
				}
				else
				{
					$("#login").css("border", "2px solid #55FF55");
					$("#img_loading_login").animate({opacity:"hide"}, 300);
				}
			}
		});
	}
	else
	{
		$("#login_msg").text(bp_err_nologin);
		$("#login_msg").css("color", "#FF5555");
		$("#login").css("border", "2px solid #FF5555");
		$("#login_msg").animate({opacity:"show"}, 500);
	}
}

function CheckPasswordConfirm()
{
	var pas = $("#password").val();
	var con = $("#password_confirm").val();
	
	if (pas.length == 0 || pas != con)
	{
		$("#confirm_msg").text(bp_err_password_confirm);
		$("#confirm_msg").css("color", "#FF5555");
		$("#password_confirm").css("border", "2px solid #FF5555");
		$("#confirm_msg").animate({opacity:"show"}, 500);
	}
	else
	{
		$("#confirm_msg").hide();
		$("#password_confirm").css("border", "2px solid #55FF55");
	}
}

/* header login account - submit form */

function LoginAccount()
{
	var login = $("#login_username").val();
	var pass = $("#login_password").val();
	
	if (login == undefined || login == '') AlertWin(lgn_login, gl_error, null, null, 350, 175);
	else if (pass == undefined || pass == '') AlertWin(lgn_pass, gl_error, null, null, 350, 175);
	else $("#IDLoginForm").submit();
}

function EnterAccountLogin()
{
	$("#login_password, #login_username").keydown(function(event)
	{
		switch (event.keyCode)
		{
			case 13: LoginAccount(); break;
		}
	});
}

/* functions */

function TruncateString(string, length)
{
    var etc = '...';
    if (length == 0) return '';
	
    if (string.length > length) 
    {
        length -= etc.length;
        
        if (string[length] != ' ')
        {
	        var str = string.substr(0, length);
	        for (i = length; i < string.length; i++)
	        {
	        	if (string[i] != ' ' && string[i] != undefined) str += string[i];
	        	else return str + etc;
	        }
	        return str + etc;
	    }
	    else return string.substr(0, length) + etc;
    } 
    else 
    {
        return string;
    }
}

function AlertWin(msg, title, ok, timer, swidth, sheight)
{
	title = (title != null && title != undefined) ? title : '!!!';
	swidth = (swidth != null && swidth != undefined) ? swidth : '350';
	sheight = (sheight != null && sheight != undefined) ? sheight : '175';
	
	$("body #AlertWindow").html(msg);
	$("body #AlertWindow").show();

	$("body #AlertWindow").dialog(
	{
		modal 			: true,
		closeOnEscape 	: true,
		dialogClass 	: 'alert_window',
		title 			: title,
		minWidth 		: 300,
		minHeight 		: 150,
		maxWidth 		: 600,
		maxHeight 		: 500,
		width 			: swidth,
		height 			: sheight,
		zIndex 			: 10000,
		buttons 		:
		{
			"Ok": function()
			{
				$(this).dialog("close");
				$("body #AlertWindow").hide();
				if (ok != null && ok != undefined) setTimeout(ok, 1);
			}
		},
		overlay: 
		{ 
			opacity 	: 0.5, 
			background 	: "black" 
		}
	});
	
	//var timer = (timer != null && timer != undefined && timer != '') ? timer : 10000;
	//setTimeout('$("body #AlertWindow").dialog("close")', timer);
}

function onlyNumbers(e)
{
	var keynum;
	var keychar;
	var numcheck;
	var ctrl = (document.all) ? event.ctrlKey : e.modifiers & Event.CONTROL_MASK;
	var shift = (document.all) ? event.shiftKey : e.modifiers & Event.CONTROL_MASK;

    if (window.event) // IE
    {
		keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
		keynum = e.which;
    }

	if (keynum == 27) // Eescape
	{
		return false;
	}
	
	if (keynum == 32) // Space
	{
		return false;
	}
	
	if (e.ctrlKey && (keynum == 67 || keynum == 86)) //CTRL+V (Paste) CTRL+C (Copy)
	{
		return true;
	}

	if (e.shiftKey) // Shift
	{
		return false;
	}

	if (keynum == 13) // Enter
	{
		return false;
	}

	if (ctrl && (keynum == 86)) // CTRL+V (Paste)
	{
		return false;
	}
	else if (ctrl && (keynum == 67)) // CTRL+C (Copy)
	{
		return false;
	}

	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar)
			|| (keynum == null)
			|| (keynum == 8) // backspace
			|| (keynum == 46) // delete
			|| (keynum == 37) // left arrow
			|| (keynum == 39) // right arrow
			|| (keynum == 96) //numLock 0
			|| (keynum == 97) //numLock 1
			|| (keynum == 98) //numLock 2
			|| (keynum == 99) //numLock 3
			|| (keynum == 100) //numLock 4
			|| (keynum == 101) //numLock 5
			|| (keynum == 102) //numLock 6
			|| (keynum == 103) //numLock 7
			|| (keynum == 104) //numLock 8
			|| (keynum == 105) //numLock 9
			//|| (keynum == 3) //numLock
			//|| (keynum == 22) //numLock
			|| (keynum == 110) //numLock .
			//|| (keynum == 190) // .
			//|| (keynum == 188) // ,
			|| (keynum == 9); // tab
}

function AddVote(qid)
{
	if (qid > 0)
	{
		var aid = $("#questionnaire_table input[name=rQuestionnaire]:checked").val();
		var loadimg = '<img src="/img/q_loading.gif" class="hide" id="q_loadimg" />';
		
		$("#questionnaire_con").append(loadimg);
		$("#questionnaire_table").animate({opacity: "hide"}, 300);
		setTimeout('$("#q_loadimg").animate({opacity: "show"}, 200)', 300);
		
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/q_vote.php",
			data: 	"qid=" + qid + 
					"&aid=" + aid,
			success:function(msg)
			{
				setTimeout('$("#q_loadimg").animate({opacity: "hide"}, 200)', 600);
				
				if (msg == 'false') 
				{
					setTimeout('$("#questionnaire_table").animate({opacity: "show"}, 300)', 800);
				}
				else
				{
					var html = '<div id="q_result_container" class="hide">' + msg + '</div>';
					var strss = "$(\"#questionnaire_con\").html('" + addslashes(html) + "');";
					var str_pat = ["\r", "\n", "\t"];
					var temp = str_replace(str_pat, "", strss);
					setTimeout(temp, 799);
					setTimeout('$("#q_result_container").animate({opacity: "show"}, 300)', 800);
				}
			}
		});
	}
}

function Revote(qid)
{
	if (qid > 0)
	{
		var loadimg = '<img src="/img/q_loading.gif" class="hide" id="q_loadimg" />';
		
		$("#questionnaire_con").append(loadimg);
		$("#questionnaire_table").animate({opacity: "hide"}, 300);
		setTimeout('$("#q_loadimg").animate({opacity: "show"}, 200)', 300);

		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/q_revote.php",
			data: 	"qid=" + qid,
			success:function(msg)
			{
				setTimeout('$("#q_loadimg").animate({opacity: "hide"}, 300)', 600);
				
				if (msg == 'false') 
				{
					setTimeout('$("#questionnaire_table").animate({opacity: "show"}, 300)', 800);
				}
				else
				{
					var html = '<div id="q_result_container" class="hide">' + msg + '</div>';
					var strss = "$(\"#questionnaire_con\").html('" + addslashes(html) + "');";
					var str_pat = ["\r", "\n", "\t"];
					var temp = str_replace(str_pat, "", strss);
					setTimeout(temp, 799);
					setTimeout('$("#q_result_container").animate({opacity: "show"}, 300)', 800);
				}
			}
		});
	}		
}

function NextQuestionnaire(qid)
{
	if (qid > 0)
	{
		var loadimg = '<img src="/img/q_loading.gif" class="hide" id="q_loadimg" />';
		
		$("#questionnaire_con").append(loadimg);
		$("#questionnaire_table").animate({opacity: "hide"}, 300);
		setTimeout('$("#q_loadimg").animate({opacity: "show"}, 200)', 300);

		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/q_next_questionnaire.php",
			data: 	"qid=" + qid,
			success:function(msg)
			{
				setTimeout('$("#q_loadimg").animate({opacity: "hide"}, 300)', 600);
				
				if (msg == 'false') 
				{
					setTimeout('$("#questionnaire_table").animate({opacity: "show"}, 300)', 800);
				}
				else
				{
					var html = '<div id="q_result_container" class="hide">' + msg + '</div>';
					var strss = "$(\"#questionnaire_con\").html('" + addslashes(html) + "');";
					var str_pat = ["\r", "\n", "\t"];
					var temp = str_replace(str_pat, "", strss);
					setTimeout(temp, 799);
					setTimeout('$("#q_result_container").animate({opacity: "show"}, 300)', 800);
				}
			}
		});
	}	
}

function ToLatin(obj) 
{
	if (/^[a-zA-Z0-9 ,.\-:"()/]*?$/.test(obj.value)) obj.defaultValue = obj.value;
	else obj.value = obj.defaultValue;
}

function ToLatinCap(obj)
{
    obj.value = obj.value.replace('  ', ' ');
    var spaceArray = obj.value.split(' ');
	for (var i in spaceArray)
	{
	    var hyphenArray = spaceArray[i].split('-');
	    for (var j in hyphenArray) hyphenArray[j] = Capitalize(hyphenArray[j]);
	    spaceArray[i] = hyphenArray.join('-');
	}
	
	obj.value = spaceArray.join(' ');
	var reg = /^(\-)?[a-zA-Z -]*?$/;
	if (reg.test(obj.value)) obj.defaultValue = obj.value;
	else obj.value = obj.defaultValue;
}

function Capitalize(value)
{
	var first = value.substr(0,1).toUpperCase();
	var last = value.substr(1).toLowerCase();
	return first + last;
}

function CheckDate(obj)
{
	if (/^[0-9]{2}\.[0-9]{2}\.[0-9]{4}$/.test(obj.value)) obj.defaultValue = obj.value;
	else obj.value = obj.defaultValue;
}

function CheckEmail(obj)
{
	if (!CheckValidEmail(obj.value)) obj.value = obj.defaultValue;
}


function OnChangeAccountTypes()
{
	if ($("#sAccountTypes").val() == 'Standard') $("#TRIDSwaps").animate({'opacity':'show'}, 200);
	else $("#TRIDSwaps").animate({'opacity':'hide'}, 200);
}


function CheckPassportID(obj)
{
	var pasid = obj.value;
	$("#tbPassportID").css("border", "2px solid #FFFF33");

	if (pasid != '')
	{
		$.ajax(
		{
			type: 	"POST",
			url: 	"/ajax/check_passport_id.php",
			data: 	"pasid=" + pasid,
			success:function(msg)
			{
				if (msg != '') 
				{
					$("#tbPassportID").css("border", "2px solid #FF3333");
					obj.value = obj.defaultValue;
					AlertWin(msg, gl_error, null, null, 350, 175);
				}
				else
				{
					$("#tbPassportID").css("border", "2px solid #33FF33");
				}
			}
		});
	}
	else
	{
		$("#tbPassportID").css("border", "2px solid #FF3333");
	}
}

function OnlyDigits(e)
{
	var keynum;
	var keychar;
	var numcheck;
	var ctrl = (document.all) ? event.ctrlKey : e.modifiers & Event.CONTROL_MASK;
	var shift = (document.all) ? event.shiftKey : e.modifiers & Event.CONTROL_MASK;

    if (window.event) // IE
    {
		keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
		keynum = e.which;
    }

	if (keynum == 27) // Eescape
	{
		return false;
	}
	
	if (e.shiftKey) // Shift
	{
		return false;
	}

	if (keynum == 13) // Enter
	{
		return false;
	}

	if (ctrl && (keynum == 86)) // CTRL+V (Paste)
	{
		return false;
	}
	else if (ctrl && (keynum == 67)) // CTRL+C (Copy)
	{
		return false;
	}

	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar)
			|| (keynum == null)
			|| (keynum == 8) // backspace
			|| (keynum == 46) // delete
			|| (keynum == 37) // left arrow
			|| (keynum == 39) // right arrow
			|| (keynum == 96) //numLock 0
			|| (keynum == 97) //numLock 1
			|| (keynum == 98) //numLock 2
			|| (keynum == 99) //numLock 3
			|| (keynum == 100) //numLock 4
			|| (keynum == 101) //numLock 5
			|| (keynum == 102) //numLock 6
			|| (keynum == 103) //numLock 7
			|| (keynum == 104) //numLock 8
			|| (keynum == 105) //numLock 9
			|| (keynum == 9); // tab
}

function MagicBlink()
{
	var duration = 1000;
	var fix_time = 100;

	$(".magic-blink").animate({color: "#FFFFFF"}, duration);
	setTimeout('$(".magic-blink").animate({color: "#3582CD"}, ' + duration + ')', duration + fix_time);
	setTimeout('$(".magic-blink").animate({color: "#5EEE2F"}, ' + duration + ')', (duration * 2) + (fix_time * 2));
	setTimeout('$(".magic-blink").animate({color: "#E74907"}, ' + duration + ')', (duration * 3) + (fix_time * 3));
	setTimeout('MagicBlink()', (duration * 4) + (fix_time * 4));
}