var transf_width=730;
var xmlHttp;

function addValue(id)
{
	if (document.getElementById("filename_"+id).value != '')
	{
		objid=new Number(id);
		if (document.getElementById("filename_"+(objid+1))==undefined)
		{
			nrord=objid+1;
			
			$('#file_inputs').append('<div class="spacer_xm"></div><div style="position:relative; margin-left:0px;"><input name="upload_file[]" type="file" size="39" class="file" style="width:330px; height: 30px; cursor:pointer;" id="filename_' + nrord + '" onclick="addValue(' + nrord + ')" onchange="changeFileContentValue(' + nrord + '); addValue(' + nrord + ');"  /><div style="position: absolute; top: 0px; left: 0px; z-index: 1;"><div style="width:265px; float:left;"><input name="" id="filecontent_' + nrord + '" type="text" class="inputStyle" style="width:245px;" /></div><div style="float:left; padding-top:3px;"><a href="#" title=""><img src="/includes/images/buttons/btn_browse.gif" border="0" alt="" /></a></div></div></div>');
		}
	}
}

function changeFileContentValue(id)
{
	document.getElementById("filecontent_"+id).value = document.getElementById("filename_"+id).value;
}

// Functions used for address book
addresses=new Array();

function vab(ch)
{
	if (ch.checked==true)
	{
		checked++;

		if (checked <= 5)
			addresses.push(ch.value);
		else
			{
			alert('Ati selectat deja 5 adrese!');
			checked--;
			ch.checked=false;
			return;
			}
	}
	else
	{
		checked--;
		for (i=0;i<addresses.length;i++)
			if (addresses[i]==ch.value)
				{
				for (j=i;j<addresses.length-1;j++)
					addresses[j]=addresses[j+1];
				addresses.pop()
				break;
				}
	}
}

function sab()
{
	checked=0;
	text=document.getElementById('send_to').value;
	document.getElementById("send_to").value = '';
	text=trim(text);
	if (text.length>1)
		text += '\r\n';
	for(i=0;i<addresses.length;i++)
		text+=addresses[i]+'\n';
	document.getElementById('send_to').value=text;
	addresses=new Array();
	abook(0);
	
}


function abook(sh)
{
	 	
	 if (sh==1)
	 {
		var myWidth = 0, myHeight = 0;
		 if( typeof( window.innerWidth ) == 'number' )
			 {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
			 }
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
				{
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
				 }
			else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
					{
					//IE 4 compatible
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
					}
		
		x=myWidth/2-500/2;
		y=myHeight/2-450/2;

		//document.getElementById('body').className='body2';
		document.getElementById('address_book').style.left=x+'px';
		document.getElementById('address_book').style.top=y+'px';
		document.getElementById('address_book').style.display='block';

		if (document.getElementsByName('address[]'))
		{
		elem= document.getElementsByName('address[]');
		for(i=0;i<elem.length;i++)
			document.getElementById(elem[i].id).checked=false;
		}
		addresses=new Array();
	 }
	 else
	{
		$('#address_book').dialog( "close" );
	}
}


function getEmails()
{
	var emails = new Array();
	emailsTextArea = document.getElementById('send_to').value;
	emails = emailsTextArea.split("\n");
	var emailsString = '';
	
	for(var i = 0; i < emails.length; i++)
	{
		emailsString += emails[i] + ';';
	}
	
	return emailsString;
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function trim(str) {
	return ltrim(rtrim(str));
}

// END functions used for address book

// Overlay start
var isOpen = false;
function showOverlayBox() {
	if( isOpen == false ) return;
	
	$('.transfContent').css({
		display:'block',
		left:( $(window).width() - $('.transfContent').width() ) / 2,
		top:( $(window).height() - $('.transfContent').height() ) / 2 -20,
		position:'absolute'
	});
	$('.transf').css({
		display:'block',
		width: $('.white_bg').width(),
		height: $('.white_bg').height()
	});
	$('#progressiframe').css({
		display:'block'
	});
}

function doOverlayOpen() {
	isOpen = true;
	showOverlayBox();
	$('.transf').css({opacity:0}).animate( {opacity:0.5} );
	return false;
}
function doOverlayClose() {
	isOpen = false;
	$('.transfContent').css( 'display', 'none' );
	$('.transf').css({opacity:0}).animate( {opacity:0} );
	$('.transf').css( 'display', 'none' );
}
$(window).bind('resize',showOverlayBox);
// end overlay

// Price calculator
function calculate_price(eur) {
	pachet = $('input:radio[class=radioPackage]:checked').val();
	perioada = $('input:radio[class=radioPeriod]:checked').val();
	
	if(pachet != '' && perioada != '') {
		if(pachet == 1) {
			if(perioada == 0) {
				noua_perioada = 1;
				discount = 0;
				calcul = noua_perioada * 3  * eur;
				new_price = calcul;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 1) {
				noua_perioada = 3;
				discount = 10 / 100;
				calcul = noua_perioada * 3  * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 2) {
				noua_perioada = 6;
				discount = 15 / 100;
				calcul = noua_perioada * 3  * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 3) {
				noua_perioada = 12;
				discount = 20 / 100;
				calcul = noua_perioada * 3  * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			
			new_price = new_price.toFixed(2);
		} 
		
		if(pachet == 6) {
			if(perioada == 0) {
				noua_perioada = 1;
				discount = 0;
				calcul = noua_perioada * 10 * eur;
				new_price = calcul;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 1) {
				noua_perioada = 3;
				discount = 10 / 100;
				calcul = noua_perioada * 10 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 2) {
				noua_perioada = 6;
				discount = 15 / 100;
				calcul = noua_perioada * 10 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 3) {
				noua_perioada = 12;
				discount = 20 / 100;
				calcul = noua_perioada * 10 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			new_price = new_price.toFixed(2);
		}
		
		if(pachet == 3) {
			if(perioada == 0) {
				noua_perioada = 1;
				discount = 0;
				calcul = noua_perioada * 12 * eur;
				new_price = calcul;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 1) {
				noua_perioada = 3;
				discount = 10 / 100;
				calcul = noua_perioada * 12 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 2) {
				noua_perioada = 6;
				discount = 15 / 100;
				calcul = noua_perioada * 12 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			if(perioada == 3) {
				noua_perioada = 12;
				discount = 20 / 100;
				calcul = noua_perioada * 12 * eur;
				new_price = calcul - calcul * discount;
				new_price = (new_price + ((new_price * 24) /100));
			}
			new_price = new_price.toFixed(2);
		}
		
		$('#total_plata').html('<font color="#000000">' + new_price + '</font> lei. Discount: <font color="#000000">' + discount * 100 + '%</font>');
	}
}
// End price calculator

// Check radios and divs for payment page

function checkOption(option)
{
	document.getElementById('card').style.display='none';
	document.getElementById('sms').style.display='none';
	document.getElementById('op').style.display='none';
	document.getElementById(option).className='checked';
	document.getElementById(option).style.display='block';
}

function checkOptionChecked(option)
{
	document.getElementById('start').style.display='none';
	document.getElementById('pro').style.display='none';
	//document.getElementById('pro+').style.display='none';
	document.getElementById(option).className='checkedPackage';
	document.getElementById(option).style.display='block';
}

function checkOptionPackage(option)
{
	if(option == 'start')
	{
		document.getElementById('start-div').style.background ='';
		document.getElementById('pro-div').style.background ='#FFF';
		//document.getElementById('pro+-div').style.background ='#FFF';
	}
	else if(option == 'pro')
	{
		document.getElementById('start-div').style.background ='#FFF';
		document.getElementById('pro-div').style.background ='';
		//document.getElementById('pro+-div').style.background ='#FFF';
	}
	else if(option == 'pro+')
	{
		document.getElementById('start-div').style.background ='#FFF';
		document.getElementById('pro-div').style.background ='#FFF';
		//document.getElementById('pro+-div').style.background ='';
	}

	document.getElementById(option + '-div').style.display='block';
	document.getElementById(option + '-div').className='backgroundChange';
}

function selectRadio(objDivID) {
	document.getElementById(objDivID).checked=true;
}
// END check radio

function convertFileSize(filesize) {
	var converted_filesize = Math.round(filesize / 1024 * 100) * .01;
	var suffix = 'KB';
	if (converted_filesize > 1000) {
		converted_filesize = Math.round(converted_filesize *.001 * 100) * .01;
		suffix = 'MB';
	}
	if (converted_filesize > 1000) {
		converted_filesize = Math.round(converted_filesize *.001 * 100) * .01;
		suffix = 'GB';
	}
	var sizeParts = converted_filesize.toString().split('.');
	if (sizeParts.length > 1) {
		converted_filesize = sizeParts[0] + '.' + sizeParts[1].substr(0,2);
	} else {
		converted_filesize = sizeParts[0];
	}

	return converted_filesize+suffix;
}
