var gTrigger = '';

$(document).ready(function() {
  $('#submitBtn').click(function() { 
    validateSubmission();
  });
  
  // capture enter key and have it trigger validation
  $('#modalform').keyup(function(e) {
  	if(e.keyCode == 13) {
  		validateSubmission();
  	}
  });
  
  // disable normal form submit
  $('#modalform').submit(function() {
  	return false;
  }); 
  
  $('#closeBtn').click(function() { 
    $.fn.colorbox.close();	
  });  
  
});  

function validateSubmission() {
  // unset errors     
  var errors = 0;    
  var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var regex = new RegExp(emailReg);
  var errormsg = 'Please fill out required fields';
  
  document.getElementById('errormessages').innerHTML="";
  document.getElementById('errormessages').style.display="none";
  document.getElementById('fnameLabel').style.color="#003366";
  document.getElementById('lnameLabel').style.color="#003366";
  document.getElementById('emailLabel').style.color="#003366";  
  document.getElementById('phoneLabel').style.color="#003366";  
  document.getElementById('fnameLabel').style.fontWeight="normal";
  document.getElementById('lnameLabel').style.fontWeight="normal";
  document.getElementById('emailLabel').style.fontWeight="normal";
  document.getElementById('phoneLabel').style.fontWeight="normal";  
	  
  // test and set error display on labels
  if (document.getElementById('fname').value == "") {
    document.getElementById('fnameLabel').style.color="#9a4";
    document.getElementById('fnameLabel').style.fontWeight="bold";
    errors = 1;
  }
  if (document.getElementById('lname').value == "") {
    document.getElementById('lnameLabel').style.color="#9a4";
    document.getElementById('lnameLabel').style.fontWeight="bold";
    errors = 1;
  }  
  if (document.getElementById('email').value == "") {
    document.getElementById('emailLabel').style.color="#9a4";
    document.getElementById('emailLabel').style.fontWeight="bold";
    errors = 1;
  }   
  else if (!(regex.test(document.getElementById('email').value))) {
    document.getElementById('emailLabel').style.color="#9a4";
    document.getElementById('emailLabel').style.fontWeight="bold";
	  errormsg = errormsg + ' and make sure email is properly formatted';
    errors = 1;
  }     
  
  // set error display
  if (errors == 1) {
    document.getElementById('errormessages').innerHTML='<br/>' + errormsg + '.';
		document.getElementById('errormessages').style.display="block";  
		document.getElementById('errormessages').style.color="#9a4";
		return false;
  }
  else { 
    submitInfo();
  }
}

function submitInfo() {
  var resp;
  var responseArr;
  
  if (document.getElementById('ctaID').value > '') {
    document.getElementById('modalform').action = '/includes/ajax/ctaRequest.cfm';
	ajaxSubmit(document.getElementById('modalform'));	 
  }
  else {
    ajaxSubmit(document.getElementById('modalform'));	 
  }		  
}

function ajaxSubmit(frm) {
  var status = AjaxRequest.submit(
    frm
       ,{
	   'onSuccess':function(req){  
	   	 // urchinTracker("/modalFormSubmitted.cfm");
	     var responseArr = req.responseText.split(String.fromCharCode(248));
		 
	     if (responseArr[1] == 'normal') { 
		   displayWhitePaper(responseArr[2],responseArr[3],responseArr[4],responseArr[5],responseArr[6],responseArr[7],responseArr[8],responseArr[9]);			   
		 }
		 else if (responseArr[1] == 'cta') {   		   
		   displayCTA(responseArr[2]);
		 }		 
	    },
	   'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText); }	   
	   }		
     ); 	
}

function displayWhitePaper(reqType,reqURL,reqExternal,reqTitle,reqSource,reqDate,reqDLsize) {
  var newContent = ''; 
  newContent = newContent + '<img height="1" width="1" border="0" src="http://www.googleadservices.com/pagead/conversion/1072497518/?label=E39uCNK2YhDuhrT_Aw&amp;script=0"/>';
  
  newContent = newContent + '<span style="font-size:135%; font-weight:bold;"><strong>Title: ' + reqTitle + '</strong></span>';
  newContent = newContent + '<br /><br /><div class="downloadLnk"><a href="' + reqURL + '"';
  if (reqExternal == 1 || reqType != 'LNK') {
    newContent = newContent + 'target="_blank"';
  }
  newContent = newContent + '><img src="/images/common/button-download-now.gif" alt="Download Now" style="border-style: none" onMouseOver="this.src=\'/images/common/button-download-now-over.gif\'" onMouseOut="this.src=\'/images/common/button-download-now.gif\'" /></a></div><br /><br />';
  newContent = newContent + 'Author/Source Name: <strong>' + reqSource + '</strong><br />';  
  newContent = newContent + 'Date Published: <strong> ' + reqDate + '</strong><br />';   
 
  if (reqType != 'LNK') {
    newContent = newContent + 'File Size: <strong>' + reqDLsize + '</strong><br /><br />';
  }
       
  
  if (document.cookie.indexOf('wpregistration') > 0) {
    newContent = newContent + '<p><input name="" class="clear_btn"  style="width:63px;" value="Close" type="button" id="closeBtn" /></p>';  
  }
  else {
    newContent = newContent + '<p><img src="/images/common/button-close.gif" onClick="window.location.href=window.location.href;" onMouseOver="this.src=\'/images/common/button-close-over.gif\'" onMouseOut="this.src=\'/images/common/button-close.gif\'" /></p>';
	//<input name="" class="clear_btn" style="width:63px;" value="Close" type="button" onClick="window.location.href=window.location.href;"
  }

  document.cookie = 'wpregistration=true; path=/';
  document.getElementById('signupForm').innerHTML = newContent;       
  trackConversion();  
}

function launchForm(wp) {  
  $.fn.colorbox({inline:true, href:"#signupForm", open:true, iframe:false, width:474, height:675});
  $().bind('cbox_closed', function(){
    $('#signupForm').hide();
  });  
  $('#signupForm').show();
  document.getElementById('wpID').value=wp;
  document.getElementById('ctaID').value='';  
}

function launchResource(wp,wpTitle) {  
  $.fn.colorbox({inline:true, href:"#signupForm", open:true, iframe:false, width:474, height:675});
  $().bind('cbox_closed', function(){
    $('#signupForm').hide();
  });  
  $('#signupForm').show();
  document.getElementById('wpID').value=wp;
  document.getElementById('ctaID').value='';  
  var wpTitleHTML = '<p></p><p><label>Resource:</label>'+wpTitle+'</p>';
  $('#resourceTitle').html(wpTitleHTML);   
}

function launchResource(wp,wpTitle,gt) {  
  $.fn.colorbox({inline:true, href:"#signupForm", open:true, iframe:false, width:474, height:675});
  $().bind('cbox_closed', function(){
    $('#signupForm').hide();
  });  
  $('#signupForm').show();
  document.getElementById('wpID').value=wp;
  document.getElementById('ctaID').value='';  
  var wpTitleHTML = '<p></p><p><label>Resource:</label>'+wpTitle+'</p>';
  $('#resourceTitle').html(wpTitleHTML);   
  
  gTrigger = gt;
  
  if (gTrigger == 'whitePaper') {
    submitUrchin('/whitepaperModalLaunched');
  }  
  else {
    submitUrchin('/dotboomModalLaunched');   
  }    
}

function submitUrchin(pg) {
  urchinTracker("/"+pg);
}

function trackConversion() { 
  if (gTrigger == 'whitePaper') {
    submitUrchin('/whitepaperModalSubmitted');
  }  
  else {
    submitUrchin('/dotboomModalSubmitted');   
  }
}