var justpreload = new Image();
justpreload.src = 'images/loading.gif';

function generate() {
  var background = document.getElementById('background').value;
  var text = document.getElementById('text').value;
  var text2 = document.getElementById('text2').value;
  var email = document.getElementById('email').value;  
  var offers = document.getElementById('offers').checked;  
  var dateDay = document.getElementById('dateDay').value;
  var dateMonth = document.getElementById('dateMonth').value;
  var dateYear = document.getElementById('dateYear').value;
  var sizes = new Array(300, 400, 500, 600, 700);
  var prop = 500/160;
  var size = document.getElementById('countdownSize').value;
  if (!emailCheck(email)) {
    return;
  }
  var newwidth = sizes[size];
  var newheight = Math.floor(sizes[size]/prop);
  
  text = text.replace(/&/g, "%26");
  text2 = text2.replace(/&/g, "%26");
  var res_img_src = webfolder+'/countdown.php?width='+newwidth+'&height='+newheight+'&bg='+background+'&date_month='+dateMonth+'&date_day='+dateDay+'&date_year='+dateYear+'&un='+text+'&un2='+text2;
  var res_img_forum = '[img]'+webfolder+'/countdown.php?width='+newwidth+'&height='+newheight+'&bg='+background+'&date_month='+dateMonth+'&date_day='+dateDay+'&date_year='+dateYear+'&un='+text+'&un2='+text2+'[/img]';
  var res_flash = '<object type="application/x-shockwave-flash" data="'+webfolder+'/countdown_'+background+'.swf?bgcolor=FFFFFF&date_month='+dateMonth+'&date_day='+dateDay+'&date_year='+dateYear+'&un='+text+'&un2='+text2+'" width="'+newwidth+'" height="'+newheight+'"><param name="movie" value="'+webfolder+'/countdown_'+background+'.swf?bgcolor=FFFFFF&date_month='+dateMonth+'&date_day='+dateDay+'&date_year='+dateYear+'&un='+text+'&un2='+text2+'" width="'+newwidth+'" height="'+newheight+'" /><param name="bgcolor" value="#FFFFFF" /></object>';
  var res_flash_forum = '[FLASH WIDTH='+newwidth+' HEIGHT='+newheight+']'+webfolder+'/countdown_'+background+'.swf?bgcolor=FFFFFF&date_month='+dateMonth+'&date_day='+dateDay+'&date_year='+dateYear+'&un='+text+'&un2='+text2+'[/flash]';
  var so = new simpleajax.obj('ajaxemail');
  so.mode = 'simple';
  so.afterEvent = function(e)
  { 
    //alert(e.result);
  }
  
  var mail1 = res_flash+'<div style="font-family:Arial;font-size:11px;color:#999">Created by <a href="http://www.weddingfavors.org" style="color:#CC3300;text-decoration:underline">Wedding Favors</a></div>'; 
  var mail2 = res_img_forum+'Created by [url=http://www.weddingfavors.org]Wedding Favors[/url]';
  var mail3 = '[img]'+res_img_src+'[/img]'+'\nCreated by [url=http://www.weddingfavors.org]Wedding Favors[/url]';
  var mail4 = '<img src="'+res_img_src+'" alt="" />\nCreated by <a href="http://www.weddingfavors.org">Wedding Favors</a>';
  
  var res = '<br /><br /><div id="countdown_image"><img src="'+res_img_src+'" alt="" /></div>';
  res += '<br /><div class="note">To post on a website / blog (HTML):</div><textarea style="width:500px; height: 70px" id="embedcode" onclick="this.select()" onfocus="this.select()"><img src="'+res_img_src+'" alt="" />\nCreated by <a href="http://www.weddingfavors.org">Wedding Favors</a></textarea>';
  res += '<br /><br /><div class="note">To post on a message board, forum (bbCODE, The Knot message boards, etc):</div><textarea style="width:500px; height: 70px" onclick="this.select()" onfocus="this.select()">[img]'+res_img_src+'[/img]\nCreated by [url=http://www.weddingfavors.org]Wedding Favors[/url]</textarea><br /><br /><div id="divWildfirePost"></div><br />';
  
  res += res_flash + '<br /><br />';
  res += '<div class="note">To post to your website / blog / or myspace use this code (HTML/FLASH):</div>';
  res += '<textarea style="width:500px; height: 70px" onclick="this.select()" onfocus="this.select()">'+res_flash+'\n<div style="font-family:Arial;font-size:11px;color:#999">Created by <a href="http://www.weddingfavors.org" style="color:#CC3300;text-decoration:underline">Wedding Favors</a></div></textarea><br /><br />';
  
  res += '<div class="note">To post to a discussion forum with flash, use this code:</div><textarea style="width:500px; height: 70px" onclick="this.select()" onfocus="this.select()">'+res_flash_forum+'\nCreated by [url=http://www.weddingfavors.org]Wedding Favors[/url]</textarea><br /><br />';
   
  res += '<div class="note nopad">To create another wedding ticker, <a href="javascript:prevPage();">click here</a></div><div class="note nopad">To edit this one, just change your options and click generate.</div>';
  mail1 = Url.encode(mail1);
  mail2 = Url.encode(mail2);
  mail3 = Url.encode(mail3);
  mail4 = Url.encode(mail4);
  so.get('mail.php?email='+email+'&offers='+offers+'&code1='+mail1+'&code2='+mail2+'&code3='+mail3+'&code4='+mail4);
  
  
  document.getElementById('result').innerHTML = res;
  document.getElementById('countdown_image').innerHTML = '<img src="'+res_img_src+'" alt="" />';
  document.getElementById('countdown_image').style.height = ''+newheight+'px';
  document.getElementById('countdown_image').style.background = 'url("images/loading.gif") no-repeat center center';
  //document.getElementById('result').style.height = (newheight+140)+'px';

var pconf={
  defaultContent: 'embedcode', 
  UIConfig: '<config><display showDesktop="false" showEmail="true" showBookmark="true"></display><body><background frame-color="#dcdcdc" background-color="#f8f8f8" gradient-color-begin="" gradient-color-end="" corner-roundness="4;4;4;4"></background><controls><snbuttons frame-color="#cacaca" background-color="#fafafa" over-frame-color="#bababa" over-background-color="#ebebeb" color="#2e2e2e" over-color="#2e2e2e"></snbuttons></controls><texts color="#2e2e2e"><links color="#415bbb"></links></texts></body></config>'
};
Wildfire.initPost('641662', 'divWildfirePost', 500, 300, pconf);
  
  
}

function choose(id) {
  //document.getElementById('background').value = id;
  document.location = 'index.html?page=2&bg='+id;
  //nextPage();
}

function nextPage() {
  document.getElementById('page1').style.display = 'none';
  document.getElementById('page2').style.display = 'block';
}

function prevPage() {
  document.location = 'index.html';
  
}

function emailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Please enter a valid email");
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Please enter a valid email");
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   alert("Please enter a valid email");
	   return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	   alert("Please enter a valid email");
	   return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   alert("Please enter a valid email");
	   return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	   alert("Please enter a valid email");
	   return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	   alert("Please enter a valid email");
	   return false
	 }

	 return true					
}
