var formaction="";

function doPreview(){
	formaction = document.theform.action;
	document.theform.action="/cards/preview.php";
	document.theform.target="_blank";
	document.form.submit();
}
function doResetForm(){
	document.theform.action="step4";
	document.theform.target="_self";
}
function doResetForm2(){
	if(formaction!="")
			document.theform.action=formaction;
	document.theform.target="_self";
}
function changeImage(id, image){
	document.getElementById(id).src=image;
}

function addAddress(){
	
	
}

/************ FORM VALIDATION FUNCTIONS *************/

function validateNewPassword(){
	
	if(!validate())
		return false;
	if(!validateSame('password', 'retype_password'))
		return false;
	
}
function validateForgotten(){
	if(!validateEmailAddress('email_address'))
		return false;
}


function validateReferFriend(){
	if(!validate())
		return false;
	if(!validateEmailAddress('email_address'))
		return false;
	if(!validateEmailAddress('recipient_email'))
		return false;
}

function validateStep1(){
	
	if(!validate())
		return false;
	if(!validateEmailAddress('email_address'))
		return false;
	if(!validateSame("email_address", "email_address_2")){
		alert("Email addresses do not match");
		return false;
	}
	if(!validateSame("password", "password_2")){
		alert("Passwords do not match");
		return false;
	}
	return true;
}

function validateStep2(){
	if(!validate())
		return false;
	return true;	
}

function validateStep3(){

	return validate();
}

function validateStep4(){
	if(document.getElementById('csv').value==""){
		if((document.getElementById('num_recipients').value<1) || (!validate())){
			if(document.getElementById('num_recipients').value<1)
				alert("Please enter number of recipients and click 'Set'");
			return false;
		}
		else
			return true;
	}
	else
		return true;
}

function validateStep5(){
	return validate();
}

function validateLogin(){
	if(!validate())
		return false;
	if(!validateEmailAddress('email_address'))
		return false;
}

//check that all fields with classname 'required' are filled in
function validate(formname){
	if(!formname) formname="theform";
var elems=document.getElementsByTagName('input'); 
valid=true;
for(var i=0;i<elems.length;i++){
	if(elems[i].className=='required'){
		switch(elems[i].type) {
			case "text" :
				if(elems[i].value==""){
					valid=false;
					alert("Please fill in all required fields");
				}
				break;
			case "radio" :
				valid = isSelected(formname, elems[i].name);
				break;
		}
		if(!valid){
			return valid;
		}
	}
}
return true;
}
//check that all fields with classname 'required' are filled in
/**function validate(){
var elems=document.getElementsByTagName('input'); 
for(var i=0;i<elems.length;i++){
	if((elems[i].className=='required')&&(elems[i].value=="")){
		alert("Please fill in all required fields");
		return false;
	}
}
return true;
}**/

/** isChecked
 * returns true if at least one of a group of checkboxes is checked
 **/
function isChecked(formname, checkboxname){
	var isChecked = false;
	var chkbx=eval('document.'+formname+'.'+checkboxname);
	for (var i = 0; i < chkbx.length; i++) {
  	 	if (chkbx[i].checked) {
   	   	isChecked = true;
   		}
	}
	if(!isChecked)
		alert("Please tick all required checkboxes");
	return isChecked;
}

/** isSelected()
 * Returns true if one of a group of radio buttons is selected
 **/
function isSelected(formname, radioname){
	var isChecked = false;
	var chkbx=eval('document.'+formname+'.'+radioname);
	if(!chkbx.length){
		if(chkbx.checked)
			isChecked=true;
	}
	else{
		for (var i = 0; i < chkbx.length; i++) {
			if (chkbx[i].checked) {
			isChecked = true;
			}
		}
	}
	if(!isChecked)
		alert("Please tick all required Radio buttons");
	return isChecked;
}

/*** This validation function checks that an email address contains both '@' and '.' **/
function validateEmailAddress(email_address){
var email = document.getElementById(email_address).value;
	if((email.indexOf('@')==-1)||(email.indexOf('.')==-1)){
		alert("Invalid email address");
		return false;
	}
	else
		return true;
}

//Check that two fields have the same values, useful for password changes etc
function validateSame(type, retype){
if(document.getElementById(type).value == document.getElementById(retype).value)
	return true;
else{
	return false;
}
}