// JavaScript Document
var clearedFeedback = 0;
var clearedEmailPage = 0;

// urls
var sendFeedback = '/email/AjaxSendFeedback.html';
var sendQualPort = '/email/AjaxSendQualPort.html';
var sendPage = '/email/AjaxSendPage.html';
var sendQualPortRequest = '/email/AjaxSendQualPortRequest.html';
var shareJobSite = '/email/AjaxShareJobSite.html';
var subToQualPort = '/email/AjaxSubcontractorToQualPort.html';


// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax sendFeedback Form ------------------------------
// ----------------------------------------------------------------------------------------

function resetFeedback(){
	
		// $('fb_details').value = '';
		// $('fb_details').removeClassName('red');
		$('helpImproveConfirm').update('');
		$('helpImproveConfirm').innerHTML;
		$('helpImproveConfirm').hide();
		$('helpImproveForm').show();
	
}

function clearFeedback(){

	if (!clearedFeedback){
		
		clearedFeedback = 1;
		$('fb_details').value = '';
		$('fb_details').removeClassName('red');
		
	}

}

function submitFeedback(){
	
	if (!clearedFeedback){
		alert('In order to send this form you must enter a valid Email address and details of your suggestion.');
		return false;
	}
	
	if (!Validation.validate('fb_email',{showError:false})){
		alert('In order to send this form you must enter a valid Email address.');
		return false;		
	}
	
	if ($F('fb_details') == ''){
		alert('In order to send this form you must enter the details of your suggestion.');
		return false;		
	}
	
	if ($F('fb_email') != '' && $F('fb_details') != '' && clearedFeedback == 1){

		var pars = $('fbFrm').serialize();

		new Ajax.Request(sendFeedback,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,
		
			onSuccess: function(transport){
				
				$('hiLink').hide();
				
				Effect.BlindUp('helpImproveForm', { duration: .25 });

				$('helpImproveConfirm').update(transport.responseText);
				$('helpImproveConfirm').innerHTML;
				
				Effect.BlindDown('helpImproveConfirm', { duration: .25, queue: 'end',afterFinish:function(){$('hiLink').show();}});
			}
		});
	}
}


// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax sendPage (emailPage) Form ----------------------
// ----------------------------------------------------------------------------------------

function resetEmailPage(){
	
		$('emailPageConfirm').update('');
		$('emailPageConfirm').innerHTML;
		$('ep_sendToEmail').value = '';
		$('ep_message').value = $('ep_message').title;
		$('emailPageConfirm').hide();
		$('emailPageForm').show();
	
}

function submitEmailPage(){
	
	if ($F('ep_sendByName') == ''){
		alert('In order to send this form you must enter a valid From Name.');
		return false;		
	}
	
	if ($F('ep_sendToEmail') == ''){
		alert('In order to send this form you must enter a valid Email Address.');
		return false;		
	}

	if ($F('ep_message') == ''){
		alert('In order to send this form you must enter a Personal Message.');
		return false;		
	}


	if ($F('ep_sendByName') != '' && $F('ep_sendToEmail') != '' && $F('ep_message') != ''){

		var pars = $('epFrm').serialize();

		new Ajax.Request(sendPage,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,

			onSuccess: function(transport){
				
				$('epLink').hide();
				
				Effect.BlindUp('emailPageForm', { duration: .25 });

				$('emailPageConfirm').update(transport.responseText);
				$('emailPageConfirm').innerHTML;
				
				Effect.BlindDown('emailPageConfirm', { duration: .25, queue: 'end',afterFinish:function(){$('epLink').show();}});
			}
		});
	}
}


// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax sendQualport Form ------------------------------
// ----------------------------------------------------------------------------------------

function resetSendQualport(){
	
		$('sqp_sendToEmail').value = '';
		$('sqp_message').value = '';
		$('sendQualportForm').show();
		$('sendQualportConfirm').update('');
		$('sendQualportConfirm').innerHTML;
		$('sendQualportConfirm').hide();
		
		if ($('sqp_qualportID').selectedIndex){
			 $('sqp_qualportID').selectedIndex = 0;
		}
		
		setUpMsg_sqp();
	
}

function submitSendQualport(){
	
	if ($F('sqp_qualportID') == ''){
		alert('In order to send this form you must select a Profile.');
		return false;				
	}
	
	if ($F('sqp_sendByName') == ''){
		alert('In order to send this form you must enter a valid From Name.');
		return false;		
	}
	
	if ($F('sqp_sendToEmail') == ''){
		alert('In order to send this form you must enter a valid Email Address.');
		return false;		
	}

	if ($F('sqp_message') == ''){
		alert('In order to send this form you must enter a Personal Message.');
		return false;		
	}


	if ($F('sqp_sendByName') != '' && $F('sqp_sendToEmail') != '' && $F('sqp_message') != ''){

		var pars = $('sqpFrm').serialize();

		new Ajax.Request(sendQualPort,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,
			
			onSuccess: function(transport){
				
				var rtn = transport.responseText;
				
				if (rtn == '' || rtn == -1){
					
					location.reload();
					
				}else{	
			
					$('sqLink').hide();
					
					Effect.BlindUp('sendQualportForm', { duration: .25 });
	
					$('sendQualportConfirm').update(rtn);
					$('sendQualportConfirm').innerHTML;
					
					Effect.BlindDown('sendQualportConfirm', { duration: .25, queue: 'end',afterFinish:function(){
																											   $('sqLink').show();
																											   setUpMsg_sqp();
																											   }});
				}
			}
		});
	}
}

function setUpMsg_sqp(){
	
	var s = $('sqp_qualportID');
	var t = $('sqp_message');
	var i = s.selectedIndex;
	var msg = ' would like you to view their complete and current qualifications. ';
	
	if (i == undefined || i == 0){
		t.value = defMsgShare;
	}else{
		t.value = s[i].title + msg;
	}	
	
	
}


// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax sendQualportRequest Form -----------------------
// ----------------------------------------------------------------------------------------

function resetSendQualportRequest(){
		
		$('sqpr_sendToEmail').value = '';
		
		$('sendQualportRequestForm').show();
		$('sendQualportRequestConfirm').update('');
		$('sendQualportRequestConfirm').innerHTML;
		$('sendQualportRequestConfirm').hide();
		
		if ($('sqpr_qualportID').selectedIndex){
			 $('sqpr_qualportID').selectedIndex = 0;
		}
		
		setUpMsg();
	
}

function submitSendQualportRequest(){
	
	if ($F('sqpr_qualportID') == ''){
		alert('In order to send this form you must select a Profile.');
		return false;				
	}
	
	if ($F('sqpr_sendByName') == ''){
		alert('In order to send this form you must enter a valid From Name.');
		return false;		
	}
	
	if ($F('sqpr_sendToEmail') == ''){
		alert('In order to send this form you must enter a valid Email Address.');
		return false;		
	}

	if ($F('sqpr_message') == ''){
		alert('In order to send this form you must enter a Personal Message.');
		return false;		
	}


	if ($F('sqpr_sendByName') != '' && $F('sqpr_sendToEmail') != '' && $F('sqpr_message') != ''){
		
		var pars = $('sqprFrm').serialize();
		
		new Ajax.Request(sendQualPortRequest,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,

			onSuccess: function(transport){
				
				var rtn = transport.responseText;
				
				if (rtn == '' || rtn == -1){
					
					location.reload();
					
				}else{	
				
					$('sqrLink').hide();
					
					Effect.BlindUp('sendQualportRequestForm', { duration: .25 });
	
					$('sendQualportRequestConfirm').update(rtn);
					$('sendQualportRequestConfirm').innerHTML;
					
					Effect.BlindDown('sendQualportRequestConfirm', { duration: .25, queue: 'end',afterFinish:function(){
																													  $('sqrLink').show();
																													  setUpMsg();
																													  }});
				
				}
			}
		});
	}
}

function setUpMsg(){
	
	var s = $('sqpr_qualportID');
	var t = $('sqpr_message');
	var i = s.selectedIndex;
	var msg = " recommends you build a Profile in jobsite123, in order to connect and cross-promote each other online.  Profiles that are connected appear in the other's Profile – which generates opportunities because of the increased online visibility and exposure.";
		
	if (i == undefined || i == 0){
		t.value = defMsgReq;
	}else{
		t.value = s[i].title + msg;
	}
	
}


// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax subcontractorToQualport Form -------------------
// ----------------------------------------------------------------------------------------

function resetSubcontractorToQualport1(){
	
		$('subcontractorToQualportForm1').show();
		$('subcontractorToQualportConfirm1').update('');
		$('subcontractorToQualportConfirm1').innerHTML;
		$('subcontractorToQualportConfirm1').hide();
	
}

function resetSubcontractorToQualport2(){
	
		$('subcontractorToQualportForm2').show();
		$('subcontractorToQualportConfirm2').update('');
		$('subcontractorToQualportConfirm2').innerHTML;
		$('subcontractorToQualportConfirm2').hide();
	
}


function submitSubcontractorToQualport(n){
	
	frm = $('stqpFrm'+n);
	lnk = $('stqpLink'+n);
	f = 'subcontractorToQualportForm'+n;
	c = 'subcontractorToQualportConfirm'+n;
	
	if (frm.stqp_qualportID.value == 0){
		alert('In order to send this form you must select one of your Profiles.');
		return false;				
	}
	
	if (frm.stqp_sendByName.value == ''){
		alert('In order to send this form you must enter a valid From Name.');
		return false;		
	}
	
	if (frm.stqp_message.value == ''){
		alert('In order to send this form you must enter a Personal Message.');
		return false;		
	}
	
	

	if (frm.stqp_sendByName.value != '' && frm.stqp_message.value != ''){
		
		var pars = frm.serialize();
		
		
		new Ajax.Request(subToQualPort,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,

			onSuccess: function(transport){
				
				var rtn = transport.responseText;
				
				if (rtn == '' || rtn == -1){
					
					location.reload();
					
				}else{	
				
				
					lnk.hide();
					
					Effect.BlindUp(f, { duration: .25 });
	
					$(c).update(rtn);
					$(c).innerHTML;
					
					Effect.BlindDown(c, { duration: .25, queue: 'end',afterFinish:function(){lnk.show();}});
					
				}
				
			}
		});
	}
}

// ----------------------------------------------------------------------------------------
// ---------------------------------- Ajax Share JobSite Form ------------------------------
// ----------------------------------------------------------------------------------------

function resetShareJobSite(){
	
		$('sjs_sendToName').value = '';
		$('sjs_sendToEmail').value = '';
		$('shareJobsiteConfirm').update('');
		$('shareJobsiteConfirm').innerHTML;
		$('shareJobsiteConfirm').hide();
		$('shareJobsiteForm').show();
	
}

function submitShareJobSite(){
	
	if (!Validation.validate('sjs_sendToName',{showError:false})){
		alert('Please enter the person\'s name.');
		return false;		
	}
	
	if (!Validation.validate('sjs_sendToEmail',{showError:false})){
		alert('Please enter the person\'s name to send the Email address.');
		return false;		
	}
	
	if ($F('sjs_sendToName') != '' && $F('sjs_sendToEmail') != ''){

		var pars = $('sjsFrm').serialize();

		new Ajax.Request(shareJobSite,{
			method: 'post', // post method works better with IE 7 (cache bug)
			parameters: pars,
		
			onSuccess: function(transport){
				
				var rtn = transport.responseText;
				
				if (rtn == '' || rtn == -1){
					
					location.reload();
					
				}else{	
				
					$('sjsLink').hide();
					
					Effect.BlindUp('shareJobsiteForm', { duration: .25 });
	
					$('shareJobsiteConfirm').update(rtn);
					$('shareJobsiteConfirm').innerHTML;
					
					Effect.BlindDown('shareJobsiteConfirm', { duration: .25, queue: 'end',afterFinish:function(){$('sjsLink').show();}});
				
				}
			}
		});
	}
}

function wteb(e,t){
	var frmt = 'ma';
	frmt = frmt + 'il';
	frmt = frmt + 'to:';
	new Ajax.Request('/em/' + e + '.html',{
		method: 'post', // post method works better with IE 7 (cache bug)
		parameters: 't=' + t,
		onSuccess: function(transport){
			var rtn = transport.responseText;
			if (rtn != '') {
				if (rtn.substring(0,3) == '-1|') {
					alert('You must be a logged in registered user to access this feature. \nYou may login or create an account at the top of the page.');
				} else if (rtn.substring(0,3) == '-2|') {
					new Ajax.Request('/user/registration/AjaxSendVerificationEmail.html',{method:'post'});
					alert('Your email address has not been verified. Please verify your email address to use this feature. \nAn email has just been sent to you with instructions on how to do this.');
				} else if (rtn.substring(0,3) == '-3|') {
					alert('You have reached the limit of emails allowed for today. \nIf you feel this is an error or would like us to increase this limit for you, \nplease contact solutions@jobsite123.com and give us a brief explaination of your needs.');
					window.location = frmt + 'solutions@jobsite123.com';
				} else {
					window.location = frmt + rtn;
				}
			} else {
				alert('Invalid email returned. We thank you for your patience while we work to resolve this issue.');
			}
		}
	});
}
