var handleSuccess = function(o)
{
	if (o.responseText !== undefined)
	{
		var t = o.responseText.replace(/\r\n/g, '');
		try
		{
			eval(t);
		}
		catch(e)
		{
//			alert('Error. Please try again later or put in contact with us');
			alert('Response couldn\'t be decoded, response text: \r\n' + o.responseText + '\r\n' + 'Error: '+ e.messege  );
			return;
		}
	}
};

var handleFailure = function(o)
{

	if (o.responseText !== undefined)
	{
		div = "<li>Transaction id: " + o.tId + "</li>";
		div += "<li>HTTP status: " + o.status + "</li>";
		div += "<li>Status code message: " + o.statusText + "</li>";
		alert(div);
	}
};

var callback =
{
  success: handleSuccess,
  failure: handleFailure,
  argument: []
};
	
var callbackup =
{
	upload: handleSuccess,
	failure: handleFailure,
	argument: []
};

function doRequest(method, url, postParams, callback)
{
	glue = '?';
	if (url.indexOf('?') >= 0)
	{
		glue = '&';
	}
	var d = new Date();
	
	url += (glue + 'time=' + Date.parse(d.toString()));
	YAHOO.util.Connect.asyncRequest(method, url, callback, postParams);
}

function asyncImageLibrary()
{
	doRequest('GET', '/async/getlibrary.php?from=1', '', callback);
}

function asyncGetLibraryPopUp()
{
	doRequest('GET', '/async/getlibrarypop.php?from=3', '', callback);
}


function asyncGetFotologPopUp()
{
	doRequest('GET', '/async/getfotologpop.php?from=3', '', callback);
}

function asyncGetRecentPopUp()
{
	doRequest('GET', '/async/getrecentpop.php?from=1', '', callback);
}

function asyncListPrices()
{
	doRequest('GET', '/async/pricelist.php?from=1', '', callback);
}

function asyncUploadForm()
{
	doRequest('GET', '/async/getuploadform.php', '', callback);
}

function asyncRecent()
{
	doRequest('GET', '/async/getrecent.php?from=1', '', callback);
}

function asyncDeleteAddress(id)
{
	doRequest('POST', '/async/deleteaddress.php', 'id='+id, callback);
}

function asyncGetAddressPopUp()
{
	doRequest('GET', '/async/getaddresspopup.php', '', callback);
}

function asyncGetCsvPopUp(step)
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/getcsvpopup.php?step=' + step, '', callbackup);
}


function asyncGetHelpPopUp()
{
	doRequest('GET', '/async/gethelppopup.php', '', callback);
}

function asyncFotolog()
{
	doRequest('GET', '/async/getfotolog.php?from=3', '', callback);
}

function asyncFacebook()
{
	doRequest('GET', '/async/getalbumfacebook.php?', '', callback);
}

function asyncCheckSessionComplete(contest,idcurrentcard,editoldpostcard,posicion)
{
	doRequest('GET', '/async/checksession.php?contest='+contest + '&cards='+totalpostcards + '&id='+idcurrentcard + '&idpostcard='+editoldpostcard+ '&ultimo='+ultimo2 + '&posicion='+posicion,'', callback);
}

function asyncResamplePhoto(url)
{
	if (contest == 1)
	{
		doRequest('GET', '/async/resample.php?url='+url.Base64Encode() + '&destwidth=315&destheight=250&contest=1&idphoto=true', '', callback);
	}
	else
	{
		if (create_2)
		{
			doRequest('GET', '/async/resample.php?url='+url.Base64Encode() + '&destwidth=313&destheight=424&contest=0', '', callback);	
		}
		else
		{
			doRequest('GET', '/async/resample.php?url='+url.Base64Encode() + '&destwidth=422&destheight=310&contest=0&idphoto=true', '', callback);
		}
	}
}

//function asyncResamplePhoto2(url)
//{
//	doRequest('GET', '/async/resample.php?url='+url.Base64Encode() + '&destwidth=190&destheight=137&postcard=1', '', callback);
//}

function asyncClearRegistration(url)
{
	$('txtName').value = '';
	$('txtEmail').value = '';
	$('txtPass').value = '';
	$('txtPassConfirm').value = '';
	$('drpCountries').value = '';
	$('drpAgeDay').value = '1';
	$('drpAgeMonth').value = '1';

	$('drpGender').value = '1';
	$('txtPassConfirm').style.background = 'white';
	$('txtName').style.background = 'white';
	$('txtEmail').style.background = 'white';
	$('txtPass').style.background = 'white';
	$('txtPassConfirm').style.background = 'white';
	$('error').innerHTML = '&nbsp;';
	var myDate=new Date();
	$('drpAgeYear').value = myDate.getFullYear();
	return;
}

function asyncSubmitUploadForm()
{
	$('errormessage').innerHTML = '&nbsp;';
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/uploadfile.php', '', callbackup);
}

function asyncSubmitLoginForm(aftercall)
{
	 $('errormessage').innerHTML = '&nbsp;';
	 
	 if ($('username').value && $('password').value)
	 {
	 
		 var source;
		
		if ($('source'))
		{
			source = $('source').value;
		}
		 else
		 {
			if ($('source1').checked)
			{
				source = 'IMAP ';
			}
			else
			{
				source = 'FOT';
			}
		 }
	
	 YAHOO.util.Connect.setForm($('xform'), true, true);
	 doRequest('POST', '/async/login.php','', callbackup);
	 }
}



function asyncSubmitLoginFormAddress(addressBook)
{
	$('errormessage').innerHTML = '&nbsp;';

	var source;

	if ($('source'))
	{
		source = $('source').value;
	}
	else
	{
		if ($('source1').checked)
		{
			source = 'IMAP  ';
		}
		else
		{
			source = 'FOT';
		}
	}
	
	addressBook = addressBook == false ? 0 : 1;
   
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/loginpop.php?addressbook=' + addressBook,'', callbackup);
	
}

function asyncSubmitLoginMassSendingFormAddress(csv)
{
	$('errormessage').innerHTML = '&nbsp;';

	var source;

	if ($('source'))
	{
		source = $('source').value;
	}
	else
	{
		if ($('source1').checked)
		{
			source = 'IMAP  ';
		}
		else
		{
			source = 'FOT';
		}
	}
	
   csv = csv == false ? 0 : 1;
   
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/loginpop.php?csv=' + csv+'&masssending=1','', callbackup);
	
}

function asyncSubmitLoginFormCheckout()
{
	$('errormessage').innerHTML = '&nbsp;';
	
	if ($('username').value && $('password').value)
	{
		YAHOO.util.Connect.setForm($('xform'), true, true);
		doRequest('POST', '/async/loginchk.php?rturnuri='+$('rturnuri').value,'', callbackup);
	}
}

function asyncUpdateCurrentPostcardImage(type, url, format, color, rotate, returnuri,frame,regionsquare)
{
	var urltogive = '';
	
	if (url)
	{
		urltogive = '&url='+url.Base64Encode();
	}

	var top = regionsquare[0];
	var left = regionsquare[1];
	var width = regionsquare[2];
	var height = regionsquare[3];
	
	doRequest('POST', '/async/updatecurrentimage.php?', 
	'type='+type+urltogive+'&format='+format+'&color='+color+'&rotate='+rotate+'&frame='+frame+'&returnuri='+returnuri.Base64Encode() + '&top=' + top + '&left=' + left + '&width=' + width + '&height=' + height, 
	callback);
}

function asyncUpdateCurrentPostcardMessage(postcards,primerpostcard)
{
	addresses = new Array();
	
	if ($('moreaddresses') && totaladdresses != 0)
	{
		for (x = 1 ; x<=totaladdresses;x++)
		{
			if ($('address'+x))
			{
				z= { 'address':  ($('address'+x).value),
					'country' : ($('country'+x).value)
					}
			
				addresses.push(z);
			}
		}
		
		doRequest('POST', '/async/updatecurrentmessage.php', 
		 'postcards='+postcards+'&cards='+totalpostcards + '&ultimo='+ultimo + '&primero='+primerpostcard + '&moreaddresses='+ YAHOO.lang.JSON.stringify(addresses), 
		callback);
	}
	else
	{
		doRequest('POST', '/async/updatecurrentmessage.php', 
	 'postcards='+postcards+'&cards='+totalpostcards + '&ultimo='+ultimo + '&primero='+primerpostcard, 
		callback);
	}
}

function asyncUpdateContestPoscardMessage(bodytext, address, country, returnuri, name, email,font,x)
{
	doRequest('POST', '/async/updatecurrentmessage.php', 
	'bodytext='+escape(encodeURI(bodytext))+'&address='+escape(encodeURI(address))+'&country='+country+'&returnuri='+returnuri.Base64Encode() + '&name=' +name + '&email=' + email + '&font='+ font + '&cards='+totalpostcards + '&id='+x,
	callback);
}

function asyncContest(id)
{
	if (($('txtName').value) && ($('txtLastName').value) &&   ($('txtEmail').value))  
	{
		doRequest('POST', '/async/insertcontest.php?', 'name=' + $('txtName').value + '&lstname=' + $('txtLastName').value + '&mail=' + $('txtEmail').value + '&url=' + $('url').value + '&id=' + $('iduser').value + '&key=' + id, callback);
	}
	else
	{
		doRequest('GET','/async/validatecontest.php?&error=1','',callback);
	}
}

function asyncContestcart(id)
{
	doRequest('GET', '/async/cartcontest.php?id='+ id,'', callback);
}

function asyncWhithoutLogin()
{
	if ($('txtEmail').value)
	{
		doRequest('POST','/async/withoutlogin.php?','email=' + $('txtEmail').value+ '&rturnuri='+ $('rturnuri').value,callback);
	}
}

function asyncClearContest()
{
	doRequest('POST','/async/clearcontest.php','',callback);
}

function asyncClearPartners()
{
	doRequest('POST','/async/clearpartners.php','',callback);
}

function asyncClearWithoutlogin()
{
	doRequest('POST','/async/clearwithoutlogin.php','',callback);
}

//function asyncResamplePhoto3(url,key,step)
//{
//	doRequest('GET', '/async/resamplecart.php?url='+url.Base64Encode() + '&destwidth=153&destheight=115&key='+key + '&format='+step1_ImageFormat+'&urlencode='+base64_encode(step1_selectedphotourl)+'&step='+step, '', callback);
//}

function asyncCambiarIdioma(script,language)
{
	doRequest('GET','/async/cambiaridioma.php?script='+script+'&language='+language,'',callback);
}

function asyncPartnerRegistro()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/registropartner.php?','',callbackup);
}

function asyncPaginador(pag,id)
{
	doRequest('GET','/async/pagerpartner.php?pagina='+pag+'&id='+id,'',callback);
}

function asyncFiltroPartner(id,pagina,fechadesde,fechahasta,country,user)
{
	var from= fechadesde;
	var to = fechahasta;
	
	doRequest('GET','/async/pagerpartnerfilter.php?id='+id + '&user=' + user + '&country=' + country + '&from=' + from + '&to=' + to + '&pagina='+pagina,'',callback);
	
}
function asyncValidarCsv(archivo)
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/validarcsv.php', '', callbackup);
}

function uploadCsvAddress()
{
	addresses = new Array();
	
	YAHOO.util.Connect.setForm($('xform'), true, true);
	if ($('moreaddresses') && totaladdresses != 0)
	{
		for (x = 1 ; x<=totaladdresses;x++)
		{
			if ($('address'+x))
			{
				z= { 'address':  ($('address'+x).value),
					'country' : ($('country'+x).value)
					}
			
				addresses.push(z);
			}
		}

		doRequest('POST', '/async/uploadcsvaddress.php?cantidad='+$('cantidadaddresses').value + '&addresses=' + YAHOO.lang.JSON.stringify(addresses), '', callbackup);
	}
	else
	{
		doRequest('POST', '/async/uploadcsvaddress.php?cantidad='+$('cantidadaddresses').value, '', callbackup);
	}
	
}

function asyncClonarPostcards(name)
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/clonarpostcards.php?name='+ name, '', callbackup);
}

function asyncGetAlbumFacebook()
{
	doRequest('POST', '/async/getalbumfacebook.php?', '', callback);
}

function asyncGetPhotoFacebook(id)
{
	doRequest('GET', '/async/getphotosfacebook.php?id='+id, '', callback);
}

function asyncCrearAlbumFacebook()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/crearalbumfacebook.php?','', callbackup);
}

function asyncNuevoAlbumFacebook()
{
	doRequest('POST','/async/nuevoalbumfacebook.php?','', callback);
}

function asyncUploadPhotos()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/formupload.php?','', callbackup);
}

function asyncCargarImagen()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/formcargar.php?','', callbackup);
}


function asyncSubirImagen()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/guardarimagen.php?','idalbun='+ $('idalbum').value, callbackup);
}

function asyncNewrecipient(text,country,address,font)
{
	doRequest('GET', '/async/newcard.php?idcard=' + totalpostcards + '&text='+ escape(text) + '&country=' + country + '&address=' + escape(address) + '&primero='+primero + '&font='+font , '', callback);
}

function asyncAddDestinatarios()
{
	doRequest('GET', '/async/adddestinatarios.php' , '', callback);
}

function asyncDeleteRecipient()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/deleterecipient.php?totalpostcards='+totalpostcards + '&cantidadpostcards=' + cantidadpostcards +'&postales='+postales ,'', callbackup);
}

function asyncModificarDatos()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/modificarperfil.php?' , '', callbackup);
}

function asyncDeleteCurrentPostcard()
{
	doRequest('GET', '/async/borrarsession.php?', '', callback);
}

function asyncAffiliateMethod(id)
{
	doRequest('GET', '/async/affilitemethod.php?idmethod='+id, '', callback);
}

function asyncPaymentMethod(idmethod)
{
	code = false;

	if (validcode)
	{
		code = $('hiddenpromotioncodenumber').value;
	}
	
	doRequest('POST', '/async/changepaymentmethod.php','idmethod=' + idmethod + '&idtransaction=' + $('idtransaction').value + '&promotioncode='+ code, callback);
}

function asyncPopAddress()
{
	doRequest('GET', '/async/popaddress.php?', '', callback);
}

function asyncAddAddress()
{
	doRequest('GET', '/async/addaddress.php?txtaddress1='+base64_encode($('txtaddress1').value) + '&txtaddress2=' +base64_encode($('txtaddress2').value) + '&txtaddress3=' +base64_encode($('txtaddress3').value) + '&txtaddress4=' + base64_encode($('txtaddress4').value) + '&txtaddress5=' + base64_encode($('txtaddress5').value) + '&country='+ $('drpCountry').value, '', callback);
}


function asyncWindowForgetPassword()
{
	var url = document.referrer.split('/');
	
	doRequest('GET', '/async/windowforgetpassword.php?url='+url[count(url)-2], '', callback);
}

function asyncWindowForgetPasswordPhotoselection()
{
	doRequest('GET', '/async/windowforgetpassword.php?url=Photo_Selection', '', callback);
}

function asynRestorePassword(url)
{
	doRequest('GET', '/async/restorepassword.php?email='+$('txtemail').value + '&url='+url, '', callback);
}

function asyncUpdatePassword(url)
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST', '/async/updatepassword.php?url='+url,'', callbackup);
}

function asyncClearTransaction(transaction)
{
	doRequest('GET', '/async/checkoutredirect.php?idtransaction='+transaction, '', callback);
}

function asyncClearAllTransaction(uri)
{
	doRequest('GET', '/async/cleartransaction.php?idtransaction='+$('idtransaction').value + '&uri='+base64_encode(uri), '', callback);
}

function asyncGotoGallery()
{
	doRequest('GET', '/async/gotogallery.php', '', callback);
}

function asyncTest()
{
	var url = '';
	d = document.getElementsByTagName( "input" );
	
	for (i = 0; i < d.length; i++) {
		
		if (d[i].type == "text" && d[i].value != '') {
			url += '&value'+i+'=' + d[i].value * d[i].name + '&cant'+i+'=' + d[i].value + '&';
		}
	}
	if(url == '')
	{
		return false;
	}
	doRequest('POST', '/async/test.php', url, callback);
}


function asyncMask(nro)
{
	doRequest('GET', '/async/changemask.php?nro='+nro, '', callback);
}

function asyncPaymentMethodForHazelBucks(idmethod)
{
	var item;
	var idhazelcreditsbulk;
	
	radiobuttons = document.getElementsByName('idhazelcreditsbulk');
	var itemsamount = count(radiobuttons) - 1;
	
	for (var x = 0; x < itemsamount;x++)
	{
		if (isset(radiobuttons[x]))
		{	
		    if (radiobuttons[x].checked)
	    	{
	    		idhazelcreditsbulk = radiobuttons[x].value;
	    	}
	    	radiobuttons[x].disabled = true;
	    }
	}
	
	doRequest('POST', '/async/changepaymentmethodhazelbucks.php','idmethod='+idmethod + '&idhazelcreditsbulk='+idhazelcreditsbulk, callback);
}

function asynchideshowform(value)
{
	doRequest('GET', '/async/showform.php?value='+value, '', callback);
}

function asyncSelectFotologPhoto(url)
{
	doRequest('GET', '/async/selectfotologimage.php?url='+url, '', callback);
}

function asyncGetAddressPopUpMassSending()
{
	doRequest('GET', '/async/getaddresspopupmasssending.php', '', callback);
}

function asyncAddressFromMassSending(addresses)
{
	doRequest('GET', '/async/useaddressfrommasssending.php?addresses='+addresses, '', callback);
}

function asyncdeleteRealPostcardFromMassSending(idpostcard)
{
	doRequest('GET', '/async/deletepostcardfrommasssending.php?idpostcard='+idpostcard, '', callback);
}

function asyncdeleteAddressFromMassiveSend(id)
{
	doRequest('GET', '/async/deletepostcardfrommasssending.php?idsession='+id, '', callback);
}

function asyncEditAddress(id)
{
	doRequest('GET', '/async/popaddress.php?idaddress='+id, '', callback);
}

function asyncEditAddressById(id)
{
	doRequest('GET', '/async/addaddress.php?txtaddress1='+base64_encode($('txtaddress1').value) + '&txtaddress2=' +base64_encode($('txtaddress2').value) + '&txtaddress3=' +base64_encode($('txtaddress3').value) + '&txtaddress4=' + base64_encode($('txtaddress4').value) + '&txtaddress5=' + base64_encode($('txtaddress5').value) + '&country='+ $('drpCountry').value + '&idaddress=' +id, '', callback);
}

function asyncChangeThemeCombo(idtheme)
{
	doRequest('GET', '/async/changecombothemes.php?idtheme='+idtheme, '', callback);
}

function asyncFacebookLogin(id)
{
	doRequest('POST', '/async/facebookconnection.php','id='+ id, callback);
}

function asyncLoginWithProgramAward()
{
	$('errormessage').innerHTML = '&nbsp;';
	
	if ($('username').value && $('password').value)
	{
		var source = 'IMAP';
		
		YAHOO.util.Connect.setForm($('xform'), true, true);
		doRequest('POST', '/async/loginchk.php?rturnuri='+$('rturnuri').value+'&idprogramaward='+$('idprogramaward').value + '&source='+source,'', callbackup);
	}
}

function asyncMoveFramesToLeft()
{
	marcoframeoffset = $('marcoframeoffset').value;
	marcoframeoffset--;
	idtheme = $('drpThemes').value;
	doRequest('GET', '/async/marcospager.php?marcoframeoffset='+marcoframeoffset+ '&idtheme='+idtheme, '', callback);
}


function asyncMoveFramesToRight()
{
	marcoframeoffset = $('marcoframeoffset').value;
	marcoframeoffset++;
	idtheme = $('drpThemes').value;
	doRequest('GET', '/async/marcospager.php?marcoframeoffset='+marcoframeoffset+ '&idtheme='+idtheme, '', callback);
}

function asyncSendCkbOpt()
{
	doRequest('GET', '/async/updateuserwithnewsletter.php','', callback);
}

function asyncLonelyPlanetForm()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/getpartnersgalleryform.php?','',callbackup);
}

function asyncChangeGalleryCategories(combo)
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/changepartnersgallerycombos.php?combo='+combo,'',callbackup);
}

function asyncSetPopPartnersGalleries()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/setpopuppartnersgalleries.php?','',callbackup);
}

function asyncOpenPopImage(idphoto)
{
	doRequest('GET','/async/putgallerypicture.php?idphoto='+ idphoto,'',callback);
}

function asyncSelectGalleryImage()
{
	YAHOO.util.Connect.setForm($('xform'), true, true);
	doRequest('POST','/async/photoselected.php','',callbackup);
}

function asyncSetNewPrice()
{
	doRequest('POST','/async/setpromotioncodeprice.php','numbercode='+ $('numbercode').value + '&idtransaction='+ $('idtransaction').value,callback);
}

function asyncSetNewPriceHazelBucks()
{
	var item;
	var idhazelcreditsbulk;
	
	radiobuttons = document.getElementsByName('idhazelcreditsbulk');
	var itemsamount = count(radiobuttons) - 1;
	
	for (var x = 0; x < itemsamount;x++)
	{
		if (isset(radiobuttons[x]))
		{	
		    if (radiobuttons[x].checked)
	    	{
	    		idhazelcreditsbulk = radiobuttons[x].value;
	    	}
	    }
	}
	doRequest('POST','/async/setpromotioncodepricehazelbucks.php','numbercode='+ $('numbercode').value + '&idhazelbulk=' + idhazelcreditsbulk,callback);
}

function asyncChangeHazelBucksPromo()
{
	doRequest('GET','/async/setpromotioncodepricehazelbucks.php','',callback);
}

function asyncCompleteTransactionFree(idtransaction,idpromocampaign)
{
	doRequest('POST','/async/completefreetransaction.php?','idtransaction=' + idtransaction + '&idpromocampaign=' + idpromocampaign,callback);
}

function asyncPageCroptool()
{
	boxedwidth = 425;
	boxedheight = 295;
	doRequest('POST','/async/croptoolinstance.php?step='+step1_RotateStep,'&boxedwidth='+boxedwidth + '&boxedheight='+boxedheight,callback);	
}