/* 
 * Emin KURA
 */
var index = 0;
function animateNews(){

    var size = $("#size").html();

    if(size != null){
        $("#news").hide(100, function(){
            var html = $("#"+index).html();
            $("#news").html(html);
            $("#news").show(100);
        });

        index = ((index + 1) % size ) ;
    }
}

function getDocAt(index){
    $("#searchResults").css("visibility", "hidden");
    $("#loader").css("visibility","visible");
    $.get("showdoc", {
        id: index
    }, function(e){
        $("#searchResults").html(e);
        $("#loader").css("visibility","hidden");
        $("#searchResults").css("visibility", "visible");
    });
}

function select(){
    $("#searchInput").select();
}

function searchDocs(){

    var regex2 = "";
    if(regex2 == null){
        regex2 = '';
    }
    $("#searchResults").css("visibility", "hidden");
    $("#loader").css("visibility","visible");
    $.post('search', {
        regex: regex2,
        offset:'0',
        limit:'10'
    }, function(e){
        $("#searchResults").html(e);
        $("#loader").css("visibility","hidden");
        $("#searchResults").css("visibility", "visible");
    });
}



function smsFormSubmit(){
    if(emailCorrection("mail")){
        

        var data = $("#smsForm").serialize();
        var ar = data.split('&',3);
        var code = ar[0].split('=', 3)[1];
        var name = $("#"+code).html();
        data += "&country=" + name
        

        $.post("sendsms", data, function(e){
            if(e.indexOf('success') > -1){
                alert("The download link has been sent to your mobile phone via SMS. Please check your inbox");
            }
            else{
                alert("SMS could not be sent! Please try again.");
            }
            $("#number").val("");
            $("#mail").val("");
        });
    }
   
}

function controlNumber(){

    var number = $("#number").val();
   
    
    var array = new Array();
    array[0] = '1'.charCodeAt(0);
    array[1] = '2'.charCodeAt(0);
    array[2] = '3'.charCodeAt(0);
    array[3] = '4'.charCodeAt(0);
    array[4] = '5'.charCodeAt(0);
    array[5] = '6'.charCodeAt(0);
    array[6] = '7'.charCodeAt(0);
    array[7] = '8'.charCodeAt(0);
    array[8] = '9'.charCodeAt(0);
    array[9] = '0'.charCodeAt(0);

    for(var i = 0 ; i < number.length ; i++){
        var res = false;
        for(var k = 0 ; k < 10 ; k++){
            if(number.charCodeAt(i) == array[k] ){
                res = true;
                break;
            }
        }
        if(!res){
            alert("Your number is not valid !");
            $("#number").val("");
            break;
        }

    }

}
    



function changeCode(){

    var code = "+" + $("#country").val();
    $("#countryCode").val(code);

}

function startAnimation(){
    animateNews();
    setInterval(animateNews, 10000);
}

function controlModel(){
    var vendor = $("#phone").val();
    var model = $('#pc2SelectBoxes').val();
    if(vendor == 'none'){
        alert('Please select your vendor');
        return false;
    }
    else if(model == 'Select model'){
        alert("Please select your phone model");
        return false;
    }
    else{
        loadForm('pc3');
        return true;
    }
}

function changeModel(){
    var phone = $("#phone").val();
    
    $.get("model", {
        'model':phone
    }, function(e){
        $("#model").html(e);
    });
}

function clearForm(form){

    $("#"+form).val("");

}

function formFocusOut(form , text){
    var value = $("#"+form).val();

    if(value == '')
        $("#"+form).val(text);

}

function loadForm2(form2, id2){
    $.get('form', {
        'form': form2,
        'id': id2
    }, function(e){
        $('#form').html(e);
    });
}



function loadForm(form){

    switch(form){

        case 'sms1' :
            $.get('form', {
                'form' : form
            }, function(e){
                $('#form').html(e);
            });
            break;

        case 'pc1' :
            $.get('form', {
                'form' : form
            }, function(e){
                $('#form').html(e);
            });
            break;

        case 'pc2':
            $.get('form', {
                'form' : form
            }, function(e){
                $('#form').html(e);
            });
            break;

        case 'pc3':
            $.get('form', {
                'form' : form
            }, function(e){
                $('#form').html(e);
            });
            break;

        case 'wap' :
            $.get('form', {
                'form' : form
            }, function(e){
                $('#form').html(e);
            });
            break;

        default :
            $.get('form', {
                'form' : 'pc'
            }, function(e){
                $('#form').html(e);
            });
    }


}

function submitForm(type , emailField){

    switch(type){

        case 'pc' :
            emailCorrectionAndSubmitPC(emailField)
            break;

    }
    
    return false;
}

function emailCorrectionAndSubmitPC(mailField){
    var field= "#" + mailField;
    var mail = $(field).val();
    var indexOfAt = -1 ;
    var ret = true;
    if((indexOfAt = mail.indexOf("@", 0)) > 0){

        if(mail.indexOf(".", indexOfAt) > 0){

            $.post("send" , {
                'mail' : mail
            },function(e){

                if($.trim(e.toString()) == 'error'){
                    alert("An error occurred! Please try again");
                }else{
                    loadForm2('pc2',$.trim(e.toString()));
                }
            });
        }
        else{
            ret = false;
            alert("Your e-mail address is not valid");
        }
    }
    else{
        ret = false;
        alert("Your e-mail address is not valid");
    }

    return ret;
}

function emailCorrection(mailField){
    var field= "#" + mailField;
    var mail = $(field).val();
    var indexOfAt = -1 ;
    var ret = true;
    if((indexOfAt = mail.indexOf("@", 0)) > 0){

        if(mail.indexOf(".", indexOfAt) > 0){

            return true;
        }
        else{
            ret = false;
            alert("Your e-mail address is not valid");
        }
    }
    else{
        ret = false;
        alert("Your e-mail address is not valid");
    }

    return ret;
}