/***********************************\
*																*
*    JavaScript Document for popups				*
*    Coder: Szymon Kosydor vel. BuBi 				*
*  	  szymon [_at_] kosydor [_dot_] pl				*
*																*
\***********************************/

function copyHours() {
	var ar = new Array( 'tue', 'wed', 'thu', 'fri' );
	var mon_from = $( '#hours_mon_from' ).val();
	var mon_to = $( '#hours_mon_to' ).val();
	var mon_closed = $( '#hours_mon_closed' ).attr( 'checked' );
	for( i in ar ) {
		var k = '#hours_' + ar[ i ];
		$( k + '_from' ).val( mon_from );
		$( k + '_to' ).val( mon_to );
		$( k + '_closed' ).attr( 'checked', mon_closed );
	}
}

function triggerTypeIcon( types ) {
	var type = getValue( 'type' );
	var arr = types.split( '|' );
	for( i in arr ) {
		var elem = arr[ i ];
		var arr2 = elem.split( ',' );
		if( arr2[ 0 ] == type ) {
			$( '#type_icon' ).html( '<img src="/'+arr2[ 1 ]+'"/>' );
		}
	}
}

function trySendAbusePost() {
	var id = getValue( 'id' );
	var reason = getValue( 'reason' );
	var ajax = new myAjax();
	ajax.action = 'trySendAbusePost';
	ajax.post( 'id='+id+'&reason='+reason );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			hide( 'sendButton' );
			hide( 'messanger' );
			show( 'abuse_response' );
		} else {
			alert( this.response );
		}
	}
}

function trySendAbuse() {
	var id = getValue( 'id' );
	var reason = getValue( 'reason' );
	var ajax = new myAjax();
	ajax.action = 'trySendAbuse';
	ajax.post( 'id='+id+'&reason='+reason );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			hide( 'sendButton' );
			hide( 'messanger' );
			show( 'abuse_response' );
		} else {
			alert( this.response );
		}
	}
}

function saveReminder() {
	document.forms[ 'form_1' ].submit();
}

function toggleDistrictString( id, str ) {
	get( 'wojew_paste' ).innerHTML = str;
	get( 'province' ).value = id;
	var ajax = new myAjax();
	ajax.action = 'toggleCitiesUrl';
	ajax.post( 'province='+id, 'cities' );
}

function toggleCityType() {
	if( getValue( 'street_type' ) == 'wioska' ) {
		hide( 'cityLabel' );
		show( 'closeCityLabel' );
	} else {
		show( 'cityLabel' );
		hide( 'closeCityLabel' );
	}
}

function closeShadowbox() {
	Shadowbox = window.top.Shadowbox;
	Shadowbox.close();
}
/* krok drugi dodania warsztatu */
function saveWorkshop() {
	document.forms[ 'form_1' ].submit();
}

/* krok pierwszy dodania warsztatu */
function resizeShadowboxWorkshop1() {
	Shadowbox = window.top.Shadowbox;
	if( window.top.Shadowbox != undefined ) {						
		Shadowbox = window.top.Shadowbox;
		if( $.browser.msie() ) {
			if( Shadowbox.client.isIE7 ) {
				Shadowbox.adjustHeight( 350, 200, true, null );
			}
			else {
			    // o 1  px większy tutaj
				Shadowbox.adjustHeight( 325, 200, true, null );
			}

		} else {
			Shadowbox.adjustHeight( 324, 200, true, null );
		}
	}


}
document.workshopTypes = new Array();
document.selectedWorkshopTypes = new Array();
function selectWorkshopType( id, green, white ) {
    var position = document.selectedWorkshopTypes.indexOf( id );
    if( position != -1 ) {
	document.selectedWorkshopTypes.splice( position, 1 );
	get( 'img_'+id.toString() ).src = white;
    } else {
	document.selectedWorkshopTypes.push( id );
	get( 'img_'+id.toString() ).src = green;
    }
}

function addWorkshopSecond() {
	if( document.selectedWorkshopTypes.length > 0 ) {
	    get( 'type' ).value = document.selectedWorkshopTypes;
	    document.forms[ 'form_1' ].submit();
	} else {
		alert( 'Wybierz conajmniej jeden typ warsztatu' );
	}
}

function getWindowHeight() { 
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function switchEventType( type ) {
	var ch = get( 'check_' + type );
	if( ch.value == 'checkbox_on' ) {
		var imgsrc = '/graph/a_'+type+'.gif';
		ch.value = "";
	} else {
		var imgsrc = '/graph/a_'+type+'_green.gif';		
		ch.value = "checkbox_on"
	}
	get( 'img_'+type ).src = imgsrc;
}

function addEventSecond( id ) {
	var has = false;
	var ar = [ 'oil', 'light', 'wheel', 'pomp', 'belt', 'tools' ];
	for( i in ar ) {
		if( get( 'check_' + ar[ i ] ).value == 'checkbox_on' ) {
			has = true;
		}
	}
	if( has ) {
	    if( !getValue( 'pop_date' ).match( /^\d{2}\-\d{2}\-\d{4}$/ ) ) {
		alert( 'proszę wprowadzić datę wydarzenia w formacie DD-MM-RRRR' );
	    } else {
		if( !getValue( 'pop_mileage' ).match( /\d+/ ) ) {
		    alert( 'proszę wprowadzić przebieg auta w momencie wydarzenia' );
		} else {
		    var ajax = new myAjax();
		    ajax.action = 'checkPastDate';
		    ajax.post( 'date='+getValue( 'pop_date' ) );
		    ajax.onLoad = function() {
			if( this.response == 'ok' ) {
			    document.forms[ 'form_1' ].submit();
			} else {
			    alert( this.response );
			}
		    }
		}
	    }
	} else {
		alert( 'Wybierz conajmniej 1 typ wpisu' );
	}
}

function saveEvent( goBack ) {
	if( goBack == true ) {
		get( 'direction' ).value = 'back';
	}
	document.forms[ 'form_1' ].submit();
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


