var xmlhttp;
var activeMilage;

(function($)
{
var cache = [];
$.preLoadImages = function()
{
    var args_len = arguments.length;
    for (var i = args_len; i--;)
    {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        cache.push(cacheImage);
    }
}
})(jQuery)

jQuery.preLoadImages("images/milageboxes_bg_active.jpg");

function getOptionList(type,id,oid)
{
    $.ajax({
       type: "POST",
       url: "ajaxGetTypes.php",
       data: "type="+type+"&id="+oid,
       success: function(msg) {
           $('#'+id).html(msg);
           $.jNice.SelectUpdate('#'+id);
       },
       unsuccess: function(msg){
           alert(msg);
       }
    });
    //alert(type);
}
    
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest) return new XMLHttpRequest();
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	return null;
}

function scrollToMilage()
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) { alert ("Nincs AJAX támogatás!"); return; }
    var value = document.getElementById('service_milage').value;
	var url="ajaxScroll.php";
	var url=url+"?value="+value;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
            var res=parseInt(xmlhttp.responseText);
            
            jQuery('html,body').animate({scrollTop: jQuery('#details_'+res).offset().top},'slow');
            jQuery(activeMilage).css("background", "url('images/milageboxes_bg.jpg') no-repeat");
            jQuery(activeMilage+" p.details").css("color", "#FFFFFF");
            jQuery(activeMilage+" p.type").css("color", "#FFFFFF");
            activeMilage='#details_'+res;
            jQuery(activeMilage).css("background", "url('images/milageboxes_bg_active.jpg') no-repeat");
            jQuery(activeMilage+" p.details").css("color", "#000000");
            jQuery(activeMilage+" p.type").css("color", "#000000");
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function getEvent(id)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) { alert ("Nincs AJAX támogatás!"); return; }
	var url="ajaxGetEvent.php";
	var url=url+"?id="+id;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
            var res=xmlhttp.responseText;
            //document.getElementById('details_'+res).style.border="1px solid #F57E16";
            //jQuery('html,body').animate({scrollTop: jQuery('#details_'+res).offset().top},'slow');
            //jQuery("#eventDesc").fadeOut()
            //jQuery('#eventDesc').hide(500, function() { jQuery('#eventDesc').html(res, function(){ jQuery('#eventDesc').show(500); }) });
            
            jQuery('#eventDesc').fadeOut("fast",function(){jQuery('#eventDesc').html(res);});
            jQuery('#eventDesc').fadeIn("fast");
            //document.getElementById('eventDesc').innerHTML=res;
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function noEvent()
{
    jQuery('#eventDesc').fadeOut("fast",function(){jQuery('#eventDesc').html("There is no event in this month");});
    jQuery('#eventDesc').fadeIn("fast");
}

function getCalendar(year,month)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) { alert ("Nincs AJAX támogatás!"); return; }
	var url="ajaxGetCalendar.php";
	url+="?year="+year;
	url+="&month="+month;
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
            var res=xmlhttp.responseText;
            jQuery('#calendar').fadeOut("fast",function(){jQuery('#calendar').html(res);});
            jQuery('#calendar').fadeIn("fast");
            //alert(res);
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}


function sendEmail(frm)    
{
    $.ajax({
        type: "POST",
        url: "./ajaxCaptureEmailus.php",
        data: $('#'+frm).serialize(),
        success: function(msg){
            var arrData = msg.split('##');
            if (arrData[0] == "OK") {
                //alert(arrData[1]);
                warnPopup('alert','E-mail sent!',arrData[1]);
                $('.formclear').val('');
            }else{
                //alert(" - "+arrData[0].replace(/<br \/>/gi, "\n - "));
            warnPopup('alert','E-mail sending error!',arrData[0]);
            }
        },
        unsuccess: function(msg){
            console.log(msg);
        }
    });
}
    
function sendContactForm(strType)
{
    $.ajax({
        type: "POST",
        url: "ajaxCaptureContact.php",
        data: $('#'+strType).serialize(),
        success: function(msg){
            var arrData = msg.split('##');
            if (arrData[0] == "OK") {
                //$("#popup h1").html('Thank you for your request');
                //$("#popup #popup_form").html('<div style="color:black; font-size: 15px; margin-top: -30px;">'+arrData[1]+'</div>');
                //showPopup('enquiry','Thank you!',arrData[1]);
                warnPopup('alert','Thank you!',arrData[1]);
                //resetForm(strType);
                document.getElementById(strType).reset();
            }else{
                //$("#sotetitoDiv").show();
                //$("#errorPopup").html("<h1 class='errortitle'>Error!</h1><img id='closebtn_img' src='images/close_btn.png' alt='Close' onclick='closepopup(\"errorPopup\");hideAll();' /><p id='errormsg'>" +arrData[0]+"</p>");
                warnPopup('alert',"Error",arrData[0]);
                //$("#errorPopup").html("<h1 class='errortitle'>Error!</h1><img id='closebtn_img' src='images/close_btn.png' alt='Close' onclick='closepopup(\"errorPopup\");hideAll();' /><p id='errormsg'>" +arrData[0]+"</p>");
                //$("#errorPopup").fadeIn("300");
                //$("#errorPopup").fadeIn("300");
            //alert(" - "+arrData[0].replace(/<br \/>/gi, "\n - "));
            //showPopup('enquiry','Enquiry error!',arrData[0]);
            }
        },
        unsuccess: function(msg){
            alert('Error: '+msg);
        }
    });
}

function sendServiceForm(strType)
{
    $.ajax({
        type: "POST",
        url: "ajaxCaptureService.php",
        data: $('#'+strType).serialize(),
        success: function(msg){
            var arrData = msg.split('##');
            if (arrData[0] == "OK") {
                //showPopup('enquiry','Thank you!',arrData[1]);
                warnPopup('alert','Thank you!',arrData[1]);
                //resetForm(strType);
                document.getElementById(strType).reset();
            }else{
                //$("#sotetitoDiv").show();
                //$("#errorPopup").html("<h1 class='errortitle'>Error!</h1><img id='closebtn_img' src='images/close_btn.png' alt='Close' onclick='closepopup(\"errorPopup\");hideAll();' /><p id='errormsg'>" +arrData[0]+"</p>");
                warnPopup('alert',"Error",arrData[0]);
                //$("#errorPopup").fadeIn("300");
            }
        },
        unsuccess: function(msg){
            alert('Error: '+msg);
        }
    });
}

function sendSellCarForm(strType)
{
    $.ajax({
        type: "POST",
        url: "ajaxSellCar.php",
        data: $('#'+strType).serialize(),
        success: function(msg){
            var arrData = msg.split('##');
            if (arrData[0] == "OK") {
                //showPopup('enquiry','Thank you!',arrData[1]);
                warnPopup('alert','Thank you!',arrData[1]);
                //resetForm(strType);
                document.getElementById(strType).reset();
            }else{
                //$("#sotetitoDiv").show();
                warnPopup('alert',"Error",arrData[0]);
                //$("#errorPopup").html("<h1 class='errortitle'>Error!</h1><img id='closebtn_img' src='images/close_btn.png' alt='Close' onclick='closepopup(\"errorPopup\");hideAll();' /><p id='errormsg'>" +arrData[0]+"</p>");
                //$("#errorPopup").fadeIn("300");
            }
        },
        unsuccess: function(msg){
            alert('Error: '+msg);
        }
    });
}

function closepopup(identifier){
    $('#'+identifier).fadeOut(200);
    $("#"+identifier).hide();
    if(identifier != 'errorPopup') {
        $("#sotetitoDiv").hide();
    }
}

function getNewsLetter()
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = $('#newsletter_mail').val();
    var name = $('#newsletter_name').val();
    
    if(reg.test(address) == false) {
        warnPopup('alert','E-mail error','Please give a valid e-mail address!');
        
    }else{
        
        $.ajax({
            type: "POST",
            url: "./ajaxNewsLetter.php",
            data: "newsletter_mail="+address+"&newsletter_name="+name,
            success: function(msg){
                warnPopup('alert','Newsletter information',msg);
            },
            unsuccess: function(msg){
                warnPopup(msg);
            }
        });
    }
}

function getOptionList(type,id,oid)
{
    $.ajax({
        type: "POST",
        url: "./ajaxGetTypes.php",
        data: "type="+type+"&id="+oid,
        success: function(msg) {
            $('#'+id).html(msg);
            $.jNice.SelectUpdate('#'+id);
        },
        unsuccess: function(msg){
            alert(msg);
        }
    });
}

function makePopup()
{
    jQuery("#epopup").bind('submit', function () {
        jQuery.ajax({
            type: 'POST',
            url: './ajaxSendToAFriend.php',
            data: jQuery(this).serialize(),
            success: function (data) {
                var arrData = data.split('##');
                if (arrData[0] == "OK") {
                    warnPopup('alert', 'Thank you for ...!', arrData[1]);
                    // Reset
                    if (typeof jQuery("#epopup")[0].reset == 'function') {
                        jQuery("#epopup")[0].reset();
                    } else {
                        jQuery("#epopup")[0].reset.click();
                    }
                } else {
                    //position_layer('epopup', 1, 0, -200);
                    warnPopup('alert', 'Simply complete the form and submit below!', data);
                }
            }
        })
        return false;
    })

}

function showPopup(type, p_type, title, subject)
{
    jQuery.ajax({
        type: "POST",
        url: "./ajaxShowPopup.php",
        data: "type="+type+"&title="+title+"&subject="+subject+"&p_type="+p_type,
        success: function(msg){
            jQuery('#popup').html(msg);
            jQuery('#sotetitoDiv').show();
            jQuery('#sotetitoDiv').fadeTo('fast', 0.8, function(){
                jQuery('#sotetitoDiv').bind('click',function(){
                    hideAll();
                });
                //position_layer('popup', 1, 0, -200);
                centerMe('popup');

                jQuery('#popup').fadeIn('fast');
		
            });
        },
        unsuccess: function(msg){
            alert("Please try again later!\nError: "+msg);
        }
        
    });
}

function warnPopup(type,title,message)
{
    jQuery.ajax({
        type: "POST",
        url: "./ajaxShowPopup.php",
        data: "type="+type+"&title="+title+"&message="+message,
        success: function(msg){
            jQuery('#warn').html(msg);
            jQuery('#sotetitoDiv').show();
            jQuery('#sotetitoDiv').fadeTo('fast', 0.8, function(){
                jQuery('#sotetitoDiv').bind('click',function(){
                    hideWarn();
                });
                //position_layer('warn', 1, 0, -200);
                centerMe('warn');
                jQuery('#warn').fadeIn('fast');
            });
        },
        unsuccess: function(msg){
            alert("Please try again later!\nError: "+msg);
        }
        
    });
}


function hideAll(){
    jQuery('#popup').fadeOut('fast',function(){
        jQuery('#sotetitoDiv').hide(); 
        jQuery('#popup').html(" ");
    });
}

function hideWarn(){
    jQuery('#warn').fadeOut('fast',function(){
        jQuery('#warn').html(" ");
    });
    if(jQuery('#popup').css('display')!="block")
        jQuery('#sotetitoDiv').hide(); 
}
