function validateMTNChallengeLogin(form){
    if(form.email.value == ''){
        alert('Please supply us with your email address');
        form.email.focus();
        return false;
    }else{
        if(!checkEmail(form.email.value)){
            alert('Please supply a valid email address');
            form.email.select();
            return false;
        }
    }
    
    if(form.password.value == ''){
        alert('Please supply us with your password');
        form.password.focus();
        return false;
    }
    
    var myAjax = new Ajax('/mtn_challenge/login/?email='+form.email.value+'&password='+form.password.value,{
        method:'get',
        async:false,
        onComplete:function(response){
            if(response == '1'){
                alert('Welcome, you are about to be redirected');
                window.location.href = '/mtn_challenge/';
            }else{
                alert('The email and password combination seems to be wrong , please try again');
                form.email.select();
            }
        }
    }).request();
    
return false;
}

function validateBbgComment(form){
    if(form.email.value == ''){
        alert('Please supply your email address');
        form.email.focus();
        return false;
    }else{
        if(!checkEmail(form.email.value)){
            alert('Please supply a valid email address');
            form.email.select();
            return false;
        }
    }
    
    if(form.comment.value == ''){
        alert('Please supply a comment');
        form.comment.focus();
        return false;
    }
    
    return true;
}

function validateChallengeAsk(form){
    if(form.full_name.value == ''){
        alert('Please supply your full name');
        form.full_name.focus();
        return false;
    }
    
    if(form.email.value == ''){
        alert('Please supply your email address');
        form.email.focus();
        return false;
    }else{
        if(!checkEmail(form.email.value)){
            alert('Please supply a valid email address');
            form.email.select();
            return false;
        }
    }
    
    if(form.question.value == ''){
        alert('Please supply your question');
        form.question.focus();
        return false;
    }
    
    var myAjax = new Ajax('/twelve_week_challenge/ask_the_coach/',{
        method:'post',
        data:$('challenge_ask').toQueryString(),
        async:false,
        onComplete:function(response){
            if(response == '1'){
                alert('You question has been processed and sent, Thank you for your time.');
            }else{
                alert('We encountered an issue while sending your message, Please try again.');
            }
        }
    }).request();
    
return false;
}

function forgotMTNChallengePassword(){
    if(email = prompt('Please enter in the email address you used to register your account')){
        if(email == ''){
            alert('You didnt enter in anything, Please try again');
            return false;
        }else{
            if(!checkEmail(email)){
                alert('Please supply a valid email address');
                return false;
            }
        }
        
        var myAjax = new Ajax('/mtn_challenge/forgotPassword/?email='+email,{
            method:'get',
            async:false,
            onComplete:function(response){
                switch(response){
                    case '0': alert('We cant find any record of the email address supplied, Please try again'); break;
                    case '1': alert('The details have been sent to the email address supplied'); break;
                    case '2': alert('We found your details, but we encountered an issue while trying to email them. Please try again'); break;
                }
            }
        }).request();
    }
return false;
}

function forgotChallengePassword(){
    if(email = prompt('Please enter in the email address you used to register your account')){
        if(email == ''){
            alert('You didnt enter in anything, Please try again');
            return false;
        }else{
            if(!checkEmail(email)){
                alert('Please supply a valid email address');
                return false;
            }
        }
        
        var myAjax = new Ajax('/twelve_week_challenge/forgotPassword/?email='+email,{
            method:'get',
            async:false,
            onComplete:function(response){
                switch(response){
                    case '0': alert('We cant find any record of the email address supplied, Please try again'); break;
                    case '1': alert('The details have been sent to the email address supplied'); break;
                    case '2': alert('We found your details, but we encountered an issue while trying to email them. Please try again'); break;
                }
            }
        }).request();
    }
return false;
}

function validateChallengeLogin(form){
    if(form.email.value == ''){
        alert('Please supply us with your email address');
        form.email.focus();
        return false;
    }else{
        if(!checkEmail(form.email.value)){
            alert('Please supply a valid email address');
            form.email.select();
            return false;
        }
    }
    
    if(form.password.value == ''){
        alert('Please supply us with your password');
        form.password.focus();
        return false;
    }
    
    var myAjax = new Ajax('/twelve_week_challenge/login/?email='+form.email.value+'&password='+form.password.value,{
        method:'get',
        async:false,
        onComplete:function(response){
            if(response == '1'){
                alert('Welcome, you are about to be redirected');
                window.location.href = '/twelve_week_challenge/';
            }else{
                alert('The email and password combination seems to be wrong , please try again');
                form.email.select();
            }
        }
    }).request();
    
return false;
}

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}

function getBrandsByTypeId(type_id){
    var myAjax = new Ajax('/bbg/getBrandsByTypeId/'+type_id,{
        method:'get',
        onComplete:function(response){
            var data = Json.evaluate(response,true);
            var count = 0;
            var selected_val = $("brand_id").value;
            $("brand_id").empty();
            
            var opt = document.createElement("option");
            opt.innerHTML = 'Any';
            opt.value = 0;
            $("brand_id").appendChild(opt);
            
            $each(data, function(val,key){
                    selected = (selected_val == key)?true:false;
                    var opt = document.createElement("option");
                    (selected)?opt.setAttribute("selected","selected"):'';
                    opt.innerHTML = val;
                    opt.value = key;
                    
                    $("brand_id").appendChild(opt);
                    
                    count++;
            });
            
            if(count){
                $("brand_id").disabled = false;
                getModelsByBrandId($("brand_id").value);
            }else{
                $("brand_id").disabled = true;
            }
        }
    }).request();
}

function getModelsByBrandId(brand_id){
    var myAjax = new Ajax('/bbg/getModelsByBrandId/'+brand_id+'/'+$("type_id").value,{
        method:'get',
        onComplete:function(response){
            var data = Json.evaluate(response,true);
            var count = 0;
            var selected_val = $("model_id").value;
           
            $("model_id").empty();
            
            var opt = document.createElement("option");
            opt.innerHTML = 'Any';
            opt.value = 0;
            $("model_id").appendChild(opt);
            
            $each(data, function(val,key){
                    selected = (selected_val == key)?true:false;
                    var opt = document.createElement("option");
                    (selected)?opt.setAttribute("selected","selected"):'';
                    opt.innerHTML = val;
                    opt.value = key;
                    
                    $("model_id").appendChild(opt);
                    count++;
            });
            
            if(count){
                $("model_id").disabled = false;
            }else{
                $("model_id").disabled = true;
            }
        }
    }).request();
}

/** validateSecondRegistration **/
function validateStoreRegistration(form){
    if(form.password1.value != ''){
        if(form.password2.value != ''){
            if(form.password1.value != form.password2.value){
                alert('Your passwords dont match, please make sure they are the same');
                form.password2.select();
                return false;
            }
        }else{
            alert('Please make sure you have confirmed your passsword');
            form.password2.select();
            return false;
        }
    }
return true;
}

/** validateComment **/
function validateComment(form){
    var name = $('name').getValue();
    var email = $('email').getValue();
    var comment = $('comment').getValue();

    if((name == '') || (name == 'Name')){
        alert('Please supply your name');
         $('name').select();
         return false;
    }
    if(!checkEmail(email)){
         alert('Please supply a valid email address');
         $('email').select();
         return false;
    }
    if((comment == '') || (comment == 'Comment')){
        alert('Please supply a comment');
         $('comment').select();
         return false;
    }
    
return true;
}

/** checkEmail **/
function checkEmail(x){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!(filter.test(x))) {
		return false;
	}else{
		return true;
	}
}

/** forgotten password **/
function forgotPassword(url){
    email = prompt('Please enter in your email address','');
    url += email;
    var myAjax = new Ajax(url,{onComplete: function(request){
                        alert(request);
                     }
                 }).request();
    return false;
}

/** hide the flash message on the toolbox layout **/
function hideFlash() {
    if (document.getElementById('flash')) {
        new Effect.Fade('flash', {'duration': 5});
    }
}

/** Image Carosel **/
window.addEvent('domready',function(){
    var myGallery;
    function startGallery() {
        myGallery = new gallery($('myGallery'), {
            timed: true,
            showArrows: false,
            showInfopane: false,
            showCarousel: false,
            embedLinks: false,
            delay: 8000
        });
        document.gallery = myGallery;
    }
    startGallery();
});


function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//-->
