NicoTyres = new Object();

NicoTyres.baseUrl = null;
NicoTyres.xmlDocument = null;

NicoTyres.loadXMLFromFile = function( path, handler ) {
	/*
    if ( document.implementation && document.implementation.createDocument ) {
        NicoTyres.xmlDocument = document.implementation.createDocument( "", "", null );
        NicoTyres.xmlDocument.onload = function() {
            NicoTyres.xmlDocument.isLoaded = true;
            if ( handler ) {
                handler( NicoTyres.xmlDocument );
            }
        }*/
	if(window.XMLHttpRequest) {
		var xmlhttp = new window.XMLHttpRequest();
		xmlhttp.open("GET", path, false);
		xmlhttp.send(null);
		NicoTyres.xmlDocument = xmlhttp.responseXML.documentElement;
		if( handler ) {
			handler( NicoTyres.xmlDocument );
		}
    }
    else {
        if ( window.ActiveXObject ) {
            NicoTyres.xmlDocument = new ActiveXObject("Microsoft.XMLDOM"); ;
            NicoTyres.xmlDocument.onreadystatechange = function () {
                if (NicoTyres.xmlDocument.readyState == 4) {
                    if ( handler ) {
                        handler( NicoTyres.xmlDocument );
                    }
                }
            };
        }
        else {
            throw "Lame browser, dude! Get Firefox";
        }
		NicoTyres.xmlDocument.load( path );
    }
}

NicoTyres.mergiLaPaginaDeCautare = function() {
    document.location = NicoTyres.baseUrl + 'anvelope/#q=' + encodeURIComponent( $( 'cautare' ).value );
}


Event.observe(  window,
                'load',
                function() {
                    NicoTyres.cosCumparaturi.actualizeaza();
                },
                false );


noSpam = function( user, domain ){
    document.location = 'mailto:' + user + '@' + domain;
}

NicoTyres.pentruDobitoci = function( text, actiune ) {
    $( 'cautare' ).value = text;
	jBookmarker.remove('pagina');
    if ( actiune == true ) {
        NicoTyres.mergiLaPaginaDeCautare();
    }
    else {
        NicoTyres.cautareAnvelope.cauta();
    }
}

NicoTyres.getParams = function( linkId ) {
    if ( ( $( 'AnvelopaClient' ).value == '' ) || ( $( 'AnvelopaAgent' ).value == '' ) || ( $( 'AnvelopaTel' ).value == '' ) || ( $( 'AnvelopaTitlu' ).value == '' ) )
    {
        alert( 'Vă rugăm să completaţi datele personale!' );
        return false;
    }
    else
    {
        $( linkId ).href = $( linkId ).href + '?client=' + $( 'AnvelopaClient' ).value + '&agent=' + $( 'AnvelopaAgent' ).value + '&tel=' + $( 'AnvelopaTel' ).value + '&titlu=' + $( 'AnvelopaTitlu' ).value;
        return true;
    }
}

NicoTyres.trimiteFormularAbonareNsl = function( formId, inputId )
{
    var emailRegExp = /^[-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/;

    var adresaEmail = $( inputId ).value;

    var expresie = adresaEmail.match( emailRegExp );

    if ( !expresie )
    {
        alert( 'Vă rugăm să specificaţi o adresă de e-mail validă!' );
        return false;
    }
    else
    {
        var url = NicoTyres.baseUrl + 'Contact/abonare_nsl/';
        new Ajax.Request( url,
                    {
                        asynchronous:   true,
                        method: 'post',
                        parameters: Form.serialize( formId ),
                        onSuccess:
                            function () {
                                alert( 'Vă mulţumim pentru înscriere!\nO scrisoare de confirmare a fost trimisă la adresa specificată' );
                            }
                    }
        );
    }
    return false;
}

NicoTyres.trimiteFeedback = function( formId ){
	var url = NicoTyres.baseUrl + 'Feedback/trimite/';
// 	alert(url);
	new Ajax.Updater( formId, url, {
		asynchronous: true,
		methos: 'post',
		parameters: Form.serialize( formId )
	} );

	return false;
}

NicoTyres.nslBlur = function( inputId )
{
    if ( $( inputId ).value == '' )
    {
        $( inputId ).value = 'Adresa dvs. de e-mail';
        return;
    }
}

NicoTyres.nslClick = function( inputId )
{
    if ( $( inputId ).value == 'Adresa dvs. de e-mail' )
    {
        $( inputId ).value = '';
    }
}
