// * @author     Jean-François Cartier <jfcartier@4rnd.com>
// * @copyright  2008 CELLFISH
// * @since      2008-08-20

//------------------------------------------------------------------------//
//this function allow the phone form to be validated
// Validations: phone, terms and conditions
// Param: invalidtc, errNumber, errEmail (origin: smarty variable)
//------------------------------------------------------------------------//		
	function submitPhone(invalidtc, errNumber, errEmail){	
		var phone = document.getElementById('phoneNumber').value;		
		var email = document.getElementById('email').value;		
		
		// phone
		if ( Number(phone) != phone || phone.length != 10 ){				
			alert(errNumber);				
			return false;
		}
		
		//email
		if(email != ''){			
			var at = '@';
			var dot = '.';
						
			//return true or false
			//email validation
			var validemail = (email.lastIndexOf(dot) > 2) && (email.indexOf(at) > 0) && (email.lastIndexOf(dot) > (email.indexOf(at)+1)) && (email.indexOf(at) == email.lastIndexOf(at));
			
			//invalid email
			if(!validemail){			
				alert(errEmail);
				return false;
			}
		}
		
		// terms & conditions
		if (document.getElementById('tc').checked != true){
			alert(invalidtc);
			return false;
		}
		
		// submit
		return true;
	}
				
//------------------------------------------------------------------------//
//this function allow the pin form to be validated
// Validations: pin
// Param: invalid -> errors to show in alerts (origin: smarty variable)
//------------------------------------------------------------------------//	
	function submitPin(invalid){					
		var pin = document.getElementById('pin').value;		
		
		//pin digits + length
		if (Number(pin) != pin || pin.length != 4 || pin == ''){
			alert(invalid);
			return false;
		}
		
		document.getElementById('pleasewait').style.display = 'inline';				
				
		// submit
		return true;
	}
		
//------------------------------------------------------------------------//
//this function allow to show a countDown timer
// ex: window.setTimeout('countDown(0,30,30)', 1000);
//------------------------------------------------------------------------//		
	var ctm, cts;
	function countDown(pm, ps, max){
		cts = (--ps == 0) ? max : ps; //loop or not
		document.getElementById("countDown").innerHTML = cts;
		window.setTimeout('countDown(0,' + cts + ',' + max + ');', 1000);
	}

//------------------------------------------------------------------------//
//this function allow to focus on an other field when the current one
//is full.
// Param: element -> current field
//        nbChar -> number of character that the current fill must have to be "full"
//        nextElement -> next element to focus on
//        onlyNumeric(optional) -> if true, it force user to put numeric value in text field
//------------------------------------------------------------------------//
	function nextElementFocus(element, nbChar, nextElement, onlyNumericBool){
		
		var stringValue = element.value;
		var stringLength = stringValue.length;
		
		//force numeric value
		if(onlyNumericBool){			
			
			var newString = '';
			
			for(var i = 0; i < stringLength; i++){
				
				var currChar = stringValue.substr(i , 1);		
				
				if(Number(currChar) == currChar){										
					newString += currChar;					
				}
				
			}			
			
			if(newString != stringValue){				
				element.value = newString;								
				stringLength = newString.length;
			}
			
		}
		
		if(stringLength >= nbChar){
			document.getElementById(nextElement).focus();		
		}
		
	}
	