﻿/***************************************************************************
| Loans Till PayDay
|---------------------------------------------------------------------------
|	Requires Prototype.js library v1..6.0.2 & protoTools.js
|---------------------------------------------------------------------------
| Designed by Dmitry Lopyrev
|****************************************************************************/


function doload() {
    try {
			var frm = $('frmApplication') || $('frmContact');
			if (frm = initForm(frm,{onsubmit:doSubmit.bindAsEventListener(frm),onreset:doReset.bindAsEventListener(frm)}), frm) 
				if (frm.id=='frmApplication') {
					
					$('sin').doComplete=validateSIN;
					$('homephone') && ($('homephone').doComplete=validatePhone);
					$('cellphone') && ($('cellphone').doComplete=validatePhone);
					$('ref_phone') && ($('ref_phone').doComplete=validatePhone);
					$('emp_phone') && ($('emp_phone').doComplete=validatePhone);
					$('emp_hrphone') && ($('emp_hrphone').doComplete=validatePhone);
		
					$('postalcode') && ($('postalcode').doComplete=validatePostalCode);
					$('emp_postalcode') && ($('emp_postalcode').doComplete=validatePostalCode);

					$('isphotoida') && ($('isphotoida').onclick = function(){
						$('isphotoid').removeClassName('error');
						$('photoid_container','photoidnumber_container').invoke('show');
						$('photoid').addClassName('required');
						$('photoid').focus();
					});			
					$('isphotoidb') && ($('isphotoidb').onclick = function(){
						$('isphotoid').removeClassName('error');
						$('photoid').$class.doShowError(false);
						$('photoid_container','photoidnumber_container').invoke('hide');
						$('photoid').removeClassName('required');
					});			


					$('bank_isperschequeb') && ($('bank_isperschequeb').onclick = function(){
						$('bank_isperscheque').removeClassName('error');
						$('ctl00_main_bank_isperscheque_note').show()
					});			
					$('bank_isperschequea') && ($('bank_isperschequea').onclick = function(){
						$('bank_isperscheque').removeClassName('error');
						$('ctl00_main_bank_isperscheque_note').hide()
					});			

					
					$('bank_nsfb') && ($('bank_nsfb').onclick = function(){
						$('bank_nsfnumber_container').show();
						$('bank_nsfnumber').addClassName('required').focus();
					});			
					$('bank_nsfa') && ($('bank_nsfa').onclick = function(){
						$('bank_nsfnumber').removeClassName('required');
						$('bank_nsfnumber_container').hide();
					});			


					$('bank_isdischargedb') && ($('bank_isdischargedb').onclick = function(){
						$('bank_isdischargeddate_container','ctl00_main_bank_isdischarged_note').invoke('show');
						$('bank_isdischargeddate').addClassName('required').focus();
					});			
					$('bank_isdischargeda') && ($('bank_isdischargeda').onclick = function(){
						$('bank_isdischargeddate_container','ctl00_main_bank_isdischarged_note').invoke('hide');
						$('bank_isdischargeddate').$class.doShowError(false);
						$('bank_isdischargeddate').removeClassName('required');
					});			
					
					$('bank_isgarnishedb') && ($('bank_isgarnishedb').onclick = function(){
						$('bank_isgarnishedfor_container').show();
						$('bank_isgarnishedfor').addClassName('required').focus();
					});			
					$('bank_isgarnisheda') && ($('bank_isgarnisheda').onclick = function(){
						$('bank_isgarnishedfor').$class.doShowError(false);
						$('bank_isgarnishedfor_container').hide();
						$('bank_isgarnishedfor').removeClassName('required');
					});			

					$('deposittypea') && ($('deposittypea').onclick = function(){
						$('ctl00_main_deposittype_note').hide()});			
					$('deposittypeb') && ($('deposittypeb').onclick = function(){
						$('ctl00_main_deposittype_note').show()});			


					$('loantypeb') && ($('loantypeb').onclick = function(){
						$('loanamount').removeClassName('required');
					});			
					$('loantypea') && ($('loantypea').onclick = function(){
						$('loanamount').addClassName('required').focus();
					});			

					$("info") && ($("info").onclick=function(){
						
						var a = frm.Invalid();
						alert(a.inject("",function(s,el){s+=el.id+";"; return s}));
					});

				}	else if (frm.id=='frmContact') {
					$('homephone') && ($('homephone').doComplete=validatePhone);
				}
    }
    catch(e){e.Show()}
}

function validatePostalCode() {
	if (!this.value) return true;
	if (/^\w\d\w\s*\d\w\d$/g.test(this.value)) {
		this.value = this.value.replace(/\s+/g,' ');
	} else {
		return this.$class.doShowError(true);
	}	
};

function validatePhone() {
	if (!this.value) return true;
	if (/^\(?\d{3}\)?[\s-\.]{1}\d{3}[\s.-]?\d{4}/.test(this.value)) {
		this.value = this.value.replace(/\s+/g,' ');
	} else {
		return this.$class.doShowError(true);
	}	
};


function validateSIN() {
		if (!this.value) return false;
		if (!/^((\d{3})[\s-]){2}\d{3}$/.test(this.value)) {
			this.$class.doShowError(true);
      return false;
    } else {
			this.value = this.value.replace(/[\s]/g,'-');
    }

		//detect all digits, except dash 
		var sin=this.value.replace(/[-]/g,'').toArray();	
		var even='',odd=0;
		
		//Step 1: Multiply even digit by 2
		sin.each(function(ch,i){
			if (/1|3|5|7/.test(i))
				even+=String(parseInt(ch)*2); 
		});
		//Step 2: Add the digits from step 1 together
		even = even.toArray().inject(0, function(sum, ch) {return sum + parseInt(ch)});
		
		//Step 3: Add the odd digits, but last.
		sin.each(function(ch,i){
			if (/0|2|4|6/.test(i))
				odd+=parseInt(ch); 
		});
		//Add the numbers found in step 2 and 3.
		//Subtract the units’ digits of step 4 from 10
		//Since the result is equal to the last digit, therefore, it is a valid SIN
		if ((10-(even + odd) % 10) % 10 != parseInt(sin[8])) {
			this.$class.doShowError(true);
      return false;
		}
}

function doReset(e){
	try {
		e && (Event.stop(e));
		this.select(".error").each(function(item){
			item.removeClassName("error");
		});
		this.Set();
	} catch(er) {e.Show()}	
}

function doSubmit(e) {
	try {
		e && (Event.stop(e));	
		window.focus();

		this.select(".required").each(function(item){
			var b=false;
			if (Object.isUndefined(item.value) && item.hasClassName("radio|check"))
				item.select("input").each(function(i){
					b|=i.checked;
				});
			if (!b && !item.value)
				item.$class ? item.$class.doShowError(true) : item.addClassName('error');
		});

		if (this.Invalid()) {
			alert("You have to enter a valid value to all required fields!");
			return false;
		}
		
		doSend.call(this,this.Get());
		return false;
	} catch(er) {er.Show()}	
		
}


//===========================================================================================================\dlo/
function doSend(o) {
	try {
		var message = {
		    'frmapplication' : {text:'Do you really want to apply for a loan?',color:'#F08080'},
		    'frmreloan' : {text:'Do you want to apply for a reloan??',color:'#FFFFE1'},
		    'frmcontact' : {text:'Do you really want to send this message?',color:'#66cc99'}
		    }[String(this.id).toLowerCase()];
		    
    
		this.Enable(false);
		if (confirm(message.text)) {
			  var request = new ajaxRequest(String(this.id).toLowerCase(),o);
				request.progress = new Progress("info",{message:"Sending data..."});
				request.onComplete=(function(){;this.Enable(false)}).bind(this);
			  request.onComplete=(function(){;this.Enable(true)}).bind(this);
			  request.process = (function(result){
					this.up(0).update(result.responseText);
				}).bind(this);
			  request.fetch();
			  return true;
	  } else 
			this.Enable(true);

      return false;
      
	} catch(e){e.Show()}
}


function doSubmit(e) {
	try {
		e && (Event.stop(e));
		window.focus();

		this.select(".required").each(function(item) {
			var b = false;
			if (Object.isUndefined(item.value) && item.hasClassName("radio|check"))
				item.select("input").each(function(i) {
					b |= i.checked;
				});
			if (!b && !item.value)
				item.$class ? item.$class.doShowError(true) : item.addClassName('error');
		});

		doSend.call(this, this.Get());
		return false;
	} catch (er) { er.Show() }

}


Event.observe(window,"load",doload);
