$(document).ready(function(){

$('.alphanumeric').alphanumeric({allow:"-"});
$('.alphanumericLow').alphanumeric({allow:"-",nocaps:true});
$('.alphanumeric-space').alphanumeric({allow:"- "});
$('.alphanumeric-link').alphanumeric({allow:"://-."});

//$('.sanitize').alphanumeric({ichars:'\'\"\/\\'});
//$("#channel-ready", top.document).slideDown();
//$("#content1", top.document).hide();
	
	$("#btnContinue").click(function activated(){
		
			channelReady()
		
	});
	
function channelReady()
{
	if($("#btnContinue").attr('rel')=='renew')
		{
			
			$("#renewForm").fadeOut('slow',function(){
				$("#channel-renewed").fadeIn();
			});
			
			return false;
		}
		$("#content1", top.document).slideUp('slow',function(){
			
			/*
			$("#content1").animate({height: '295px'},500,function(){
				$("#channel-ready").fadeIn();
			});
			*/
			
			$("#channel-ready", top.document).slideDown();
		});	
		
		bg1				= $("#fld-newImageName1").val();	
		bg1ext			= $("#fld-imageExtension1").val();	

		bg2				= $("#fld-newImageName2").val();	
		bg2ext			= $("#fld-imageExtension2").val();	
		
		bg3				= $("#fld-newImageName3").val();	
		bg3ext			= $("#fld-imageExtension3").val();	
		
		if(bg1!=''){
			name		=	bg1;
			extension	=	bg1ext;
		}else if(bg2!=''){
			name		=	bg2;
			extension	=	bg2ext;
		}else if(bg3!=''){
			name		=	bg3;
			extension	=	bg3ext;
		}

		path		= 	$("#fld-img_path").val();
		
		$("#channel-ready", top.document).attr("style", "background:url(" + path + "/" + name + "_thumb." + extension + ");");
			
		subdomainName	= $("#fld-subDomain").val();

		$("#channelLink", top.document).attr("href","http://" + subdomainName + ".wetransfer.com/");
}
	



$("#btn-next").click(function(){
	
	message = checkAll();

	if(message!=''){
		//alert(message);
		return false;
	}else{
		
		//create temporary record
		companyWebsite	= $("#fld-website").val();		
		firstName		= $("#fld-name").val();		
		emailAddress	= $("#fld-email").val();		
		password		= $("#fld-pass").val();		
		passwordCheck	= $("#fld-passCheck").val();		
		
		formType		= $("#fld-formType").val();
		referral		= $("#fld-referral").val();
		
		subdomainName	= $("#fld-subDomain").val();
		subdomainName	= subdomainName.replace(".wetransfer.com","");			

		advertTitle		= $("#fldTitle").val();		
		advertLink		= $("#fld-Link").val();	
		
		if(advertLink.search("http://") < 0){
			advertLink = "http://" + advertLink;
		}
		
		bg1				= $("#fld-newImageName1").val();	
		bg1ext			= $("#fld-imageExtension1").val();	
		bg1active		= $("#bg1-active").val();	
		
		bg2				= $("#fld-newImageName2").val();	
		bg2ext			= $("#fld-imageExtension2").val();	
		bg2active		= $("#bg2-active").val();		
		
		bg3				= $("#fld-newImageName3").val();	
		bg3ext			= $("#fld-imageExtension3").val();	
		bg3active		= $("#bg3-active").val();	
		
		emailbg			= $("#bgChanged").val();
		appPos			= $("#fld-wt-pos").val();	
		
		defEmail2		= $("#defEmail").val();	
	
		companyName		= $("#fld-companyName").val();
		street			= $("#fld-street").val();
		city			= $("#fld-city").val();
		zipcode			= $("#fld-zipcode").val();
		cntry			= $("#fld-country").val();
		
		if(cntry == "Select your country"){
			cntry = "";
		}
		
		if($("#fld-Coupon").val()!="Coupon code"){
			couponCode	= $("#fld-Coupon").val();
		}else{
			couponCode	= "";
		}
		
		vat				= $("#fld-vat").val()
		
		$("#btn-next").attr("src","img/btn-checkout-passive.png");
		
		$.post('includes/create_entry.php',{ vat:vat, couponCode:couponCode, companyName:companyName, street:street, city:city, zipcode:zipcode, country:cntry, referral:referral, formType:formType, companyWebsite:companyWebsite, firstName:firstName, emailAddress:emailAddress, password:password, passwordCheck:passwordCheck, subdomainName:subdomainName, advertTitle:advertTitle,advertLink:advertLink, bg1:bg1, bg1ext:bg1ext, bg1active:bg1active, bg2:bg2, bg2ext:bg2ext, bg2active:bg2active, bg3:bg3, bg3ext:bg3ext, bg3active:bg3active, emailbg:emailbg, appPos:appPos, defEmail2:defEmail2 }, function(data) {
			
			if(data.user_id){

				stopit = 0;
				
				$("#fld-uid").val(data.user_id);				
				
				if(data.codeResult!="false" && data.codeResult != null)
				{
							
					$("#display-amount").html(data.codeResult);
					vatAmount = data.codeResult*1.19;
					$("#display-amount-vat").html(vatAmount);	
						
					$("#fld-amount").val(data.codeResult);
					
					if(data.codeResult==0){
						stopit = 1;
						//alert("OMG U UZED A FREECODEZ0r");
						//activate record
						
						$.post('js/checkCouponCode.php',{code:couponCode,uid:data.user_id}, function(data) {
						
							if(data=="9"){
								window.location = "https://" + subdomainName + ".wetransfer.com/"
							}
							
							channelReady();
							
						//	$("#btn-next").attr("src","img/btn-checkout.png");
							
						});
					
					
	
					}
				
				alert("Your coupon code was successfully applied!");
				
				}else if(data.codeResult=="false"){
					alert("Error: " + data.codeError);
					stopit = 1;
				}

				
				if(data.no_vat==1)
				{
					$("#fld-notax").val("1");
				}
				
				if(stopit ==0){
					$("#btn-next").attr('disabled', 'disabled');
					$("#btn-next").css({"background":"grey","opacity":"0.4"});
					$("#formPaypal").submit();
				}else{
					$("#btn-next").attr("src","img/btn-checkout.png");
					return false;
				}
				
					//	$("#registerWrap").css({overflow: 'hidden'});
					//	$("#registerWrap").fadeOut('slow',function(){						   
					//		$("#channel-paypal").show();
					//		$("#fld-uid").val(data.user_id);
							
					//		height = $("#channel-paypal").height() + 25;
					//		$("#content1",window.parent.document).animate({height:height});
					//		
					//		if($("#fld-partnerCode").val() !== undefined)
					//		{
					//			checkCode();
					//		}
							
					//	});
						
					//	$('body', top.document).scrollTo(600,800);
						
						//$('window', top.document).animate({ scrollTop: 600 });  
						
					//	imgName = data.imageName;
					//	extension =  data.extension;
					//	imgPath = $('#fld-img_path').val();
					//	path = 'url(' + imgPath + imgName + '_thumb' + '.' + extension + ')';
					//	$("#channel-ready").css("background",path);
						
					//	channel = data.channel;
					//	$("#channelLink").attr("href", "http://" + channel + ".wetransfer.com/");				
			
			//$("#btn-next").attr("src","img/btn-checkout.png");
			}else{
				$("#btn-next").attr("src","img/btn-checkout.png");
				alert('error: ' + data);
				return false;
			}
		},"json");
	
	}
	
	
});


$(".channel-info-link").click(function(){
										   
	$('body', top.document).scrollTo($('#content1', top.document),800);

	//$("#channel-form-container", top.document).hide(function(){

		$("#content1", top.document).animate({height: '1780px'},500,function(){
			
			$("#channel-info", top.document).fadeIn('slow');
			$("#channel-form-container", top.document).hide();
		});
		
	//});
		
});

$("#enter-coupon-link").click(function(){
	$(this).hide();
	$("#fld-CouponCode").show();
	//$("#coupon-wrapper").toggle();
	$("#btnPay").css('margin-top','45px');
});	

	

}); //end of documentready

function updateUploadedImage(fileName,extension,formId){
	$("#ajaxLoader").hide();
	
	formId = "#upform" + formId;
	
	$(formId).children('.newImageName').val(fileName);	
	$(formId).children('.imageExtension').val(extension);	

	if($(formId).attr('action')=='includes/uploadEmailBg.php')
	{
		path = $("#fld-emailBgPath").val();
		$(formId).children('#bgChanged').val('1');	
	}else{
		path = $("#fld-img_path").val();
	}

	var timestamp = Number(new Date());
	
	// border:5px solid #000; -moz-border-radius: 7px; -webkit-border-radius:7px; border-radius: 7px;
	
	$(formId).children('.imgPreview').fadeOut(400,function(){
				
		if($(formId).children('#highlight').length != 0){
			
			$("#upform2").children('.newImageName').val(fileName);	
			$("#upform2").children('.imageExtension').val(extension);
			
			$("#bigFileSelect").hide();
			
			$("#previewBgTop").css('background', 'url(' + path + "/" + fileName +"_thumbBig."+ extension + '?t='+ timestamp +')');
			$("#channelPreview").fadeIn();
			$("#title-design-your-channel").fadeIn();
			
			$("#upform2").children('.imgPreview').css('background', 'url(' + path + "/" + fileName +"_thumbSmall."+ extension + '?t='+ timestamp +')');
			$("#upform2").children().children('.onOffSelect').fadeIn();
			$("#upform2").children().children('.onOffSelect').click();
			
			$("#smallUploadFields").fadeIn();
			
			return false;
			
		}else{
			
			$(formId).children('.imgPreview').css('background', 'url(' + path + "/" + fileName +"_thumbSmall."+ extension + '?t='+ timestamp +')');
			
			$(formId).children('.imgPreview').fadeIn();
			
			$(formId).children().children('.onOffSelect').fadeIn();
		
			onOffVal = $(formId).children().children('.onOffSelect').children('.fldOnOff').val();
			if(onOffVal!=1){
				$(formId).children().children('.onOffSelect').click();
			}
		
		}
		
		
	});
	
	
	$(formId).children('.imgPreview').children('.fileSelectContainer').children('.imageFieldLoader').hide();
	
	if($("#fld-newImageName1").val()!='' || $("#fld-newImageName2").val()!='' || $("#fld-newImageName3").val()!=''){
		$("#channelPreviewLink").fadeIn();
	}
	
	if($("#bgChanged").val()!='0'){	
		$("#emailPreviewLink").fadeIn();
	}
	
}

function checkAll()
{

	message = '';
	
	$(".newInput").each(function(){
		if($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))
		{
			if($(this).hasClass('optional'))
			{
				//optional field, do nothing
			}else{
				message += "This field is empty \n";
				
				
				
				if($(this).parent().children().hasClass('errorCheck'))
				{
					//do nothing
				}else{
					$(this).parent().children("div .errorCheck").remove();
					$(this).addClass("errorInput");
					$(this).parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:none;' ><div class=\"pijltje\"></div>This field is required and cannot be empty</div>");
					$(this).parent().children("div .errorCheck").slideDown();
				}
			}
		}
	});
	
	//Channel Details
	if($("#subCheckInfo").val() != '')
	{
		message += "There is a problem with your channel name \n";
		
		$("#fld-subDomain").parent().children("div .errorCheck").remove();
		
		$("#fld-subDomain").addClass("errorInput");
		$("#fld-subDomain").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>There is a problem with your channel name</div>");
	}
	
	/*
	if($("#fld-subDomain").val() == '')
	{
		message += "Channel URL is empty\n";
		
		$("#fld-subDomain").parent().children("div .errorCheck").remove();
		
		$("#fld-subDomain").addClass("errorInput");
		$("#fld-subDomain").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Channel URL is empty</div>");
		
	}

	if($("#fldTitle").val() == '')
	{
		message += "Channel title field is empty\n";
		
		$("#fldTitle").parent().children("div .errorCheck").remove();
		
		$("#fldTitle").addClass("errorInput");
		$("#fldTitle").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Channel title field is empty</div>");
		$("#fldTitle").parent().children(".errorCheck").slideDown();
	}

	if($("#fld-Link").val() == '')
	{
		message += "Channel link is empty\n";
		
		$("#fld-Link").parent().children("div .errorCheck").remove();
		
		$("#fld-Link").addClass("errorInput");
		$("#fld-Link").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Channel Link is empty</div>");
	}
	
	//Appearance
	
	if(($("#fld-newImageName1").val() == '') &&  ($("#fld-newImageName2").val() == '') && ($("#fld-newImageName3").val() == ''))
	{
		//it's now allowed not to have any backgrounds
		//message += "No backgrounds are uploaded for your Channel\n"
	}
	
	
	//User Details
	
	if($("#fld-name").val() == '')
	{
		message += "Name is empty\n";
		
		$("#fld-name").parent().children("div .errorCheck").remove();
		
		$("#fld-name").addClass("errorInput");
		$("#fld-name").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Name is empty</div>");
	}	
	
	if($("#fld-email").val() == '')
	{
		message += "Email is empty\n";
		
		$("#fld-email").parent().children("div .errorCheck").remove();
		
		$("#fld-email").addClass("errorInput");
		$("#fld-email").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Email is empty</div>");
	}
	
	if($("#fld-pass").val() == '')
	{
		message += "Password is empty\n";
		
		$("#fld-pass").parent().children("div .errorCheck").remove();
		
		$("#fld-pass").addClass("errorInput");
		$("#fld-pass").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Password is empty</div>");
	}

	if($("#fld-passCheck").val() == '')
	{
		message += "Password check is empty\n";
		
		$("#fld-passCheck").parent().children("div .errorCheck").remove();
		
		$("#fld-passCheck").addClass("errorInput");
		$("#fld-passCheck").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Password check is empty</div>");
	}
	
	*/
	
	//Additional checks
	
	if($("#fld-pass").val() != $("#fld-passCheck").val())
	{
		message += "Passwords don't match";
		
		$("#fld-passCheck").parent().children("div .errorCheck").remove();
		
		$("#fld-passCheck").addClass("errorInput");
		$("#fld-passCheck").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>Passwords don't match</div>");
	}

	if($("#fld-Link").val() != '')
	{		
		value = $("#fld-Link").val();
		
		if(value.search("http://") < 0 ){
			value = "http://" + value;
		}
		
		if(value.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/))
		{
			//valid
		}else{
			$("#fld-Link").parent().children("div .errorCheck").remove();
			message += "This URL is invalid";
			$("#fld-Link").addClass("errorInput");
			$("#fld-Link").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>This URL is invalid</div>");
		}
	}
	
	if($("#fld-email").val() != '')
	{
		//message += "Email is empty\n";
		
		value = $("#fld-email").val();

			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
			if(emailPattern.test(value))
			{
				//$('#emailCheckInfo').html("<img src='img/tick.jpg' />");
			}else{
				//$('#emailCheckInfo').html("<img src='img/cross.jpg' />Please provide a valid Email address");
				message += "The given email address is invalid";
				
				$("#fld-email").parent().children("div .errorCheck").remove();
				
				$("#fld-email").addClass("errorInput");
				$("#fld-email").parent().append("<div class=\"fieldInfoRed errorCheck\" style='display:block;' ><div class=\"pijltje\"></div>The given email address is invalid</div>");
			} 		
		
		
	}
	
	
	return message;
}

