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

//------------------------------------------------------------------------//
//this function allow the user to roll over the image of an input (submit)
//we simply change its class value
// Param: element -> submit input element
//------------------------------------------------------------------------//
	function rollOver(element){
		//css class name of the submit input
		var currentClassName = element.className;
		//rollover css class for an element always finish with "Hover"
		//if you want to replace this variable value, you must modify the css class name
		//*this is case sensitive.
		var hover = 'Hover';
		var index = currentClassName.indexOf("Hover");
		
		//simply switch the class name
		element.className = (index != -1)?currentClassName.substr(0,index):currentClassName + 'Hover';
	}

//------------------------------------------------------------------------//
//this function allow to click on a zodiac sign and change the hidden value "sign_{sitename}"
//then after submit the form				
// Param: element -> this is the zodiac sign link
//        formId -> this is the formId
//------------------------------------------------------------------------//
	function submitWithLink(element, formId){
		var currentSignId = element.id;												
		//hidden input id and name
		var hiddenSign = 'sign_' + formId;																			
		//change the hidden input value
		document.getElementById(hiddenSign).value = currentSignId;															
		//submit the form
		document.getElementById(formId).submit();				
	}
	
//------------------------------------------------------------------------//
//this function allow the phone form to be validated
// Validations: phone, terms and conditions
// Param: invalidtc, errnoname (origin: smarty variable)
//------------------------------------------------------------------------//		
	function submitPhone(invalidtc, errNumber){	
		var p1 = document.getElementById('phone1').value;
		var p2 = document.getElementById('phone2').value;
		var p3 = document.getElementById('phone3').value;
		
		// phone
		if (( Number(p1) != p1 || p1.length != 3) || 
			( Number(p2) != p2 || p2.length != 3) || 
			( Number(p3) != p3 || p3.length != 4) ){				
				alert(errNumber);				
				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 the media form to be validated
// Validations: email, terms and conditions
// Param: invalidtc, invalidemail -> errors to show in alerts (origin: smarty variable)
//------------------------------------------------------------------------//		
	function submitMedia(invalidtc, invalidemail){
		
		var email = document.getElementById('media-input').value;	
		
		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(invalidemail);
			return false;
		}
		
		// terms & conditions
		if (document.getElementById('tc').checked != true){
			alert(invalidtc);
			return false;
		}
		
		// 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();		
		}
		
	}
	