function requestFinance(frm, result, from)
{
	var objForm = document.getElementById(frm),
		objResult = document.getElementById(result);
	
	if (objForm != null && objResult != null)
	{
		var TheCollector = new frmDataCollector(frm),
			params = TheCollector.frmDataToParams(),
			url = "ajaxFinanceRequest.php";
		
		params += "&from="+from;
		
		jQuery.post(url, params, function(retval){
			var arrRetval = retval.split("##");
			if (arrRetval[0] == "OK")
			{
				jQuery("#"+result).html(arrRetval[1]);
			}
			else
				alert(arrRetval[0]);
		});
	}
	else
		alert("Form is NULL!");
}
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 {
                    warnPopup('alert', 'Simply complete the form and submit below!', data);
                }
            }
        })
        return false;
    })

}

function showPopup(type,title,data,formtype)
{
	if(typeof formtype == 'undefined') formtype = 'emailus';
    jQuery.ajax({
        type: "POST",
        url: "./ajaxShowPopup.php",
        data: "type="+type+"&title="+title+"&data="+encodeURIComponent(data)+'&formtype='+formtype,
        success: function(msg){
            jQuery('#popup').html(msg);
            jQuery('#sotetitoDiv').show();
            jQuery('#sotetitoDiv').fadeTo('fast', 0.8, function(){
                jQuery('#sotetitoDiv').bind('click',function(){
                    hideAll();
                });
                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();
                });
                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(); 
}

function getModels(id,selected) {
    jQuery("#Models").html('<option value="-1">... Loading ...</option>');	
    jQuery("#Models").customSelect('refresh');
	
    jQuery.ajax({
        url: 'search_ajax.php',
        data: {
            id:id
        },
        type: 'post',
        dataType: "json",
        success: function(data){
            jQuery("#Models").html('<option value="-1">Select</option>');
            jQuery.each(data,function(index,d){
                jQuery("#Models").append('<option '+(d.id==selected?'selected="selected"':'')+' value="'+d.id+'">'+d.text+'</option>');
            });
            jQuery("#Models").customSelect('refresh');
        }
    })
}
	
	/*******NEW*******/
	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 ujvalt(obj) {
		var id= obj.id.split("_").pop();
		var name= obj.id.split("_",1);
		
		jQuery("#"+name+"_switcher > *").removeClass("sel");
		jQuery("#"+name+"_boxes > *")
			.removeClass("sel");
		jQuery("#"+name+"_label_"+id).addClass("sel");
		jQuery("#"+name+"_box_"+id)
			.addClass("sel");
	}
	/**************/
	
function Simple_ajaxcall( process, div, method, form )
{
    myFormData = '';
    if( !method )
    {
        method = 'get';
    }
    if( !form )
    {
        form = false;
    }
    else
    {
        myFormData = jQuery(form).serialize(true);
    }

    if( !process ) return false;
    if( !div ) div = 'false_div';
	
    ajaxRequest = new Ajax.Request(
        process,
        {
            method: method,
            parameters: myFormData,
            onComplete: function(request) 
            {
                var myMesArr = request.responseText.split("##");
				
                // alert(request.responseText);
                if( myMesArr[0] == 'ok' )
                {
                    if( jQuery(div) )
                    {
                        jQuery(div).innerHTML = myMesArr[1];
                    }
                    else
                    {
                        var index = 1;
                        if( !jQuery(myMesArr[index]) && jQuery('resp') && !myMesArr[index].include('jscode') )
                        {
                            // alert(myMesArr[index++]);
                            jQuery('resp').innerHTML = myMesArr[index++];
                            jQuery('resp').style.display='';
                            position_layer('resp', 1, 0, -200);
                        }
						
                        if( myMesArr[index].include('jscode') )
                        {
                            code = myMesArr[index].split('jscode');
                            // alert(code[1]);
                            if( code[1].include('js#') )
                            {
                                codes = code[1].split('js#');
                                // alert(codes.length);
                                for( var k=0; k<codes.length; k++ )
                                {
                                    eval( codes[k] );
                                }
                            }
                            else
                            {
                                eval( code[1] );
                            }
                            index++;
                        }
						
                        while( true )
                        {
                            if( jQuery(myMesArr[index]) )
                            {
                                jQuery(myMesArr[index++]).innerHTML = myMesArr[index++];
                            }
                            else
                            {
                                break;
                            }
                        }
						
                        if( myMesArr[index].include('jscode') )
                        {
                            code = myMesArr[index].split('jscode')
                            if( code[1].include('js#') )
                            {
                                codes = code[1].split('js#');
                                // alert(codes.length);
                                for( var k=0; k<codes.length; k++ )
                                {
                                    eval( codes[k] );
                                }
                            }
                            else
                            {
                                eval( code[1] );
                            }
                            index++;
                        }
                    }					
                }
                else {
                    alert(request.responseText);
                    return false;
                }
            }
        }
        ) 
}
function position_layer(div, type, korx, kory )
{
    if( !type ) type = 1;
    if( !korx ) korx = 0;
    if( !kory ) kory = 0;
	
    var dim = document.viewport.getDimensions();
    var myLayer = jQuery(div);
    AktTop = document.viewport.getScrollOffsets().top;
    mywidth = myLayer.getWidth();
    myheight = myLayer.getHeight();
    // myheight += (myheight<50 ? plusheight : 0);
	
    alert( dim.width + ' - ' + AktTop + ' :: ' + mywidth + ' - ' + myheight );
  
    newLeft = parseInt( (dim.width-mywidth)/2 );
    if(myheight > dim.height)newtop = AktTop + 10;
    else newtop = AktTop + parseInt( (dim.height-myheight)/2 );
    if(newLeft < 10)newLeft = 10;
    if(newtop < 10)newtop = 10;

    if( type == 1 )
    {
        myLayer.setStyle({
            left : newLeft + korx + "px", 
            top : newtop + kory + "px"
        });
    }
    else if( type == 2 )
    {
        myLayer.setStyle({
            left : newLeft + korx + "px"
        });
    }
    else if( type == 3 )
    {
        myLayer.setStyle({
            top : newtop + kory + "px"
        });
    }
// alert(myLayer.style.left + ' - ' + myLayer.style.top);
// myLayer.style.left = newLeft + "px";
// myLayer.style.top = newtop + "px";
}

var captcha_here_ID = 'captcha_here';
var captcha_plus_ID = 'captcha_plus';
function Init_Captcha( num )
{
    if( !num )num = '';
	
    var spamsource_ID = 'spamsource' + num;
    captcha_here_ID = 'captcha_here' + num;
    captcha_plus_ID = 'captcha_plus' + num;
	
    jQuery('#' + spamsource_ID).html( '<div class="ctextarea" style="background:none; height:auto;">'+
        '<div id="' + captcha_here_ID + '" style="float:left; width:140px; height:45px; font-size:10px !important;"></div>'+	
        '<div style="float:left;" id="' + captcha_plus_ID + '"><a href="javascript:loadCaptcha_ajax( \''+captcha_here_ID+'\', \''+captcha_plus_ID+'\' ) "><img src="captcha_files/refresh.png" border="0" alt="(New)" /></a></div>'+
        '</div>');
    loadCaptcha_ajax( captcha_here_ID, captcha_plus_ID );
}

function loadCaptcha_ajax( captcha_here_ID, captcha_plus_ID ) 
{
    jQuery("#" + captcha_plus_ID).css("display","none");
    jQuery("#" + captcha_here_ID).html('Loading code...<br />Please wait...');
    jQuery.get("captcha_files/captcha_table.php", { },
        function(data)
        {	
            jQuery("#" + captcha_here_ID).html(data);
            jQuery("#" + captcha_plus_ID).html('<a href="javascript:loadCaptcha_ajax( \''+captcha_here_ID+'\', \''+captcha_plus_ID+'\' )"><img src="captcha_files/refresh.png" border="0" alt="(New)" /></a>');
            jQuery("#" + captcha_plus_ID).css("display","block");
        }
        );
}
