var arrayFields = new Array();
var validationErrorsSlide;
var validationErrorsScroll;
var mySlider;
var sliderSupport;
var sliderContact;
var sliderReason;
window.addEvent('domready', function() {

    sliderReason = new Fx.Slide('reasons');
    sliderReason.hide();
    
    sliderSupport = new Fx.Slide('supportForm');

    
    mySlider = new Fx.Slide('validationErrors');
    mySlider.hide();


});
function sliderShowReasons(){
    sliderReason.toggle();
}
function sliderSupportShow(){
    
    sliderSupport.slideIn();
    sliderContact.slideOut();
    
}
function option_out(e){
    
    e.className = 'option';

}
function option_over(e){
    e.className = 'option_over';


}
function sliderShowContact(){
    sliderSupport.slideOut();
    sliderContact.slideIn();
}
function validateSupportForm(){

      var form = $('supportForm');
 
    //remove all the created elements
     arrayFields = new Array();
     
 
     
     arrayFieldsToValid = new Array();
     arrayFieldsToValid.push(new Array('txtName', 'Name', true, 'text', 2, 50));
     arrayFieldsToValid.push(new Array('txtNumber', 'Phone Number', true, 'text', 5, 30));
     arrayFieldsToValid.push( new Array('txtEmail', 'Email Address', true, 'email', 5, 50));
     arrayFieldsToValid.push( new Array('txtSuburb', 'Address', true, 'text', 6, 1000));
     arrayFieldsToValid.push( new Array('txtLetter', 'Your Letter', true, 'text', 5, 50000));
    
    
       
    for(x = 0; x < arrayFieldsToValid.length; x++){


    //validate
    if((errorV = validateInput($(arrayFieldsToValid[x][0]).getValue(), arrayFieldsToValid[x][2], arrayFieldsToValid[x][3], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5])) < 0){
    
       if(arrayFieldsToValid[x][6]){
        arrayFields.push(new Array(returnValidationDescription(errorV, arrayFieldsToValid[x][1], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5]), arrayFieldsToValid[x][0], arrayFieldsToValid[x][6]));      
       
       } else {
        arrayFields.push(new Array(returnValidationDescription(errorV, arrayFieldsToValid[x][1], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5]), arrayFieldsToValid[x][0], arrayFieldsToValid[x][0]));      
       
       }
    }
    
    }
     
 
    var errors = "The following errors must be fixed before submitting the form:<ul>";
    if(arrayFields.length > 0){
    
    
    for(x = 0; x < arrayFields.length; x++){
        
         errors += "<li>" + arrayFields[x][0] + "</li>";
    }
        
     errors += "</ul>";

    $('validationErrors').setHTML(errors);
    $('validationErrors').style.display = "block";
    
    mySlider.slideIn();    
   
    } else {
    
        
       $('supportForm').submit();
    
    
    }

}
function validateForm(){
    
   submitValidateRegisterForm();

}
function validateInput(value, required, type, min, max){
    /*
    return values
    -1:     empty when required
    -10:    email is invalid
    -20:    string length is too much
    -21:    string length too small
    -30:    int value is too high
    -31:    int value is too low
    -32:    not a number
    -33:    not an int
    */
    if(value == ""){
        return -1;
        
        } else {
    
    switch(type){
        case "email":
         
         
        
        var str = value;
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		  
		   return -10;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return -10;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return -10;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return -10;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return -10;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return -10;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return -10;
		 }

 		 return 1;
        break;
        case "text":
   
            if(value.length > max){
                //-20: too much
                return -20;
            } else if( value.length < min){
                return -21;
            }else {
                return 1;
            }                            
        break;
        case "number":
            if(isFinite(value)){
                if(value > max){
                    return -30;
                } else if(value < min){
                    return -31;
                } else {
                return 1;
                }
            } else {          
                return -32;
            }    
        break;
    }
    
    }
}
function returnValidationDescription(errno, textTitle, min, max){
    switch(errno){
        case -1:
            return  textTitle + " Field is required.";
        break;
        case -10:
            return textTitle + " Field is not a valid email address!";
        break;
        case -20:
            return textTitle + " Field should be less than " + max + " charactors in length";
        break;
        case -21:
            return textTitle + " Field should be more than " + min + " characters in length";
        break;
        case -30:
            return textTitle + " Field should be of a value between " + min + " & " + max;
        break;
        case -31:
            return textTitle + " Field should be of a value between " + min + " & " + max;
        break;
        case -32:
            return textTitle + " Field should only contain numbers";
        break;
    } 
}


function submitValidateRegisterForm(){
  
    var form = $('contactForm');
 
    //remove all the created elements
     arrayFields = new Array();
     
     
     arrayFieldsToValid = new Array();
     arrayFieldsToValid.push(new Array('txtName', 'Name', true, 'text', 2, 50));
     arrayFieldsToValid.push(new Array('txtClub', 'Club', true, 'text', 2, 50));
     arrayFieldsToValid.push(new Array('txtPhone', 'Phone Number', true, 'text', 5, 30));
     arrayFieldsToValid.push( new Array('txtEmail', 'Email Address', true, 'email', 5, 50));
     arrayFieldsToValid.push( new Array('txtEnquiry', 'Enquiry', true, 'text', 5, 50000));
    
    
       
    for(x = 0; x < arrayFieldsToValid.length; x++){


    //validate
    if((errorV = validateInput($(arrayFieldsToValid[x][0]).getValue(), arrayFieldsToValid[x][2], arrayFieldsToValid[x][3], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5])) < 0){
    
       if(arrayFieldsToValid[x][6]){
        arrayFields.push(new Array(returnValidationDescription(errorV, arrayFieldsToValid[x][1], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5]), arrayFieldsToValid[x][0], arrayFieldsToValid[x][6]));      
       
       } else {
        arrayFields.push(new Array(returnValidationDescription(errorV, arrayFieldsToValid[x][1], arrayFieldsToValid[x][4],arrayFieldsToValid[x][5]), arrayFieldsToValid[x][0], arrayFieldsToValid[x][0]));      
       
       }
    }
    
    }
     
 
    var errors = "The following errors must be fixed before submitting the form:<ul>";
    if(arrayFields.length > 0){
    
    
    for(x = 0; x < arrayFields.length; x++){
        
         errors += "<li>" + arrayFields[x][0] + "</li>";
    }
        
     errors += "</ul>";

    $('validationErrors').setHTML(errors);
    $('validationErrors').style.display = "block";
    
    mySlider.slideIn();    
   
    } else {
    
        
       $('contactForm').submit();
    
    
    }
              
}

